1. 程式人生 > >A計劃 hdu2102(BFS)

A計劃 hdu2102(BFS)

unsigned 迷宮 就會 左右移動 closed 天下 clas 計劃 sign

A計劃 hdu2102

可憐的公主在一次次被魔王擄走一次次被騎士們救回來之後,而今,不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在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,就是可能會跨層,當跨層的時候可通過異或值來跨層,在跨層的時候如果是墻就over了,如果跨層過去還是跨層那麽就一直死循環跨層也over了

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include
<cmath> #include<cstdlib> #include<queue> using namespace std; #define ll long long #define llu unsigned long long #define INF 0x3f3f3f3f #define PI acos(-1.0) const int maxn = 1e5 + 10; char map[2][12][12]; int vis[2][12][12]; int dx[] = { 1,0,-1,0 }; int dy[] = { 0,1,0,-1 }; int N, M, T;
struct node { int ce; int x; int y; int step; }; int flag; void bfs() { memset(vis, 0, sizeof vis); queue<node>que; node now; now.ce = 0; now.x = now.y = 0; now.step = 0; que.push(now); vis[0][0][0]=1; flag = 0; while (!que.empty()) { now = que.front(); if (map[now.ce][now.x][now.y] == P && now.step <= T) { flag = 1; return; } if (map[now.ce][now.x][now.y] == P) return; for (int i = 0; i < 4; i++) { node next; next.x = now.x + dx[i]; next.y = now.y + dy[i]; next.ce = now.ce; int xx = next.x; int yy = next.y; int cece = next.ce; if (xx<0 || xx>=N) continue; if (yy<0 || yy>=M) continue; if (map[cece][xx][yy] == *) continue; if (map[cece][xx][yy] == # && map[cece ^ 1][xx][yy] == *) continue; if (map[cece][xx][yy] == # && map[cece ^ 1][xx][yy] == #) continue; if (!vis[cece][xx][yy] && map[cece][xx][yy] == # && vis[cece ^ 1][xx][yy]==0) { vis[cece][xx][yy] = 1; next.ce ^= 1; vis[next.ce][next.x][next.y] = 1; next.step = now.step + 1; que.push(next); } if (!vis[cece][xx][yy] && map[cece][xx][yy] != #) { vis[cece][xx][yy] = 1; next.step = now.step + 1; que.push(next); } } que.pop(); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; scanf("%d", &t); while (t--) { char ch; scanf("%d%d%d", &N, &M, &T); for (int i = 0; i < 2; i++) { for (int j = 0; j < N; j++) scanf("%s", map[i][j]); getchar(); } bfs(); if (flag) puts("YES"); else puts("NO"); } }
View Code

A計劃 hdu2102(BFS)