zl程序教程

您现在的位置是:首页 >  其他

当前栏目

hdu2102 水搜索

2023-09-11 14:14:00 时间
题意:

                                   A计划

Problem 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 Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.


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


思路:

      直接搜索就行了,一开始随意写了个,然后直接A了,A了之后突然感觉有问题,应该用优先队列,因为穿越的那个地方会让所有点不同步,然后写了个优先队列的,还是A了,后来自己想了想,应该是不用考虑优先队列,因为只有两层,自己模拟几组数据就明白了,但是在比赛的时候如果不确定,就最好写个优先队列,保险。


#include<stdio.h>
#include<string.h>
#include<queue>

#define N 15

using namespace std;

typedef struct  NODE
{
   int x ,y ,z ,t;
   friend bool operator < (NODE a ,NODE b)
   {
      return a.t > b.t;   
   }
}NODE;

NODE xin ,tou;
int mark[N][N][3];
int map[N][N][3];
int ex ,ey ,ez ,n ,m;
int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};

bool ok(int x, int y ,int z)
{
   if(x >= 1 && x <= n && y >= 1 && y <= m && !mark[x][y][z] && map[x][y][z])
   return 1;
   return 0;
}

int BFS(int sx ,int sy ,int sz ,int xzt)
{
    xin.x = sx ,xin.y = sy ,xin.z = sz ,xin.t = 0;
    memset(mark ,0 ,sizeof(mark));
    mark[xin.x][xin.y][xin.z] = 1;
    priority_queue<NODE>q;
    q.push(xin);
    while(!q.empty())
    {
         tou = q.top();
         q.pop();
         if(tou.x == ex && tou.y == ey && tou.z == ez)
         {
            if(tou.t <= xzt) return 1;
            return 0;
         }
         if(map[tou.x][tou.y][tou.z] == 2)
         {
            if(map[tou.x][tou.y][tou.z^1] != 1 || mark[tou.x][tou.y][tou.z^1]) continue;
            xin = tou;
            xin.z ^= 1;
            q.push(xin);
            mark[tou.x][tou.y][tou.z^1] = 1;
            continue;
         }
         
         for(int i = 0 ;i < 4 ;i ++)
         {
            xin.x = tou.x + dir[i][0];
            xin.y = tou.y + dir[i][1];
            xin.z = tou.z;
            xin.t = tou.t + 1;
            if(ok(xin.x ,xin.y ,xin.z))
            {
               mark[xin.x][xin.y][xin.z] = 1;
               q.push(xin);
            }
         }
      }
      return 0;
}

int main ()
{
    int c ,t ,i ,j ,sx ,sy ,sz;
    char str[N];
    scanf("%d" ,&c);
    while(c--)
    {
         scanf("%d %d %d" ,&n ,&m ,&t);
         for(i = 1 ;i <= n ;i ++)
         {
            scanf("%s" ,str);
            for(j = 0 ;j < m ;j ++)
            {
               if(str[j] == 'S')
               {
                  sx = i ,sy = j +1 ,sz = 0;
                  map[i][j + 1][0] = 1;
               }
               if(str[j] == 'P')
               {
                  ex = i ,ey = j + 1 ,ez = 0;
                  map[i][j+1][0] = 1;
               }
               if(str[j] == '.') map[i][j+1][0] = 1;
               if(str[j] == '#') map[i][j+1][0] = 2;
               if(str[j] == '*') map[i][j+1][0] = 0;
            }
         }
         
         for(i = 1 ;i <= n ;i ++)
         {
            scanf("%s" ,str);
            for(j = 0 ;j < m ;j ++)
            {
               if(str[j] == 'S')
               {
                  sx = i ,sy = j +1 ,sz = 1;
                  map[i][j + 1][1] = 1;
               }
               if(str[j] == 'P')
               {
                  ex = i ,ey = j + 1 ,ez = 1;
                  map[i][j+1][1] = 1;
               }
               if(str[j] == '.') map[i][j+1][1] = 1;
               if(str[j] == '#') map[i][j+1][1] = 2;
               if(str[j] == '*') map[i][j+1][1] = 0;
            }
         }
         
         if(BFS(sx ,sy ,sz ,t)) puts("YES");
         else puts("NO");
   }
   return 0;
}