HDU-2102 A计划
2017-02-26 15:32
246 查看
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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解决,可以想象成一个两层的迷宫,遇到传送阵就是h这个坐标的变化,不过本题有个大坑,就是存在上下两个都是传送阵的情况,这样的话,就需要单独判断出来(被这里卡了很长时间,看了题解才明白这个,真是惭愧)
代码如下:
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是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解决,可以想象成一个两层的迷宫,遇到传送阵就是h这个坐标的变化,不过本题有个大坑,就是存在上下两个都是传送阵的情况,这样的话,就需要单独判断出来(被这里卡了很长时间,看了题解才明白这个,真是惭愧)
代码如下:
#include<stdio.h> #include<string.h> #include<math.h> #define MAX 15 char map[3][MAX][MAX];//用来储存这张地图 bool book[3][MAX][MAX];//用来标记这个坐标是否走过 int n,m,T;//T 代表最大时限 const int xx[4] = {-1,1,0,0};//四个方向的变化 const int yy[4] = {0,0,-1,1}; struct coord{ int h,x,y; int s;//代表走到这个坐标的步数 }s[MAX*MAX],begin,next; int bfs(void) { int i,j,k; int head = 1,tail = 1; s[tail] = begin;//把起点坐标放入队列 tail++; while(head < tail) { if(s[head].s > T)//如果这个根节点已经大过最高时限,则返回0 return 0; for(i=0;i<4;i++) { next.h = s[head].h; next.x = s[head].x + xx[i]; next.y = s[head].y + yy[i]; next.s = s[head].s + 1; if(next.x < 1||next.y < 1|| next.x > n||next.y > m)//边界的判断 continue; if(book[next.h][next.x][next.y] == 0 && map[next.h][next.x][next.y] == '#') { book[next.h][next.x][next.y] = 1; if(next.h == 1) next.h = 2; else if(next.h == 2) next.h = 1; if(map[next.h][next.x][next.y] == '*') continue; else if(map[next.h][next.x][next.y] == '#')//如果上下两个都是传送阵的情况,把这个传送阵也标记上 { book[next.h][next.x][next.y] = 1; continue; } else if(map[next.h][next.x][next.y] == '.') { s[tail] = next; tail++; book[next.h][next.x][next.y] = 1; } } /*这一步判断不能和上一步判断传送阵在一个book里,因为当传送阵移动后,坐标会发生变化*/ if(book[next.h][next.x][next.y] == 0 && map[next.h][next.x][next.y] == '.') { s[tail] = next; tail++; book[next.h][next.x][next.y] = 1; } if(map[next.h][next.x][next.y] == 'P')//找到终点时 { return next.s; } } head++;//队首前进,不要忘记 } return 0; } int main(void) { int t,i,j,k,ans; scanf("%d",&t); while(t--) { memset(book,0,sizeof(book)); scanf("%d %d %d",&n,&m,&T); for(i=1;i<=2;i++) { for(j=1;j<=n;j++) { for(k=1;k<=m;k++) { scanf(" %c",&map[i][j][k]); if(map[i][j][k] == 'S') { begin.h = i; begin.x = j; begin.y = k; begin.s = 0; book[i][j][k] = 1; } } } } ans = bfs(); if(ans <= T && ans > 0)//判断所需步数是否在这个范围 printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 2102 A计划
- HDU 2102A计划
- HDU 2102 A计划 (bfs + 判重)
- hdu 2102 A计划
- HDU 2102 A计划(三维BFS)
- HDU - 2102 A计划
- H - A计划 HDU - 2102----普通BFS注意坑点
- HDU 2102 A计划(简单3维BFS)
- HDU 2102 A计划DFS
- HDU - 2102 A计划(dfs)
- HDU 2102 A计划 (BFS或DFS)
- HDU 2102 A计划(宽搜)
- hdu 2102 A计划(优先队列+dfs)
- hdu 2102 A计划
- hdu 2102 A计划 BFS
- HDU 2102 A计划(BFS)
- HDU - 2102 A计划 (BFS) [kuangbin带你飞]专题二
- HDU 2102 A计划 BFS
- HDU_2102_ A计划
- HDU-2102 A计划