Battle City(POJ - 2312)(深搜+优先队列)

Battle City
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8877 Accepted: 2965

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


题意理解:就是一个起点,遇到R和S不能走,遇到B花费为2,遇到E花费为1,问走到T的花费最小是多少。

解题思路:因为每个点的花费不一样,那么我们就用优先队列加广搜,优先走步数最小的。走过的就不用再走了,因为你遇到的走过的一定花费比你小。所以只需要用一个二维数组记录有没有走过这些点就好了,当找到T的时候,就直接打印结果就好。还有千万不要忘了如果没有能走到Tde方案,就打印 -1。

#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
struct s{
    int x;   //坐标X
    int y;   //坐标Y
    int step;  //所用步数
    friend bool operator<(s a,s b)
    {
        return a.step>b.step;    //优先走步数小的
    }
};
int book[310][310];    //记录是否有走过
char map_[310][310];   //存入地图
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            return 0;
        priority_queue<s>q;
        memset(book,0,sizeof(book));
        memset(map_,0,sizeof(map_));
        int x1=0,y1=0;
        for(int i=0;i<n;i++)
        {
            getchar();
            for(int j=0;j<m;j++)
            {
                scanf("%c",&map_[i][j]);
                if(map_[i][j]=='Y')
                {
                    x1=i;
                    y1=j;
                }
            }
        }
        s start;
        start.x=x1;
        start.y=y1;
        start.step=0;
        q.push(start);
        int flag=0;
        while(!q.empty())
        {
            s temp=q.top();
            q.pop();
            int next[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
            for(int i=0;i<4;i++)   //向四个方向找
            {
                s tmp;
                tmp.x=temp.x+next[i][0];
                tmp.y=temp.y+next[i][1];
                if(tmp.x>n||tmp.y>m||tmp.x<0||tmp.y<0)
                    continue;
                if(book[tmp.x][tmp.y]==0)
                {
                    if(map_[tmp.x][tmp.y]=='B')
                    {
                        tmp.step=temp.step+2;
                        book[tmp.x][tmp.y]=1;
                        q.push(tmp);
                    }
                    else if(map_[tmp.x][tmp.y]=='E')
                    {
                        tmp.step=temp.step+1;
                        book[tmp.x][tmp.y]=1;
                        q.push(tmp);
                    }
                    else if(map_[tmp.x][tmp.y]=='T')
                    {
                        tmp.step=temp.step+1;
                        flag=1;
                        printf("%d\n",tmp.step);
                        break;
                    }
                }
                }
                if(flag==1)
                {
                    break;
                }
            }
            if(flag==0)
            {
                printf("-1\n");
            }
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值