欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 2312 Battle City

程序员文章站 2023-12-24 12:03:57
...

Battle City
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9370   Accepted: 3101

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. 
POJ 2312 Battle City

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). 
POJ 2312 Battle City

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

思路:砖墙相当于要走2步,使用优先队列,步数小的在前,找到第一个到目的地 的步数即可。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

char s[305];
int map_1[305][305],mov[4][2]={0,1,1,0,0,-1,-1,0};

struct node
{
    int x,y,s;
    friend bool operator < (node a,node b){
        return a.s>b.s;
    }
}st,ne;

int main() {
    int row,column,i,j,ex,ey;
    while(scanf("%d%d",&row,&column),row+column){
        memset(map_1,0,sizeof(map_1));
        
//初始化地图 不可行为0,普通路为1,砖墙为2
//地图从1行1列开始保存便于判断是否越界
//普通路1,砖墙2便于计算步数
        for(i=1;i<=row;i++){
            scanf("%s",s);
            for(j=1;j<=column;j++){
                if(s[j-1]=='E')map_1[i][j]=1;
                else if(s[j-1]=='B')map_1[i][j]=2;
                else if(s[j-1]=='Y'){
                    st.s=0;
                    st.x=i;
                    st.y=j;
                }
                else if(s[j-1]=='T'){
//记录终点
                    ex=i;
                    ey=j;
                }
            }
        }
        priority_queue <node> q;    //定义一个优先队列
        
        q.push(st);     //起点入队
        
        int flag=0;     //判断是否存在答案
        
//BFS
        while(!q.empty()){
            st=q.top();
            q.pop();
            for(i=0;i<4;i++){
                ne.x=st.x+mov[i][0];
                ne.y=st.y+mov[i][1];

//判断下一步是否为终点
                if(ne.x==ex&&ne.y==ey){
                    flag=1;
                    break;
                }

//越界,河流,钢墙都被0标记
                if(map_1[ne.x][ne.y]==0)continue;
                
//步数是当前步数加上下一步地点的数据
                ne.s=st.s+map_1[ne.x][ne.y];
                map_1[ne.x][ne.y]=0;
                q.push(ne);
            }
            if(flag)break;
        }
        if(flag)printf("%d\n",st.s+1);
        else printf("-1\n");
    }
    return 0;
}


相关标签: bfs

上一篇:

下一篇: