POJ 2312 Battle City

来源:互联网 发布:超级玛丽mac版 编辑:程序博客网 时间:2024/05/17 08:15

                                                                                                                                 Battle City
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9170 Accepted: 3043

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.

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?

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

3 4YBEBEERESSTE0 0

Sample Output

8

感觉和HDU的War Chess有点像。

题目大意:求从起点到终点最少需要多少步。已知S和R是不能走得,E是空的,可以走,B是砖,只有打掉后才可以通过。

也是用优先队列和广搜

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int m,n,startx,starty,endx,endy;char map[310][310];int book[310][310];int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};struct Node{  int x,y,step;  friend bool operator < (Node a,Node b){    return a.step>b.step;  }};bool judge(int x,int y){  if(x<1||y<1||x>m||y>n||map[x][y]=='S'||map[x][y]=='R') return true;  return false;}int bfs(){  priority_queue<Node> que;  memset(book,0,sizeof(book));  Node begin;  begin.x=startx;  begin.y=starty;  begin.step=0;  book[startx][starty]=1;  que.push(begin);  while(!que.empty()){    Node a=que.top();    //que.pop();    if(a.x==endx&&a.y==endy) return a.step;    que.pop();    for(int i=0;i<4;i++){      Node next=a;      next.x+=dx[i];      next.y+=dy[i];      if(book[next.x][next.y]) continue;      if(judge(next.x,next.y)) continue;      if(map[next.x][next.y]=='B') next.step+=2;      else next.step+=1;      book[next.x][next.y]=1;      que.push(next);    }  }  return -1;}int main(){  while(~scanf("%d%d",&m,&n)){    getchar();    if(!m&&!n) break;    for(int i=1;i<=m;i++){      for(int j=1;j<=n;j++){        scanf("%c",&map[i][j]);        if(map[i][j]=='Y'){          startx=i;          starty=j;        }        if(map[i][j]=='T'){          endx=i;          endy=j;        }      }      getchar();    }    printf("%d\n",bfs());  }}