题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2102
A计划
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
双层bfs。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::map; 15 using std::pair; 16 using std::queue; 17 using std::vector; 18 using std::multimap; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 12; 29 typedef unsigned long long ull; 30 bool vis[2][N][N]; 31 char G[2][N][N]; 32 int H, W, T; 33 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 }; 34 struct Node { 35 int c, x, y, s; 36 Node(int i = 0, int j = 0, int k = 0, int l = 0) :c(i), x(j), y(k), s(l) {} 37 }; 38 bool bfs() { 39 cls(vis, false); 40 queue<Node> q; 41 q.push(Node()); 42 vis[0][0][0] = true; 43 while (!q.empty()) { 44 Node t = q.front(); q.pop(); 45 if (G[t.c][t.x][t.y] == 'P') return t.s <= T; 46 rep(i, 4) { 47 int nx = t.x + dx[i], ny = t.y + dy[i]; 48 char &ch = G[t.c][nx][ny]; 49 if (nx < 0 || nx >= H || ny < 0 || ny >= W ) continue; 50 if (ch == '*' || vis[t.c][nx][ny]) continue; 51 if (ch == '#') { 52 if (G[t.c ^ 1][nx][ny] == '#' || G[t.c ^ 1][nx][ny] == '*') continue; 53 if (vis[t.c ^ 1][nx][ny]) continue; 54 q.push(Node(t.c ^ 1, nx, ny, t.s + 1)); 55 vis[t.c ^ 1][nx][ny] = vis[t.c][nx][ny] = true; 56 continue; 57 } 58 q.push(Node(t.c, nx, ny, t.s + 1)); 59 vis[t.c][nx][ny] = true; 60 } 61 } 62 return false; 63 } 64 inline void input(bool d) { 65 rep(i, H) scanf("%s", G[d][i]); 66 } 67 int main() { 68 #ifdef LOCAL 69 freopen("in.txt", "r", stdin); 70 freopen("out.txt", "w+", stdout); 71 #endif 72 int t; 73 scanf("%d", &t); 74 while (t--) { 75 scanf("%d %d %d", &H, &W, &T); 76 input(0), input(1); 77 puts(bfs() ? "YES" : "NO"); 78 } 79 return 0; 80 }