This is a prototype of the tank war. It is a variant of the maze. Given a map of mxn, there are ordinary bricks B, bricks S, and river R on the map, open space E, and a treasure location T, and your location Y, find the minimum number of steps to eat the treasure (two steps are required for tanks to pass through ordinary brick B, open space E step, cannot pass through the bricks and rivers )..
#include <queue>#include <iostream>using namespace std;int n,m;int mintime[305][305];char map[305][305];struct point{int x,y,time;}s,e;int h[4][2]={1,0,-1,0,0,-1,0,1};queue<point> q;int bfs(point s){int i,j,x,y; point t,tt;while(!q.empty()) q.pop();q.push(s); mintime[s.x][s.y]=0;while(!q.empty()){t=q.front(); q.pop();for(i=0;i<4;i++){x=t.x+h[i][0]; y=t.y+h[i][1];if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='R'&&map[x][y]!='S'){tt.x=x; tt.y=y; tt.time=t.time+1;if(map[x][y]=='B') tt.time++;if(tt.time<mintime[x][y]){q.push(tt); mintime[x][y]=tt.time;}}}}return mintime[e.x][e.y];}int main(int argc, char *argv[]){int i,j;while(cin>>n>>m&&(n+m)){for(i=0;i<n;i++)for(j=0;j<m;j++){cin>>map[i][j]; mintime[i][j]=100000005;if(map[i][j]=='Y') { s.x=i; s.y=j; s.time=0;}if(map[i][j]=='T') { e.x=i; e.y=j;}}int ans=bfs(s);if(ans<100000005) cout<<ans<<endl;else cout<<-1<<endl;}return 0;}