• 位置数组zoj1649 BFS


    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧!

        

        Rescue

        

        


        

        Time Limit:

        2 Seconds     

        Memory Limit:

        65536 KB

        

        


        Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

        Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

        You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

        
    Input

        First line contains two integers stand for N and M.

        Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

        Process to the end of the file.

        
    Output

        For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

        
    Sample Input

        7 8
    #.#####.
    #.a#..r.
    #..#x...
    ..#..#.#
    #...##..
    .#......
    ........

        
    Sample Output

        13

            因为普通的BFS是按照步数优先来求解的,但对于本题来说,步数最少的解确纷歧定是最优解。如:

        2 5

        axxxr

        .....

        每日一道理
    我拽着春姑娘的衣裙,春姑娘把我带到了绿色的世界里。

        明显按照普通的BFS求解的话,结果是7(即笔挺向左走到头),但最优解应该是6。为了解决这个抵触,我们可以另设一个数组,记载从出发点走到以后位置的最少时光,然后在BFS的过程中,只要从以后位置走到下一个位置的时光小于下一个位置的最小时光,就入队。此处还可以做一个优化,为了避免同一个节点重复入队,我们用优先队列,按照时光由小到大的顺序扩展。

    #include <iostream>
    #include<cstdio>
    #include<queue>
    
    using namespace std;
    struct st
    {
        int x,y;
        int time;
        bool operator<(const st t)const
        {
            return time>t.time;
        }
    }cur,next;
    char map[205][205];
    int mt[205][205];//记载最少时光
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    int n,m;
    
    bool path(int x,int y)
    {
        if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#')
            return true;
        return false;
    }
    
    int BFS(int sx,int sy)
    {
        priority_queue<st>q;
        cur.x=sx;
        cur.y=sy;
        cur.time=0;
        mt[sx][sy]=0;
        q.push(cur);
        while(!q.empty())
        {
            cur=q.top();
            q.pop();
            if(map[cur.x][cur.y]=='a')
                return cur.time;
            for(int i=0;i<4;i++)
            {
                next.x=cur.x+dir[i][0];
                next.y=cur.y+dir[i][1];
                next.time=cur.time+1;
                if(map[next.x][next.y]=='x')
                    next.time++;
                if(path(next.x,next.y)&&next.time<mt[next.x][next.y])
                {
                    mt[next.x][next.y]=next.time;
                    q.push(next);
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        int i,j,sx,sy;
        while(~scanf("%d%d",&n,&m))
        {
            for(i=0;i<n;i++)
                scanf("%s",map[i]);
            for(i=0;i<n;i++)
                for(j=0;j<m;j++)
                {
                    if(map[i][j]=='r')
                    {
                        sx=i;
                        sy=j;
                    }
                    mt[i][j]=100000000;
                }
            int res=BFS(sx,sy);
            if(res==-1)
                printf("Poor ANGEL has to stay in the prison all his life.\n");
            else
                printf("%d\n",res);
        }
        return 0;
    }

        
     

    文章结束给大家分享下程序员的一些笑话语录: 现在社会太数字化了,所以最好是有一个集很多功能于一身的设备!

  • 相关阅读:
    Java Excel导入
    Git在Eclipse中忽略文件提交
    Git客户端安装(仅限windows用户)
    Java输出流文件下载
    Centos6.3源码安装Mysql-5.5.34
    Centos6.3安装Mongodb2.2.4
    Jacob操作Word各格式转换参数
    sublime text3使用小结
    获得select下拉框的值
    sublim text3 配置
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3087740.html
Copyright © 2020-2023  润新知