POJ 2312 Battle City【Bfs+优先队列】

来源:互联网 发布:linux下etc没有inittab 编辑:程序博客网 时间:2024/05/29 14:32

 

Battle City

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 8114

 

Accepted: 2716

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,鲁小石

 

题目大意:坦克大战都玩过0...0,这里提供几种点,Y表示自己,T表示目标,R表示河,S表示打不动的石头,B表示能打动的石头,E表示空地,你有八种操作,每种操作都耗时为1.

1、四个方向行走,四种。

2、向四个方向开炮,四种。


思路:没事开炮多耗时,。假如我们有一个必须打的石头,使得打完这个石头能够更优的达到目标,那我们再干这个石头,并且贴着它开炮干掉。相当于直接BFS,遇到这样的石头,用两个单位时间走到这里(一个单位开炮,一个单位走到这里)。那么我们如何判断这个石头是不是这样的一个石头呢?我们并不需要判断,我们直接BFS,如果遇到了一个石头,我们无论它是不是这种石头,我们都给他干掉,加入队列中,因为我们维护的不是最小步数而是最小时间花费,这个时候我们直接用一个优先队列就能解决这个问题,让当前最优情况先行即可。


AC代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct zuobiao{    int x,y,output;    friend bool operator <(zuobiao a,zuobiao b)    {        return a.output>b.output;    }}now,nex;int n,m;char a[500][500];int vis[500][500];int fx[4]={0,0,1,-1};int fy[4]={1,-1,0,0};void Bfs(int x,int y){    memset(vis,0,sizeof(vis));    priority_queue<zuobiao>s;    now.x=x;    now.y=y;    now.output=0;    s.push(now);    vis[now.x][now.y]=1;    while(!s.empty())    {        now=s.top();        if(a[now.x][now.y]=='T')        {            printf("%d\n",now.output);            return ;        }        s.pop();        for(int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            nex.y=now.y+fy[i];            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='S'&&a[nex.x][nex.y]!='R')            {                vis[nex.x][nex.y]=1;                if(a[nex.x][nex.y]=='E'||a[nex.x][nex.y]=='T')                {                    nex.output=now.output+1;                    s.push(nex);                }                if(a[nex.x][nex.y]=='B')                {                    nex.output=now.output+2;                    s.push(nex);                }            }        }    }    printf("-1\n");    return ;}int main(){    while(~scanf("%d%d",&n,&m))    {        if(n+m==0)break;        int sx,sy;        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);            for(int j=0;j<m;j++)            {                if(a[i][j]=='Y')                {                    sx=i;sy=j;                }            }        }        Bfs(sx,sy);    }}







0 0
原创粉丝点击