码迷,mamicode.com
首页 > 其他好文 > 详细

HDU_2102 A计划 【BFS】

时间:2018-12-10 22:38:41      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:har   while   splay   als   color   open   NPU   分享图片   visit   

一、题目

HDU2102

 二、题意分析

该题其实就是三位空间内的BFS,但比较简单的是,它设置了传送门,可以直接传送上去,需要注意的是

1.到了传送门的时候要重新考虑传送的点的三种情况。

  (1)若又是传送门,这两个点都可以不再考虑了。

  (2)若是墙,如题意,直接pass掉。

  (3)若是P,找到了。

2.在T之前找到公主后也是可以的合理解释是不是骑士和公主可以在那里聊到T时刻?

 三、AC代码

技术分享图片
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <fstream>
  5 #include <queue>
  6 
  7 using namespace std;
  8 
  9 int N, M, T;
 10 const int dx[] = {0, 0, 1, -1};
 11 const int dy[] = {1, -1, 0, 0};
 12 char Map[3][15][15];
 13 bool visit[3][15][15];
 14 struct Node
 15 {
 16     int level, x, y;
 17 }S, P;
 18 // Node Q[1000000];
 19 // int Rea, Cnt;
 20 bool judge(Node t)
 21 {
 22     if(t.level < 0 || t.level >= 2 || t.x < 0 || t.x >= N || t.y < 0 || t.y >= M 
 23     || Map[t.level][t.x][t.y] == * || visit[t.level][t.x][t.y] == 1)
 24         return false;
 25     return true;
 26 }
 27 
 28 bool BFS()
 29 {
 30     memset(visit, 0, sizeof(visit));
 31     queue<Node> Q;
 32     Q.push(S);
 33 
 34     // Cnt = Rea = 0;
 35     // Q[Cnt++] = S;
 36     visit[S.level][S.x][S.y] = 1;
 37 
 38     for(int t = 1; t <= T; t++)
 39     {
 40         int size = (int)Q.size();
 41         //int size = Cnt - Rea;
 42         while(size--)
 43         {
 44             Node cur = Q.front();
 45             Q.pop();
 46             //Node cur = Q[Rea++];
 47 
 48             for(int i = 0; i < 4; i++)
 49             {
 50                 Node next = cur;
 51                 next.x += dx[i];
 52                 next.y += dy[i];
 53                 if(judge(next))
 54                 {
 55                     if(Map[next.level][next.x][next.y] == #)
 56                     {
 57                         visit[next.level][next.x][next.y] = 1;
 58                         next.level++;
 59                         next.level%=2;
 60                         if(!judge(next))
 61                             continue;
 62                     }
 63                     if(Map[next.level][next.x][next.y] == #)
 64                     {
 65                         visit[next.level][next.x][next.y] = 1;
 66                         continue;
 67                     }
 68                     else if(Map[next.level][next.x][next.y] == P)
 69                     {
 70                         return true;
 71                     }
 72                     else
 73                     {
 74                         visit[next.level][next.x][next.y] = 1;
 75                         Q.push(next);
 76                     }
 77                     //Q[Cnt++] = next;
 78                 }
 79             }
 80         }
 81     }
 82     return false;
 83 
 84 }
 85 
 86 int main()
 87 {
 88     //freopen("input.txt", "r", stdin);
 89     int C;
 90     scanf("%d", &C);
 91     while(C--)
 92     {
 93         scanf("%d %d %d", &N, &M, &T);
 94         for(int i = 0; i < 2; i++)
 95         {
 96             for(int j = 0; j < N; j++)
 97             {
 98                 scanf("%s", Map[i][j]);
 99                 for(int k = 0; k < M; k++)
100                 {
101                     if(Map[i][j][k] == S)
102                     {
103                         S.level = i;
104                         S.x = j;
105                         S.y = k;
106                     }
107                     else if(Map[i][j][k] == P)
108                     {
109                         P.level = i;
110                         P.x = j;
111                         P.y = k;
112                     }
113                 }
114             }
115             getchar();
116         }
117         if(BFS())
118             printf("YES\n");
119         else
120             printf("NO\n");
121     }
122     return 0;
123 }
View Code

 

HDU_2102 A计划 【BFS】

标签:har   while   splay   als   color   open   NPU   分享图片   visit   

原文地址:https://www.cnblogs.com/dybala21/p/10099560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!