NYOJ 坦克大战(宽度优先搜索)

来源:互联网 发布:中经网产业数据库 编辑:程序博客网 时间:2024/06/06 15:41

坦克大战

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. 
What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture). 

Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?
输入
The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.
输出
For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.
样例输入
3 4YBEBEERESSTE0 0
样例输出
8
这个和一般宽搜不大一样,普通宽搜只需将能走的路放进队列即可,而这一题需将可打破的墙也考虑进去。我的方法是第一次碰到能打破的墙,也放进队列,但标记为已打破,与普通位置做区别,第一次从队列里取到该点时,依旧放进队列,但标记为已走过。此时可作为普通位置处理。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<vector>using namespace std;struct S{int x,y;int time;//int ff=0;}p;queue<S> que;char map[301][301];int m,n,f;int bz[301][301];int xx,yy,xr,yr;int mintime;int a[5]={0,1,-1,0};int b[5]={1,0,0,-1};void bfs(){int x,y;p.x=xx;  p.y=yy;p.time=0;que.push(p);bz[xx][yy] = 1;//x=que.front().x;  y=que.front().y;/////////cout<<x<<" "<<y<<endl;////////////while(!que.empty())///{//cout<<"@@"<<endl;////////////x=que.front().x;  y=que.front().y;//cout<<x<<" "<<y<<" Time:"<<que.front().time<<endl;////////////if(x == xr && y == yr) {f=1; mintime=que.front().time;break;}if(map[x][y] == 'B' && bz[x][y] == -2)  //即原本这个点为墙 {  //还没人走过 p.x=x; p.y=y; p.time=que.front().time+1;que.push(p);bz[x][y] = 1;    //变为已走过的空地 }else{for(int i=0;i<4;i++){      if( x+a[i]>=0 && x+a[i]<m && y+b[i]>=0 && y+b[i]<n && bz[x+a[i]][y+b[i]] != 1 && bz[x+a[i]][y+b[i]] != -2 )//标记-2表示墙已被打破 {      p.x=x+a[i]; p.y=y+b[i]; p.time=que.front().time+1;que.push(p);if(bz[x+a[i]][y+b[i]] != -1) bz[x+a[i]][y+b[i]] = 1;    //变为已走过的空地if(bz[x+a[i]][y+b[i]] == -1) bz[x+a[i]][y+b[i]] = -2;}}//for}que.pop();}//while(!que.empty())}int main(){while(cin>>m>>n && m){memset(bz,0,sizeof(bz));while(!que.empty())que.pop();              //初始化 f=0;for(int i=0;i<m;i++)scanf("%s",map[i]);   //inputfor(int i=0;i<m;i++){for(int j=0;j<n;j++){if(map[i][j] == 'B') bz[i][j] = -1;else if(map[i][j] == 'R' || map[i][j] == 'S') bz[i][j] = 1;  //表示走不过 else if(map[i][j] == 'Y') {xx=i;  yy=j;}else if(map[i][j] == 'T') {xr=i;  yr=j;}}}                 //标记 bfs(); if(f) cout<<mintime<<endl;else cout<<-1<<endl;}return 0;} 
1 0
原创粉丝点击