坦克大战
1000 ms | 内存限制: 65535
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 4YBEB EERE SSTE 0 0
样例输出
8
//唉,南阳又崩了,写了两个题,提交一直在判题。。。不知正确与否。。先贴上明天再提交试试。。。
//题意:
//Y是坦克所在位置
//B表示砖墙,要花费1分钟将其摧毁变为自由空间,(也就是说遇到砖墙,坦克要花费两分钟走一格)
//R是河,坦克不能过
//S是铁墙,坦克不能过
//E是自由空间,坦克可自由行走,每走一格花费一分钟
//T是目的地
//哦,这个程序今天提交一下WA。。。改了一下过了
<pre class="cpp" name="code">#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int b[310][310];
char a[310][310];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m;
struct zz
{
int x;
int y;
int l;
friend bool operator<(zz a,zz b)
{
return a.l>b.l;
}
}f1,f2;
void bfs(int x,int y)
{
priority_queue<zz>q;
memset(b,0,sizeof(b));
f1.x=x;f1.y=y;f1.l=0;
q.push(f1);
b[x][y]=1;
while(!q.empty())
{
f1=q.top();
q.pop();
for(int i=0;i<4;i++)
{
f2.x=f1.x+dx[i];
f2.y=f1.y+dy[i];
if(a[f2.x][f2.y]=='T')
{
printf("%d\n",f1.l+1);
return ;
}
if(f2.x>=1&&f2.x<=n&&f2.y>=1&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='R'&&a[f2.x][f2.y]!='S')
{
b[f2.x][f2.y]=1;
if(a[f2.x][f2.y]=='B')
{
f2.l=f1.l+2;
a[f2.x][f2.y]='E';
}
else
{
f2.l=f1.l+1;
a[f2.x][f2.y]='E';
}
q.push(f2);
}
}
}
printf("-1\n");
}
int main()
{
int i,j,sx,sy;
while(scanf("%d%d",&n,&m),n|m)
{
for(i=1;i<=n;i++)
scanf("%s",a[i]+1);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]=='Y')
{
sx=i;
sy=j;
}
}
}
bfs(sx,sy);
}
return 0;
}
//这个也是正确的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int b[310][310];
char a[310][310];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m,flag,cnt;
struct zz
{
int x;
int y;
int l;
friend bool operator<(zz a,zz b)
{
return a.l>b.l;
}
}f1,f2;
void bfs(int x,int y)
{
priority_queue<zz>q;
f1.x=x;f1.y=y;f1.l=0;
b[x][y]=1;
q.push(f1);
while(!q.empty())
{
f1=q.top();
q.pop();
for(int i=0;i<4;i++)
{
f2.x=f1.x+dx[i];
f2.y=f1.y+dy[i];
if(a[f2.x][f2.y]=='T')
{
flag=1;
cnt=f1.l+1;
return ;
}
else if(f2.x>=1&&f2.x<=n&&f2.y>=1&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='R'&&a[f2.x][f2.y]!='S')
{
if(a[f2.x][f2.y]=='B')
f2.l=f1.l+2;
else
f2.l=f1.l+1;
b[f2.x][f2.y]=1;
q.push(f2);
}
}
}
}
int main()
{
int i,j,sx,sy;
while(scanf("%d%d",&n,&m),n&&m)
{
for(i=1;i<=n;i++)
scanf("%s",a[i]+1);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]=='Y')
{
sx=i;
sy=j;
}
}
}
memset(b,0,sizeof(b));
flag=0;
bfs(sx,sy);
if(flag)
printf("%d\n",cnt);
else
printf("-1\n");
}
return 0;
}