坦克大战
时间限制: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 4 YBEB EERE SSTE 0 0
样例输出
-
8
-
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.
题目大概意思很简单,就是“输入m行n列”,其中 "Y" 代表是你的坦克所在位置;"T" 是你的目标,途中的 "S" 代表不能通过的钢铁墙,"R" 代表不能通过的河流,"B" 代表要浪费你一个子弹的土墙(即走一步相当于走两步路),"E" 可以走代表空地。这题即相当于最短路径问题。深搜广搜都行。不过我自己做的是深搜,广搜是黏贴一大牛的代码。
深搜具体代码如下:
#include<stdio.h>
#include<string.h>
int m,n,a[3],b[2],s[302][302];
char map[302][302];
void fun(int w,int e,int x,int y)
{
if(x<0||y<0||x==m||y==n) return ;
if(map[x][y]=='R'||map[x][y]=='S'||map[x][y]=='Y') return ;
if(x==b[0]&&y==b[1])
{
if(s[x][y]==0) s[x][y]=s[w][e]+1;
else
{
if(s[x][y]<s[w][e]+1) return ;
else s[x][y]=s[w][e]+1;
}
}
if(map[x][y]=='B')
{
if(s[x][y]==0) s[x][y]=s[w][e]+2;
else
{
if(s[x][y]<s[w][e]+2) return ;
else s[x][y]=s[w][e]+2;
}
}
if(map[x][y]=='E')
{
if(s[x][y]==0) s[x][y]=s[w][e]+1;
else
{
if(s[x][y]<s[w][e]+1) return ;
else s[x][y]=s[w][e]+1;
}
}
fun(x,y,x-1,y);fun(x,y,x+1,y);fun(x,y,x,y-1);fun(x,y,x,y+1);
}
int main()
{
int k,i,j,z[5];
while(scanf("%d%d",&m,&n),m,n)
{
memset(s,0,sizeof(s));
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
if(map[i][j]=='Y') {a[0]=i;a[1]=j;}
if(map[i][j]=='T') {b[0]=i;b[1]=j;}
}
}
fun(a[0],a[1],a[0]-1,a[1]);
fun(a[0],a[1],a[0],a[1]-1);
fun(a[0],a[1],a[0]+1,a[1]);
fun(a[0],a[1],a[0],a[1]+1);
if(s[b[0]][b[1]]==0)
printf("-1\n");
else
printf("%d\n",s[b[0]][b[1]]);
}
}
广搜具体代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
char map[301][301];
int row,col;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int x;
int y;
int c;
friend bool operator < (const node &a,const node &b)
{
return a.c>b.c;
}
};
node begin1,end1;
priority_queue<node>data;
int bfs()
{
int i=0;
node tem;
while( !data.empty() )
{
begin1=data.top();
data.pop();
if( (begin1.x==end1.x) && (begin1.y==end1.y))
return begin1.c;
for(i=0;i<4;i++)
{
tem.x=begin1.x+dir[i][0];
tem.y=begin1.y+dir[i][1];
if(tem.x>=0 && tem.x<row && tem.y>=0 && tem.y<col && map[tem.x][tem.y]!='1')
{
if(map[tem.x][tem.y]=='B')
{
tem.c=begin1.c+2;
data.push(tem);
map[tem.x][tem.y]='1';
}
else
{
if(map[tem.x][tem.y]=='E' || map[tem.x][tem.y]=='T')
{
tem.c=begin1.c+1;
data.push(tem);
map[tem.x][tem.y]='1';
}
}
}
}
}
return 0;
}
int main()
{
int i=0,j=0;
int step;
while(scanf("%d%d",&row,&col)&&(row+col))
{
step=0;
memset(map,'\0',sizeof(map));
for(i=0;i<row;i++)
{
scanf("%s",map[i]);
for(j=0;j<col;j++)
{
if(map[i][j]=='Y')
{
begin1.x=i;
begin1.y=j;
begin1.c=0;
}
if(map[i][j]=='T')
{
end1.x=i;
end1.y=j;
end1.c=0;
}
}
}
data.push(begin1);
map[begin1.x][begin1.y]='1';
step=bfs();
if(step>0)
printf("%d\n",step);
else
printf("-1\n");
while(!data.empty())
data.pop();
}
return 0;
}