您的位置:首页 > 其它

POJ - 2312 Battle City (记忆话搜索bfs+优先队列)

2017-07-22 15:59 381 查看
题目链接:http://poj.org/problem?id=2312点击打开链接

Battle City

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8873 Accepted: 2963
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 4
YBEB
EERE
SSTE
0 0

Sample Output
8


步数少的优先判断 如果碰到B就步数+2

碰到河流跟坚石就跳过

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int book[333][333];
char mmap[333][333];
int beginx;
int beginy;
int endx;
int endy;
struct xjy
{
int x;
int y;
int step;
bool operator < (const xjy &r)const
{
return step>r.step;
}
};
int dir[4][2]={1,0,-1,0,0,1,0,-1};
priority_queue <xjy> q;
void bfs()
{
xjy mid;
mid.x=beginx;
mid.y=beginy;
mid.step=0;
q.push(mid);
while(!q.empty())
{

mid=q.top();
//printf("%d %d %d",mid.x,mid.y,mid.step);
//getchar();
q.pop();
if(book[mid.x][mid.y]<mid.step)
continue;
else
{
for(int i=0;i<=3;i++)
{
xjy midmid;
midmid.x=mid.x+dir[i][0];
midmid.y=mid.y+dir[i][1];
midmid.step=mid.step;
if(mmap[midmid.x][midmid.y]=='E')
{
if(book[midmid.x][midmid.y]<=(midmid.step+1))
continue;
else
{
midmid.step++;
book[midmid.x][midmid.y]=midmid.step;
q.push(midmid);
}
}
else if(mmap[midmid.x][midmid.y]=='S'||mmap[midmid.x][midmid.y]=='Y'||mmap[midmid.x][midmid.y]=='R')
{
continue;
}
else if(mmap[midmi
bf3e
d.x][midmid.y]=='B')
{
if(book[midmid.x][midmid.y]<=(midmid.step+2))
continue;
else
{
midmid.step+=2;
book[midmid.x][midmid.y]=midmid.step;
q.push(midmid);
}
}
else if(mmap[midmid.x][midmid.y]=='T')
{
if(book[midmid.x][midmid.y]<=(midmid.step+1))
continue;
else
{
midmid.step++;
book[midmid.x][midmid.y]=midmid.step;
}
}
}
}
}

}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&n!=0&&m!=0)
{
for(int i=0;i<=300;i++)
{
for(int j=0;j<=300;j++)
book[i][j]=90000;
}
memset(mmap,'R',sizeof(mmap));
getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&mmap[i][j]);
if(mmap[i][j]=='Y')
{
beginx=i;
beginy=j;
}
else if(mmap[i][j]=='T')
{
endx=i;
endy=j;
}
}
getchar();
}

bfs();
if(book[endx][endy]!=90000)
printf("%d\n",book[endx][endy]);
else
printf("-1\n");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: