POJ 2312 Battle City(優先キュー+bfs)
3713 ワード
Problem Description
Many of us had playd the game“Battle city”in our childhood,and some people(like me)even offプレイit on computer now.
What we are discussing is a simple edition of this game.Gven a map that consists of empy spaces,rivers,steel walls and brick walls only.Your task to get a bonus sons as possible the space wintement.
Your tank can't move through rivers or walls、but t t itcan destststrybrick walls byshoting.A brik wall will be turned into empty spaces when hit it、however、iyoursht Shothit a steel steel wall asteell wall、there there there there there will there will there will there will there be be chchororororore e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e wawawawawawawawall thererererererererererereretttttスペースor shot in one of the four directions without a move.The shot will go aead in that direction,until it go out of the map or hit a wall.If the shot hists a brick wall,the wall will disappear(i.the.the.the dent gint.ingnit)how many turns will take 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 follwing M lins contains N uppercase letters,each of which of which is'wageone's'wagell's's's's's's's'wagell's'wagell's's's's'wagell's's's's's's's's's's's's's's's's's'aaaaaaaaaaatttttspace.Both'Y'and'T'appar only once.A test case of M=N=0 indicates the end of input,and shound not be processed.
Output
For each test case,please output the turns you tate least in a separate line.If you can't arrive at the target,output'-1'instead.
Sample Input
Sample Output
件名:
戦車は起点(Y)から目的地(T)まで、鉄壁(S)、川(R)を通ってはいけません.空き地で走行(E)し、レンガの壁(B)を壊して射撃します.レンガの壁を撃つ時は歩かないで、単位の時間がかかります.空き地で歩く時も、一人の時間がかかります.タンクは起点から目的地まで少なくともどれぐらいの時間がかかりますか?出力に達することができません.
考え方:
この問題は広く検索する思想を使って、ただ隊を出る時処理をしただけで、優先列を利用して列の中で起点の時間まで最小の値の先にチームを出します.この方法は優先列のSTLを用いる.
コード:
Many of us had playd the game“Battle city”in our childhood,and some people(like me)even offプレイit on computer now.
What we are discussing is a simple edition of this game.Gven a map that consists of empy spaces,rivers,steel walls and brick walls only.Your task to get a bonus sons as possible the space wintement.
Your tank can't move through rivers or walls、but t t itcan destststrybrick walls byshoting.A brik wall will be turned into empty spaces when hit it、however、iyoursht Shothit a steel steel wall asteell wall、there there there there there will there will there will there will there be be chchororororore e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e wawawawawawawawall thererererererererererereretttttスペースor shot in one of the four directions without a move.The shot will go aead in that direction,until it go out of the map or hit a wall.If the shot hists a brick wall,the wall will disappear(i.the.the.the dent gint.ingnit)how many turns will take 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 follwing M lins contains N uppercase letters,each of which of which is'wageone's'wagell's's's's's's's'wagell's'wagell's's's's'wagell's's's's's's's's's's's's's's's's's'aaaaaaaaaaatttttspace.Both'Y'and'T'appar only once.A test case of M=N=0 indicates the end of input,and shound not be processed.
Output
For each test case,please output the turns you tate least in a separate line.If you can't arrive at the target,output'-1'instead.
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8
件名:
戦車は起点(Y)から目的地(T)まで、鉄壁(S)、川(R)を通ってはいけません.空き地で走行(E)し、レンガの壁(B)を壊して射撃します.レンガの壁を撃つ時は歩かないで、単位の時間がかかります.空き地で歩く時も、一人の時間がかかります.タンクは起点から目的地まで少なくともどれぐらいの時間がかかりますか?出力に達することができません.
考え方:
この問題は広く検索する思想を使って、ただ隊を出る時処理をしただけで、優先列を利用して列の中で起点の時間まで最小の値の先にチームを出します.この方法は優先列のSTLを用いる.
コード:
#include
#include
#include
#include
#include
using namespace std;
struct node {
int x,y,s;
friend bool operator b.s;
}
};
char map[305][305];
int n,m;
int vis[305][305];
int dir[][2]={
{0,1},{0,-1},{1,0},{-1,0}
};
int sx,sy,ex,ey;
bool ok(int x,int y)
{
if(x>=0&&x=0&&y q;
node head;
memset(vis,1,sizeof(vis));
head.x=sx,head.y=sy,head.s=0;
q.push(head);
vis[sx][sy]=0;
while(!q.empty())
{
node f=q.top();
q.pop();
if(f.x==ex&&f.y==ey)
return f.s;
for(int i=0;i<4;i++)
{
int dx=dir[i][0]+f.x,dy=f.y+dir[i][1];
if(ok(dx,dy)&&vis[dx][dy])
{
vis[dx][dy]=0;
int temp;
if(map[dx][dy]=='B') temp=2;
else temp=1;
node ss;
ss.x=dx,ss.y=dy,ss.s=f.s+temp;
q.push(ss);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)&&n+m)
{
for(int i=0;i>map[i][j];
if(map[i][j]=='Y') sx=i,sy=j;
else if(map[i][j]=='T') ex=i,ey=j;
}
}
printf("%d
",bfs());
}
return 0;
}