POJ 2312 Battle City ——————BFS

妖狐艹你老母 2022-05-13 12:40 298阅读 0赞

Language:Default

Battle City














Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10110 Accepted: 3364

Description

Many of us had played the game “Battle city” in our childhood, and some people (like me) even often play it on computer now. ![这里写图片描述](http://poj.org/images/2312\_1.jpg)
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). ![这里写图片描述](http://poj.org/images/2312\_2.jpg)
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?

Input

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.

Output

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.

Sample Input

  1. 3 4
  2. YBEB
  3. EERE
  4. SSTE
  5. 0 0

Sample Output

  1. 8

Source

POJ Monthly,鲁小石

—– BFS 简单题 —–

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. using namespace std;
  6. const int MAXN=309;
  7. int n,m,stx,sty,edx,edy,ans;
  8. char str[MAXN][MAXN];
  9. bool vis[MAXN][MAXN];
  10. int d[4][2]={
  11. 0,1, 0,-1, 1,0, -1,0};
  12. struct node{
  13. int x,y,step;
  14. node(){};
  15. node(int _x,int _y,int _step)
  16. {
  17. x=_x;y=_y;step=_step;
  18. }
  19. bool operator < (const node &b)const
  20. {
  21. return step>b.step;
  22. }
  23. };
  24. void bfs(int x,int y)
  25. {
  26. memset(vis,0,sizeof(vis));
  27. priority_queue<node> que;
  28. node e1,e2;
  29. vis[x][y]=1;
  30. que.push(node(x,y,0));
  31. while(que.size())
  32. {
  33. e1=que.top();que.pop();
  34. if(str[e1.x][e1.y]=='T')
  35. {
  36. ans=e1.step;
  37. break;
  38. }
  39. for(int i=0;i<4;i++)
  40. {
  41. e2.x=e1.x+d[i][0];
  42. e2.y=e1.y+d[i][1];
  43. e2.step=e1.step+1;
  44. if(!vis[e2.x][e2.y] && 0<= e2.x && e2.x < n && 0 <= e2.y && e2.y <m && str[e2.x][e2.y]!='R' && str[e2.x][e2.y]!= 'S')
  45. {
  46. vis[e2.x][e2.y]=1;
  47. if(str[e2.x][e2.y]=='B') e2.step++;
  48. que.push(e2);
  49. }
  50. }
  51. }
  52. }
  53. int main()
  54. {
  55. while(~scanf("%d %d",&n,&m)&&(n|m))
  56. {
  57. for(int i=0;i<n;i++) scanf("%s",str[i]);
  58. for(int i=0;i<n;i++)
  59. for(int j=0;j<m;j++)
  60. {
  61. if(str[i][j]=='Y')
  62. {
  63. stx=i;
  64. sty=j;
  65. break;
  66. }
  67. }
  68. ans=-1;
  69. bfs(stx,sty);
  70. printf("%d\n",ans);
  71. }
  72. return 0;
  73. }

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读

    相关 poj 3126 BFS

        BFS得到的一定是最短路径。开始我还在纠结怎么才是最短的呢。其实BFS的题目有个共性(这不废话,哪一类题没有共性啊。呵呵)。以后做这种题自己慢慢总结吧。     

    相关 poj3414_bfs

    题意:这个题以前见过,但是从没有用代码实现过,题意就是,给出两个杯子的容量,求是否可以通过fill,drop,pour,一系列操作,最后得出某一个容量的液体,如果可以则输出最少