题目:http://poj.org/problem?id=2251
一个三维地牢,L为层数,R为每层的行数,C为每层的列数,S为起点,E为终点,
‘#’为障碍,‘.’表示可以通过,可以上下左右前后行走,每走一步耗时一分钟,
问能否从起点S走到终点E,若能,则输出最少花费的时间,若不能,则输出“Trapped!”。
BFS
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 32;
int L, R, C;
char Map[MAXN][MAXN][MAXN]; // L, R, C
int book[MAXN][MAXN][MAXN];
int dir[6][3] = {1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int min_steps = INF;
struct node
{
int z, x, y, step;
}s, e; // s为start,起点; e是end,终点
void read_map()
{
for(int i = 0; i < L; i++)
{
for(int r = 0; r < R; r++)
{
for(int c = 0; c < C; c++)
{
cin >> Map[i][r][c];
if(Map[i][r][c] == 'S') // 起点
{
s.z = i; s.x = r; s.y = c;
}
else if(Map[i][r][c] == 'E') // 终点
{
e.z = i; e.x = r; e.y = c;
}
}
}
}
}
void bfs()
{
queue<node> q;
q.push(s);
while(!q.empty())
{
node now = q.front(); q.pop();
if(now.z == e.z && now.x == e.x && now.y == e.y)
{
min_steps = min(min_steps, now.step);
return ;
}
for(int i = 0; i < 6; i++)
{
node nxt;
nxt.z = now.z + dir[i][0];
nxt.x = now.x + dir[i][1];
nxt.y = now.y + dir[i][2];
if(nxt.z >= 0 && nxt.z < L
&& nxt.x >= 0 && nxt.x < R
&& nxt.y >= 0 && nxt.y < C
&& Map[nxt.z][nxt.x][nxt.y] != '#')
{
if(!book[nxt.z][nxt.x][nxt.y])
{
nxt.step = now.step + 1;
book[nxt.z][nxt.x][nxt.y] = 1;
q.push(nxt);
}
}
}
}
}
int main()
{
while(~scanf("%d %d %d", &L, &R, &C) && (L + R + C))
{
min_steps = INF;
memset(Map, 0, sizeof(Map));
memset(book, 0, sizeof(book));
read_map();
s.step = 0;
book[s.z][s.x][s.y] = 1;
bfs();
if(min_steps == INF) cout << "Trapped!" << endl;
else printf("Escaped in %d minute(s).\n", min_steps);
}
return 0;
}
DFS超时
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 32;
int L, R, C;
char Map[MAXN][MAXN][MAXN]; // L, R, C
int book[MAXN][MAXN][MAXN];
int sL, sR, sC;
int eL, eR, eC;
int dir[6][3] = {1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int min_steps = INF;
void read_map()
{
for(int i = 0; i < L; i++)
{
for(int r = 0; r < R; r++)
{
for(int c = 0; c < C; c++)
{
cin >> Map[i][r][c];
if(Map[i][r][c] == 'S')
{
sL = i; sR = r; sC = c;
}
else if(Map[i][r][c] == 'E')
{
eL = i; eR = r; eC = c;
}
}
}
}
}
void dfs(int nz, int nx, int ny, int steps)
{
if(nz == eL && nx == eR && ny == eC)
{
if(steps < min_steps)
{
min_steps = steps;
return ;
}
}
for(int i = 0; i < 6; i++)
{
int next_z = nz + dir[i][0];
int next_x = nx + dir[i][1];
int next_y = ny + dir[i][2];
if(next_z >= 0 && next_z < L
&& next_x >= 0 && next_x <= R
&& next_y >= 0 && next_y <= C
&& Map[next_z][next_x][next_y] != '#')
{
if(!book[next_z][next_x][next_y])
{
book[next_z][next_x][next_y] = 1;
dfs(next_z, next_x,next_y, steps + 1);
book[next_z][next_x][next_y] = 0;
}
}
}
}
int main()
{
while(~scanf("%d %d %d", &L, &R, &C) && (L + R + C))
{
min_steps = INF;
memset(Map, 0, sizeof(Map));
memset(book, 0, sizeof(book));
read_map();
book[sL][sR][sC] = 1;
dfs(sL, sR, sC, 0);
if(min_steps == INF) cout << "Trapped!" << endl;
else printf("Escaped in %d minute(s).\n", min_steps);
}
return 0;
}