K - UZI HDU - 2102 A计划 搜索

K - UZI HDU - 2102 

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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

很明显是搜索,小心一点就好了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>

typedef long long LL;
typedef long double LD;
using namespace std;
const int  maxn=22;
char ma[maxn][maxn];
int vis[maxn][maxn];
//int ff[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int f[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//int ff[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
//int f[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};
int N=4,M=4,T;
struct node
{
    int x,y;
    int step;
    void setnode(int xx,int yy,int ss)
    {
        x=xx;
        y=yy;
        step=ss;
    }
    friend bool operator <(node a,node b)
    {
        return a.step>b.step;
    }
};
priority_queue<node>q;
bool OK(int x,int y)//过界  墙
{
    if(x>=0&&y>=0&&x<2*N&&y<M&&ma[x][y]!='*')
        return true;
    return false;
}
void bfs(node st)
{
    q.push(st);
    vis[st.x][st.y]=1;
    while(!q.empty())
    {
        node t=q.top();
        //printf("*****%d %d\n",t.status,t.step);
        q.pop();
        if(ma[t.x][t.y]=='P')
        {
            printf("YES\n");
            return;
        }
        if(t.step+1>T)continue;
        for(int i=0;i<4;i++)
        {
            int xx=f[i][0]+t.x;
            int yy=t.y+f[i][1];
            if(!OK(xx,yy)||(xx<N&&t.x>=N)||(xx>=N&&t.x<N)||vis[xx][yy])continue;
            if(ma[xx][yy]=='#')
            {
                vis[xx][yy]=1;
                xx+=N;
                if(xx>=2*N)
                    xx%=N;
                if(!OK(xx,yy)||ma[xx][yy]=='#'||vis[xx][yy])continue;
                vis[xx][yy]=1;
                q.push((node){xx,yy,t.step+1});
            }
            else
            {
                vis[xx][yy]=1;
                q.push((node){xx,yy,t.step+1});
            }
        }
    }
    printf("NO\n");
}
int main()
{
    int C;
    scanf("%d",&C);
    while(C--)
    {
        scanf("%d%d%d",&N,&M,&T);
        for(int i=0;i<2*N;i++)
        {
            scanf("%s",ma[i]);
        }
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        bfs((node){0,0,0});
    }
    return 0;
}

 

posted on 2018-08-02 17:40  一零七  阅读(69)  评论(0编辑  收藏  举报

导航