Battle City (BFS+优先队列)

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

这道题目需要优先队列+广搜来求解
至于不能直接用广搜,我想应该是由于其中要经过砖墙时时间消耗为2,这使得广搜中的最优解难以得到
如下图:
这里写图片描述
颜色的深浅代表加入的先后,第一次加入B E,第二次加入E B,第三次加入E,第四次加入T
那么得到Y B B E T的路径而最优路径是Y E B E T,这就需要使用优先队列
来自:http://blog.csdn.net/smoggyxhdz/article/details/75670028

感觉他写的还是蛮好的,至少解决了自己的问题,之前感觉都是在尝试么,这有什么区别,都是遍历所有的点,这样的话,没有区别啊。自己忽略了一点就是,这个节点的选择会影响到下一个节点,也就是路径的问题。因为这里让我们去求最小么,所以我们在每一个点拓展的时候先去找step最小的(也就是离起始点最小的),这样的话才能维持一直都是最小step的末点在队列的最上面。这样的话那你说要是下面大了怎么办,我们一直都在维护这个有序列,就是按照最小的step排序的,并且是还能走到当前位置的点,只要他一直小,我们就可以一直放在上面,如果下面发现他大了,优先队列自动调整,不符合条件的自动清空了,这样的话,始终把最小的步数的放在上面,只要这个点能到,那就是他了,如果他大了,不好意思,我们就要换人了,也就是换了一条路径。

如果要是只是队列的话,那么就只是在找每个节点的可行的路,并且按照层状结构的最小路,实际上呢,这并不一定是最小路径

讲真,这里BFS应该就只剩下向四周找了,对于那种层状结构已经不存在了

#include<stdio.h>
#include<string.h>
#include<queue>
typedef long long ll;
using namespace std;
//写一下思路吧,就是找路,然后每次都选那个距离起始点最小的去尝试,这样的话才能保证最小么,要是不优化的话,直接都是1个顺序,上下左右,这样的话,就出现上面讲的情况了
//我们不用管优先队列里面的顺序,只想最后一步操作,有好多可以到达终点,我们肯定选那个最小的

//还有这里的X,Y,转了90度,看着范围
/*
x y y y y y y
x
x
x
x
x
*/
//最好全局变量,这样还比较把功能抽出来
int st_x,st_y,end_x,end_y;
char map[301][301];
int seen_point[301][301];
int move_x[4]={0,0,-1,1};
int move_y[4]={1,-1,0,0};//上下左右
int H,W;

struct node
{
    int x,y,step;
    bool friend operator <(node a,node b)
    {
        return   a.step > b.step;
    }
}now,next;

int no_way(int x,int y)
{
    if(x<0||y<0||x>=H||y>=W||seen_point[x][y]||map[x][y]=='R'||map[x][y]=='S')
        return 1;  //妈的,这里写错了,改了半个小时,找不到错误
    else
        return 0;
}

int bfs()
{

    priority_queue<node> pq;
    //将开始点放入到队列里面
    //x,y,step,seen_point,四个变量
    now.x=st_x;
    now.y=st_y;
    now.step=0;
    seen_point[now.x][now.y]=1;
    pq.push(now);
    //进行广搜
    while(!pq.empty())
    {
        //拿出队列的首
        now=pq.top();//这个东西,竟然可以直接将结构体赋值
        pq.pop();
        //看看是不是要找的元素
        if(now.x==end_x && now.y==end_y)//找到該点
            return now.step;
        //不是要找的元素,进行广搜,四个方向
        for(int i=0;i<4;i++)
        {
            //确定好下一个tank的位置,step
            next.x=now.x+move_x[i];
            next.y=now.y+move_y[i];
            next.step=now.step;
            //看看位置对不对
            if(no_way(next.x,next.y))
                continue;
             //位置对了,看看应该加几
            if(map[next.x][next.y]=='B')
                next.step += 2;
            else
                next.step ++;
            //这个点可以走,并且已经走过了,进行标记
            seen_point[next.x][next.y]=1;
            //将这个点放入到队列中
            pq.push(next);
        }
    }
    //没找到通路
    return -1;
}

int main()
{
    while(~scanf("%d %d",&H,&W))
    {
        if(H==0&&W==0)
            break;
        memset(map,0,sizeof(map));
        memset(seen_point,0,sizeof(seen_point));

        for(int i=0;i<H;i++)
        {
            scanf("%s",map[i]);
            for(int j=0;j<W;j++)
            {
                if(map[i][j]=='Y')
                {
                    st_x=i;
                    st_y=j;
                }
                if(map[i][j]=='T')
                {
                    end_x=i;
                    end_y=j;
                }
            }
        }
        ll ans;
        ans=bfs();
        printf("%I64d\n",ans);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值