• hdu 1885 Key Task(bfs)


    http://acm.hdu.edu.cn/showproblem.php?pid=1885

    再贴一个链接http://blog.csdn.net/u013081425/article/details/21756237

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <stack>
    #include <vector>
    #include <queue>
    #define LL long long
    #define _LL __int64
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    
    int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
    struct node
    {
        int x,y;
        int sta;
        int step;
    };
    
    int n,m;
    int sx,sy;
    char map[110][110];
    int mark[110][110][(1<<4)+10];
    
    queue <struct node>que;
    
    int ans;
    
    int small(char ch)
    {
        if(ch == 'b')
            return 0;
        else if(ch == 'y')
            return 1;
        else if(ch == 'r')
            return 2;
        else if(ch == 'g')
            return 3;
    }
    
    int big(char ch)
    {
        if(ch == 'B')
            return 0;
        else if(ch == 'Y')
            return 1;
        else if(ch == 'R')
            return 2;
        else if(ch == 'G')
            return 3;
    }
    
    void bfs()
    {
        while(!que.empty()) que.pop();
        memset(mark,0,sizeof(mark));
    
        mark[sx][sy][0] = 1;
        que.push((struct node) {sx,sy,0,0});
    
        while(!que.empty())
        {
            struct node u = que.front();
            que.pop();
            if(map[u.x][u.y] == 'X')
            {
                ans = u.step;
                return;
            }
    
            for(int d = 0; d < 4; d++)
            {
                int x = u.x + dir[d][0];
                int y = u.y + dir[d][1];
                int sta = u.sta;
    
                if(x < 1 || x > n || y < 1 || y > m) continue;
    
                if(map[x][y] == '.' || map[x][y] == '*' || map[x][y] == 'X') //注意出口‘X’也要进队列
                {
                    if(!mark[x][y][sta])
                    {
                        mark[x][y][sta] = 1;
                        que.push((struct node){x,y,sta,u.step+1});
                    }
                }
                else if(map[x][y] >= 'a' && map[x][y] <= 'z')
                {
                    int add = small(map[x][y]);
    
                    if((sta&(1<<add)) == 0)
                        sta += (1 << add);
                    if(!mark[x][y][sta])
                    {
                        mark[x][y][sta] = 1;
                        que.push((struct node){x,y,sta,u.step+1});
                    }
                }
                else if(map[x][y] >= 'A' && map[x][y] <= 'Z')
                {
                    int add = big(map[x][y]);
                    if( (sta&(1<<add)) && !mark[x][y][sta] )
                    {
                        mark[x][y][sta] = 1;
                        que.push((struct node){x,y,sta,u.step+1});
                    }
                }
            }
        }
    }
    
    
    int main()
    {
        while(~scanf("%d %d",&n,&m))
        {
            if(n == 0 && m == 0) break;
            for(int i = 1; i <= n; i++)
            {
                scanf("%s",map[i]+1);
                for(int j = 1; j <= m; j++)
                {
                    if(map[i][j] == '*')
                    {
                        sx = i;
                        sy = j;
                    }
                }
            }
    
            ans = -1;
            bfs();
            if(ans == -1)
                printf("The poor student is trapped!
    ");
            else printf("Escape possible in %d steps.
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    codeforces 733D
    HDU2647
    匈牙利算法 DFS模板(了解度+1)
    HDU1532 网络流:最大流之福德福克森算法
    mysql5 解压版 安装 建用户授权采坑
    Spring Boot 相关随笔
    Spring Boot 项目jar包 构建运行
    随笔小结
    war包 jar包理解(记录)
    vue axios异步请求及回调函数(前台)
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6798421.html
Copyright © 2020-2023  润新知