題目連結:
here
分析:
遇到上下兩層都是# 的,就把上下兩層的這個位置都弄成 牆就行。。還有遇到 一層是#一層是牆的。。也直接把倆都弄城牆就行。。。省的要判斷他撞死。。哈哈、、我遇到了點小問題。。。就是遇到#的時候,我忘了加步數。。。所以一直wa。。最後才檢查出來。。。囧。。。。。低級失誤。。。雖然上下樓不用時間。。但是你走到#需要一步的時間。。。
代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define inf (0x7f7f7f7f)const int maxn = 12;int c, n, m, t;char map[2][maxn][maxn];bool vis[2][maxn][maxn];struct node{int x, y, z;int step;};queue<node> q;int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};bool judge(int x, int y){if (x < 0 || y < 0 || x >= n || y >= m) return false;return true;}bool bfs(){bool flag = false;while (!q.empty()){node now = q.front();q.pop();if (map[now.z][now.x][now.y] == 'P'){if (now.step >= 0)return true;elsebreak;}node next;for (int i=0; i<4; i++){next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];next.z = now.z;next.step = now.step - 1;if (!judge(next.x,next.y)) continue;if (map[now.z][next.x][next.y] == '#')next.z ^= 1;if (map[next.z][next.x][next.y] != '*' && next.step >= 0 && !vis[next.z][next.x][next.y]){vis[next.z][next.x][next.y] = true;q.push(next);}}}return false;}int main(){//freopen("2102.in", "r", stdin);//freopen("2102.out", "w", stdout);scanf("%d", &c);while (c--){scanf("%d %d %d", &n, &m, &t);memset(vis, false, sizeof(vis));while (!q.empty()) q.pop();int i, j, k;for (i=0; i<2; i++)for (j=0; j<n; j++)scanf("%s", map[i][j]);node s;for (i=0; i<2; i++)for (j=0; j<n; j++)for (k=0; k<m; k++){if (map[i][j][k] == 'S')s.x = j, s.y = k, s.z = i, s.step = t;else if (map[i][j][k] == '#' && map[1^i][j][k] == '#'){map[i][j][k] = '*';map[1^i][j][k] = '*';}else if (map[i][j][k] == '#' && map[1^i][j][k] == '*')map[i][j][k] = '*';}vis[s.z][s.x][s.y] = true;q.push(s);if (bfs())puts("YES");elseputs("NO");}return 0;}
測試資料:
335 5 14S*#*..#........****....#...*.P#.*..***.....*.*.#..5 5 13S*#*..#........****....#...*.P#.*..***.....*.*.#..5 5 12S*#*..#........****....#...*.P#.*..***.....*.*.#..1 2 100SP..1 3 100S#P.#.3 3 11S..*#.#......#..*P5 5 11S*#*..#........****.P.*#...*..#.*..***.....*.*#.#.5 5 12S*#*..#........****.P.*#...*..#.*..***.....*.*#.#.5 5 12S*#*..#........****.P.*#...*..#.*..***.....*.*.##.5 5 3S*#*..#........****...*#..P*..#.*..***.....*.*#.#.5 5 9S*#*..#........****...*#...*..#.*..***.....*.*#.P.4 5 7S*#*....**.#.******..*.*..#*..#.#...#.*P5 7 12S*****P.#.*#*.**.#.*..*****........********.#*...***.#*#**************3 3 3S#P.*.....##......
輸出資料:
YESNONOYESNONONONONOYESYESNOYESNONONONONONONONONONONONONONONONONONONONO
^o^