杭电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\n");
        else
            printf("NO\n");
    }
    return 0;    
} 

 

转载于:https://www.cnblogs.com/soTired/p/4761122.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值