坦克大战
时间限制: 1 Sec 内存限制: 64 MB提交: 16 解决: 10
[ 提交][ 状态][ 讨论版]
题目描述
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).
输入
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 4
YBEB
EERE
SSTE
0 0
样例输出
8
//优先队列 # include<stdio.h> # include<queue> using namespace std; struct node{ int x,y; int step; friend bool operator<(node a,node b) { return a.step>b.step; } }; int n,m,begin1,begin2,end1,end2,b[4][2]={0,1,1,0,0,-1,-1,0}; char a[305][305]; bool dfs() { priority_queue<node>s; node q,p; q.x=begin1; q.y=begin2; q.step=0; s.push(q); while(!s.empty()) { q=s.top(); s.pop(); if(q.x==end1&&q.y==end2) { printf("%d\n",q.step); return true; } for(int i=0;i<4;i++) { p.x=q.x+b[i][0];//控制4个方向 p.y=q.y+b[i][1]; if(a[p.x][p.y]=='B') p.step=q.step+2;//如果是墙就走两步 else p.step=q.step+1; if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&(a[p.x][p.y]=='B'||a[p.x][p.y]=='E'||a[p.x][p.y]=='T')) { a[p.x][p.y]='R';//标记不在重复走 s.push(p);//如果符合条件就入队列 } } } return false; } int main(){ while(scanf("%d%d",&n,&m)&&m&&n) { getchar(); int i,j; for(i=0;i<n;i++){ for(j=0;j<m;j++) scanf("%c",&a[i][j]); getchar(); } for(i=0;i<n;i++) for(j=0;j<m;j++) { if(a[i][j]=='Y') { begin1=i; begin2=j; } if(a[i][j]=='T') { end1=i; end2=j; } } if(!dfs()) printf("-1\n"); } return 0; }