zoukankan      html  css  js  c++  java
  • 杭电2102--A计划(Bfs)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2102

    骑士救公主,迷宫问题。做的时候思路不清晰,一直Wa, 其实就是没搞清楚从posa-->posb无论b是传送门还是路都会耗时1, 只不过经过传送门又传送到了下一个位置;一直在这个误区里走不出来;Tmdi.
    bfs搜一遍就说明搜过来就说明posa一定为点,代码中又把特殊情况全部排除,

    所以只有两种情况:

             '.' --->'#',

              '.'-->'.'(包含p, 为搜索结束条件),

    瞬间明朗了很多啊。

    ac代码:

    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    char map[2][12][12];
    int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
    int n, m, T;
    struct Maze
    {
        int x, y, z, step;    
    } r, s, t;
    bool Bfs(int x, int y, int z)
    {
        r.x = x; r.y = y; r.z = z; r.step = 0;
        queue<Maze> q;
        q.push(r);
        map[x][y][z] = '*';
        while(!q.empty())
        {
            s = q.front();
            q.pop();
            for(int i = 0; i < 4; i++)
            {
                t = s; 
                t.y = s.y + ac[i][0];
                t.z = s.z + ac[i][1];
                t.step = s.step + 1;
                if(t.z >= 0 && t.y >= 0 && t.y < n && t.z < m && map[t.x][t.y][t.z] != '*')
                {
                    if(map[t.x][t.y][t.z] == '#'){
                        map[t.x][t.y][t.z] = '*';     //标记为'*', 因为后面要标记传送到的地方, 不标记就成了断路; 
                        t.x = !s.x;
                    }
                    if(map[t.x][t.y][t.z] == 'P' && t.step <= T)
                        return true;
                    map[t.x][t.y][t.z] = '*';
                    q.push(t);    
                }
            } 
        }
        return false;
    } 
    int main()
    {
        int Qi;
        scanf("%d", &Qi);
        while(Qi--)
        {
            scanf("%d %d %d", &n, &m, &T);
            for(int i = 0; i < 2; i++)
                for(int j = 0; j < n; j++)
                    for(int k = 0; k < m; k++)
                        cin >> map[i][j][k];
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)  //把不符题意的路全换成'*'; 
                {
                    if(map[0][i][j] == '#' && map[1][i][j] == '#') //上下两层相同位置处都为传送门;  
                    {
                        map[0][i][j] = '*';
                        map[1][i][j] = '*';
                    }
                    if(map[0][i][j] == '#' && map[1][i][j] == '*') //上下两层相同位置处一层为传送门, 一层为墙; 
                    {
                        map[0][i][j] = '*';
                    }
                    if(map[0][i][j] == '*' && map[1][i][j] == '#')//同上; 
                    {
                        map[1][i][j] = '#';
                    }
                }
            if(Bfs(0, 0, 0))
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;    
    } 
  • 相关阅读:
    设计模式-1-概要(c#版)
    UML图示说明
    阿里云SLB双机IIS多站点负载均衡部署笔记
    阿里云分布式关系数据库DRDS笔记
    一些小经验
    NOSQL场景梳理
    内核linux-3.4.2支持dm9000
    构建根文件系统
    u-boot-1.1.6移植之dm9000
    移植u-boot-1.1.6(原创)
  • 原文地址:https://www.cnblogs.com/soTired/p/4761122.html
Copyright © 2011-2022 走看看