Battle City

来源:互联网 发布:4g网络1g流量能用多久 编辑:程序博客网 时间:2024/04/30 09:24
Battle City
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7208 Accepted: 2427

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

Source

POJ Monthly,鲁小石

#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;char map[1010][1010];int vis[1010][1010],m,n,x,y;int dx[4]={0,1,-1,0};int dy[4]={1,0,0,-1};struct node {int x,y;int step;friend bool operator < (node n1,node n2){return n1.step>n2.step;}}p,temp;int judge(node s){if(s.x<0||s.x>=m||s.y<0||s.y>=n)return 1;if(map[s.x][s.y]=='S'||map[s.x][s.y]=='R')return 1;if(vis[s.x][s.y])return 1;return 0;}int bfs(){priority_queue<node>q;p.x=x;p.y=y;p.step=0;memset(vis,0,sizeof(map));vis[x][y]=1;q.push(p);while(!q.empty()){p=q.top();q.pop();for(int i=0;i<4;i++){temp.x=p.x+dx[i];temp.y=p.y+dy[i];if(judge(temp)) continue;if(map[temp.x][temp.y]=='B')temp.step=p.step+2;else temp.step=p.step+1;if(map[temp.x][temp.y]=='T')return temp.step;vis[temp.x][temp.y]=1;q.push(temp);}}return -1;}int main(){while(scanf("%d%d",&m,&n),m||n){int i,j;for(i=0;i<m;i++){scanf("%s",map[i]);for(j=0;j<n;j++){if(map[i][j]=='Y'){x=i;y=j;}}}printf("%d\n",bfs());}return 0;}


0 0
原创粉丝点击