您的位置:首页 > 其它

(POJ 2312)Battle City 优先队列 & 简单BFS

2017-10-07 23:06 399 查看
Battle City

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 9324 Accepted: 3086

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

Source

POJ Monthly,鲁小石

题意:

给你一个m行n列的矩阵。

Y代表起点,T代表终点。B、E可以走,S、R不可以走,B的时间花费为2,E为1.

求Y到T的最短时间。

分析:

直接就是简单的BFS即可。。

(脑残的我忘了将终点情况加入下一状态中。。。。。吐血。。。。)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const int maxn = 310;
int m,n,sx,sy;
char g[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

struct tank
{
int x,y,cost;
friend bool operator < (const tank &a,const tank &b)
{
return a.cost > b.cost;
}
};

bool check(int x,int y)
{
if(x < 0 || x >= m || y < 0 || y >= n) return 0;
if(g[x][y] == 'S' || g[x][y] == 'R' || vis[x][y] ) return 0;
return 1;
}

bool bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<tank> q;
tank now,next;
now.x = sx; now.y = sy; now.cost = 0;
q.push(now);
vis[sx][sy] = 1;
while(!q.empty())
{
now = q.top(); q.pop();
if(g[now.x][now.y] == 'T')
{
printf("%d\n",now.cost);
return true;
}
for(int i=0;i<4;i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
if(check(next.x,next.y))
{
if(g[next.x][next.y] == 'E' || g[next.x][next.y] == 'T') next.cost = now.cost + 1; //别忘了g[next.x][next.y] == 'T'
else if(g[next.x][next.y] == 'B') next.cost = now.cost + 2;
q.push(next);
vis[next.x][next.y] = 1;
}
}
}
return false;
}

int main()
{
while(scanf("%d%d",&m,&n)!=EOF && m)
{
getchar();
for(int i=0;i<m;i++) gets(g[i]);
int f = 0;
for(int i=0;i<m;i++)
{
if(f) break;
for(int j=0;j<n;j++)
{
if(g[i][j] == 'Y')
{
sx = i;
sy = j;
f = 1;
break;
}
}
}
if(!bfs()) printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: