坦克大战
来源:互联网 发布:淘宝差评如何防止骚扰 编辑:程序博客网 时间:2024/04/28 10:40
坦克大战
时间限制: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<cstdio>#include<cstdlib>#include<cstring>#include<queue>using namespace std;char map[310][310];int starx,stary,endx,endy,m,n,mov[][2]={1,0,-1,0,0,1,0,-1},vis[310][310];structNode{int x,y,turn;friend bool operator<(Node a,Node b){return a.turn>b.turn;}}s;intBFS(int h,int c){priority_queue<Node>Q;s.x=h;s.y=c;s.turn=0;Q.push(s);Node u,v;int xx,yy;vis[h][c]=1;while(!Q.empty()){u=Q.top();Q.pop();h=u.x;c=u.y;for(int i=0;i<4;++i){xx=h+mov[i][0];yy=c+mov[i][1];if(xx>=0&&xx<m&&yy>=0&&yy<n&&vis[xx][yy]==0&&(map[xx][yy]=='B'||map[xx][yy]=='E'||map[xx][yy]=='T')){v=u;if(xx==endx&&yy==endy)return v.turn+1;v.x=xx;v.y=yy;if(map[xx][yy]=='E')v.turn +=1;elsev.turn+=2;vis[xx][yy]=1;Q.push(v);}}}return -1;}int main(){int i,j;while(scanf("%d%d",&m,&n)==2){if(m==0&&n==0)break;for(i=0;i<m;++i){getchar();for(j=0;j<n;j++){scanf("%c",&map[i][j]);if(map[i][j]=='Y'){starx=i;stary=j;}if(map[i][j]=='T'){endx=i;endy=j;}}}memset(vis,0,sizeof(vis));printf("%d\n",BFS(starx,stary));}return 0;}
0 0
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 坦克大战
- 姚博文 cte 公用表表达式Common Table Expression
- C/C++ typedef用法详解(真的很详细)
- 几种字符串截取、分割方法
- Visual Studio .net 2003安装故障处理
- .NET网站建设之session和cookie
- 坦克大战
- android-ndk-r10c下载地址
- python同步文件
- _seq_example.py
- 【最简单IOC容器实现】实现一个最简单的IOC容器
- mysql_经典的mysql数据库集群、 备份 <Replication>。
- sql转义
- Spring笔记(二):Ioc 之注入与装配
- Ubuntu、gcc、kernel版本之间的复杂关系小结