看了网上的POJ题目分类,把它分在DFS中,其实这题是BFS,网上分类也不是很准确啊!DFS 肯定会TLE,
DFS代码:
#include<cstdio>
#include<cstring>
#define MAXN 40
char Maze[MAXN][MAXN][MAXN];
int L, R, C;
int s_l, s_r, s_c;
int e_l, e_r, e_c;
int vis[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 = 0xffff;
void dfs(int l, int r, int c, int step)
{
// printf("DFS: %d %d %d\n", l, r, c);
if(l == e_l && r == e_r && c == e_c )
{
if (min > step)
min = step;
return;
}
for(int i =0; i < 6; i++)
{
int t_l = l + dir[i][0];
int t_r = r + dir[i][1];
int t_c = c + dir[i][2];
if(t_l < 0|| t_l >= L || t_r < 0 || t_r >= R ||t_c < 0 || t_c >= C || Maze[t_l][t_r][t_c] == '#' || vis[t_l][t_r][t_c])
continue;
vis[t_l][t_r][t_c] = 1;
dfs(t_l, t_r, t_c, step + 1);
vis[t_l][t_r][t_c] = 0;
}
}
void info()
{
printf("E point: %d %d %d\n", e_l, e_r, e_c);
for(int i = 0; i<L; i++)
for(int j= 0; j<R; j++)
printf("Maze: %s\n", Maze[i][j]);
}
int main()
{
scanf("%d", &L);
scanf("%d", &R);
scanf("%d", &C);
while(L || R || C)
{
memset(Maze, 0, sizeof(Maze));
min = 0xffff;
for(int i = 0; i < L; i++)
{
for(int j = 0; j < R; j++)
{
scanf("%s", Maze[i][j]);
for(int k = 0; k < C; k++)
{
if(Maze[i][j][k] == 'S')
{
s_l = i;
s_r = j;
s_c = k;
}
else if(Maze[i][j][k] == 'E')
{
e_l = i;
e_r = j;
e_c = k;
}
}
}
}
// info();
vis[s_l][s_r][s_c] = 1;
dfs(s_l, s_r, s_c, 0);
if(min < 0xffff)
printf("Escaped in %d minute(s).\n", min);
else
printf("Trapped!\n");
scanf("%d", &L);
scanf("%d", &R);
scanf("%d", &C);
}
return 0;
}
BFS代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define MAX 31
using namespace std;
int L, R, C;
char maze[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];
int s[3], e[3];
int Min;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0},
{0, 1, 0}, {0, -1, 0},
{0, 0, 1}, {0, 0, -1}};
struct Point
{
int x;
int y;
int z;
int step;
};
queue<Point> mq;
bool in_range(int x, int y, int z)
{
if(x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z< C)
return true;
else
return false;
}
void bfs(int x, int y, int z)
{
// printf("BFS: %d %d %d \n", x, y, z);
Point p;
p.x = x;
p.y = y;
p.z = z;
p.step = 0;
vis[x][y][z] = 1;
mq.push (p);
while(!mq.empty ())
{
Point q = mq.front();
mq.pop();
// printf("BFS mq: %d %d %d \n", q.x, q.y, q.z);
if(q.x == e[0] && q.y == e[1] && q.z == e[2])
{
Min = q.step;
return ;
}
for( int i = 0; i < 6; i++)
{
Point m;
m.x = q.x + dir[i][0];
m.y = q.y + dir[i][1];
m.z = q.z + dir[i][2];
m.step = q.step + 1;
// printf("M: %d %d %d \n", m.x, m.y, m.z);
if(in_range(m.x, m.y, m.z) && maze[m.x][m.y][m.z] != '#' && vis[m.x][m.y][m.z] == 0)
{
vis[m.x][m.y][m.z] = 1;
mq.push(m);
}
}
}
}
int main()
{
scanf("%d", &L);
scanf("%d", &R);
scanf("%d", &C);
while(L || R || C)
{
memset( maze, 0, sizeof(maze));
memset( vis, 0, sizeof(vis));
Min = 0;
while(!mq.empty())
{
mq.pop();
}
for(int i = 0; i < L; i++)
{
for(int j = 0; j < R; j++)
{
scanf("%s", maze[i][j]);
for(int k = 0; k < C; k ++)
{
if (maze[i][j][k] == 'S')
{
s[0] = i;
s[1] = j;
s[2] = k;
}
else if (maze[i][j][k] == 'E')
{
e[0] = i;
e[1] = j;
e[2] = k;
}
}
}
}
bfs(s[0], s[1], s[2]);
if(Min)
printf("Escaped in %d minute(s).\n", Min);
else
printf("Trapped!\n");
scanf("%d", &L);
scanf("%d", &R);
scanf("%d", &C);
}
return 0;
}