[bfs] HDU2102 A计划

来源:互联网 发布:特种设备考试软件 编辑:程序博客网 时间:2024/05/16 00:49

I - A计划
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

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

普通的bfs,或者dfs也可以做,注意地图在两层之间传送时是不需要时间的。第一次做时就是因为这个地方被坑了。。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int MAXN = 15;char G[2][MAXN][MAXN];int vis[2][MAXN][MAXN];int C,N,M,T,ans,i,j,k;struct node{    int z,x,y,step;};int dir[6][3] ={    {1,0,0},    {-1,0,0},    {0,1,0},    {0,-1,0},    {0,0,1},    {0,0,-1}};queue<node> Q;bool check(int z,int x,int y){    if (vis[z][x][y]) return 1;    if (z>=2 || z<0) return 1;    if (x>=N || x<0) return 1;    if (y>=M || y<0) return 1;    if (G[z][x][y] =='*') return 1;    return 0;}int main(){ //   freopen("in.txt","r",stdin);    scanf("%d",&C);    while(C--)    {        while(!Q.empty()) Q.pop();        memset(vis,0,sizeof vis);        memset(G,0,sizeof G);        int flag = 0;        scanf("%d%d%d",&N,&M,&T);        for(int m = 0; m<2; m++)            for(int i =0; i<N; i++)                scanf("%s",G[m][i]);        node a;        a.z = 0,a.x = 0,a.y=0,a.step=0;        vis[a.z][a.x][a.y] = 1;        Q.push(a);        while(!Q.empty())        {            node a,next;            a = Q.front();            Q.pop();         //   printf("z:%d x:%d y:%d step:%d %c\n",a.z,a.x,a.y,a.step,G[a.z][a.x][a.y]);            if (G[a.z][a.x][a.y] =='P')            {                flag = (a.step<=T);                break;            }            if (G[a.z][a.x][a.y] =='#')            {                for (int i = 0; i<2; i++)                {                    next = a;                    next.z = a.z + dir[i][0];                    next.x = a.x + dir[i][1];                    next.y = a.y + dir[i][2];                    if (check(next.z,next.x,next.y))                        continue;                    vis[next.z][next.x][next.y] = 1;                    next.step = a.step;                    Q.push(next);                }            }            else            {                for(int i = 2; i<6; i++)                {                    next = a;                    next.z = a.z + dir[i][0];                    next.x = a.x + dir[i][1];                    next.y = a.y + dir[i][2];                    if(check(next.z,next.x,next.y))                        continue;                    vis[next.z][next.x][next.y] = 1;                    next.step = a.step + 1;                    Q.push(next);                }            }        }        if (flag) printf("YES\n");        else printf("NO\n");    }    return 0;}
0 0
原创粉丝点击