POJ2251
用BFS就是增加一个维度,变成3维
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; int L, R, C; const int MAX_SIZE = 30; int g[MAX_SIZE][MAX_SIZE][MAX_SIZE]; int vis[MAX_SIZE][MAX_SIZE][MAX_SIZE]; int star[3]; int endd[3]; int mov[6][3] = {{0,0,-1},{0,0,1},{0,-1,0},{0,1,0},{-1,0,0},{1,0,0}}; //以后方向数组都这样写 struct node { int x,y,z,step; }; int check(int x,int y,int z) { if(x<0 || y<0 || z<0 || x>=L || y >= R || z >= C) return 1; if(g[x][y][z] == 1) return 1; if(vis[x][y][z] == 1) return 1; return 0; } int BFS() { node tmp, next; tmp.x = star[0]; tmp.y = star[1]; tmp.z = star[2]; tmp.step = 0; vis[tmp.x][tmp.y][tmp.z] = 1; queue<node> que; que.push(tmp); vis[tmp.x][tmp.y][tmp.z] = 1; while(!que.empty()) { tmp = que.front(); que.pop(); if(g[tmp.x][tmp.y][tmp.z] == 2) return tmp.step; for(int i = 0; i < 6; ++i) { next.x = tmp.x + mov[i][0]; next.y = tmp.y + mov[i][1]; next.z = tmp.z + mov[i][2]; if (check(next.x, next.y, next.z)) continue; next.step = tmp.step + 1; vis[next.x][next.y][next.z] = 1; que.push(next); } } return 0; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); char c; while(cin >> L >> R >> C && L && R && C) { if(L == R && R == C && C == 0) break; memset(g, 0, sizeof(g)); memset(vis, 0, sizeof(vis)); for(int i = 0; i < L; ++i) { for(int j = 0; j < R; ++j) { for(int k = 0; k < C; ++k) { cin >> c; //putchar(c); if(c == 'S') { g[i][j][k] = -1; star[0] = i; star[1] = j; star[2] = k; } else if(c == '.') g[i][j][k] = 0; else if(c == '#') g[i][j][k] = 1; else if(c == 'E') { g[i][j][k] = 2; endd[0] = i; endd[1] = j; endd[2] = k; } } } if(i < L-1) getchar(); } // // for(int i = 0; i < L; ++i) // { // for(int j = 0; j < R; ++j) // { // for(int k = 0; k < C; ++k) // { // printf("%d ", g[i][j][k]); // } // puts(""); // } // puts(" "); // } // int rrr = BFS(); if(rrr == 0) printf("Trapped! "); else printf("Escaped in %d minute(s). ", rrr); } return 0; }
这道题用DFS写法超时,不过,在练DFS思路,就用DFS写了。
思路:将图保存下来之后,从起始点开始进行DFS,如果遇到终点就停止并返回。
我的DFS是,现在本层上进行DFS(二维),如果在本层不能找到,就从这个点调到下一维的这个点上,继续进行DFS。每次从当前点调到相邻的下一层。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int L, R, C; const int MAX_SIZE = 30; int g[MAX_SIZE][MAX_SIZE][MAX_SIZE]; int vis[MAX_SIZE][MAX_SIZE][MAX_SIZE]; int mov_row[4] = {-1,0,0,1}; int mov_col[4] = {0,-1,1,0}; int star[] = {0,0,0}; int endd[] = {0,0,0}; int ans; int flag = 0; void DFS(int level, int row, int col) { if(level == endd[0] && row == endd[1] && col == endd[2]) { printf("Escaped in %d minute(s). ", ans); flag = 1; return; } if(level == L) return; for(int i = 0; i < 4; ++i) { int x = row+mov_row[i]; int y = col+mov_col[i]; if(x >= 0 && x < R && y >= 0 && y < C) { if( vis[level][x][y] == 0 && g[level][x][y] == 0 && g[level][x][y] != 2 && flag == 0) { vis[level][x][y] = 1; ans += 1; DFS(level, x, y); ans -= 1; vis[level][x][y] = 0; } } } if(flag == 0) { vis[level+1][row][col] = 1; ans += 1; DFS(level+1, row, col); ans -= 1; vis[level+1][row][col] = 0; } } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); char c; while(cin >> L >> R >> C && L && R && C) { if(L == R && R == C && C == 0) break; ans = 0; flag = 0; memset(g, 0, sizeof(g)); memset(vis, 0, sizeof(vis)); for(int i = 0; i < L; ++i) { for(int j = 0; j < R; ++j) { for(int k = 0; k < C; ++k) { cin >> c; //putchar(c); if(c == 'S') { g[i][j][k] = -1; star[0] = i; star[1] = j; star[2] = k; } else if(c == '.') g[i][j][k] = 0; else if(c == '#') g[i][j][k] = 1; else if(c == 'E') { g[i][j][k] = 2; endd[0] = i; endd[1] = j; endd[2] = k; } } } if(i < L-1) getchar(); } // for(int i = 0; i < L; ++i) // { // for(int j = 0; j < R; ++j) // { // for(int k = 0; k < C; ++k) // { // printf("%d ", g[i][j][k]); // } // puts(""); // } // puts(" "); // } vis[star[0]][star[1]][star[2]] = 1; DFS(star[0],star[1],star[2]); //cout << flag << endl; if(flag == 0) printf("Trapped! "); } return 0; }