HDU 2102 A计划


可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。 
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES


题解:一道用bfs求最短路径的问题,一开始做忽略两个细节,

1、进入‘#’后,不但遇到‘*’是走不了的,遇到‘#’同样走不了

2、进入‘#’后,有可能直接就碰到P,即next.f == pf && next.x == px && next.y == py

要特别注意,bfs中返回值0或者1 的时刻


AC代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <queue>
using namespace std;


struct node
{
int f,x,y,step;
};
char map[2][15][15];
int vis[2][15][15];
int mov[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int c,n,m,t;
int pf , px , py ;


int check(int a,int b,int c) //检查是否越界
{
    if( b<0 || c<0 || b>=m || c>=n)
        return 1;
    else if(map[a][b][c] == '*')
        return 1;
    return 0;
}


int bfs()
{
    node a,next;
    queue<node> Q;
    a.f = 0 ; a.x = 0 ; a.y = 0 ; a.step = 0 ;
    vis[0][0][0] = 1 ;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();


        if( a.f==pf && a.x==px && a.y==py ){
            return 1 ;
        }
        if(a.step >= t) break ;
        for(int i=0 ; i<4 ; i++)
        {
            next = a ;
            next.x = a.x + mov[i][0];
            next.y = a.y + mov[i][1];
            next.step = a.step + 1 ;
            if(check(next.f,next.x,next.y)) continue ;//非法,进入下一循环
            if(vis[next.f][next.x][next.y]) continue;
            vis[next.f][next.x][next.y] = 1 ;
            if(map[next.f][next.x][next.y]=='#')
            {
                next.f = !next.f ;
                if(check(next.f,next.x,next.y))  continue;
                if(vis[next.f][next.x][next.y]) continue;
                if(map[next.f][next.x][next.y]=='*'||map[next.f][next.x][next.y]=='#')
                    continue ;
                vis[next.f][next.x][next.y] = 1 ;
            }
            if(next.f == pf && next.x == px && next.y == py)
            {
                return 1;
            }
            Q.push(next);
        }
    }
    return 0;
}


int main(void)
{
    cin >> c;
    while(c--)
    {
        cin >> m >> n >> t ;
        for(int i=0 ; i<2 ; i++)
        {
            for(int j=0 ; j<m ; j++)
            for(int k=0 ; k<n ; k++)
                cin >> map[i][j][k];
            getchar();
        }
        for(int i=0 ; i<2 ; i++)
        for(int j=0 ; j<m ; j++)
        for(int k=0 ; k<n ; k++)
            if(map[i][j][k]=='P')  {
                pf = i ; px = j ; py = k ;
            }
        memset(vis,0,sizeof(vis));


        int ans =  bfs();
        if(ans) cout << "YES" << endl;
        else    cout << "NO" << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值