http://bailian.openjudge.cn/practice/2251/
思路
“3D”的BFS,其实就是多个两个搜索的方向。(上下前后左右)
#include <iostream>
#include <queue>
using namespace std;
int L, R, C, Find, FindStep;
int startx, starty, startz, endx, endy, endz;
char board[33][33][33];
bool vis[33][33][33];
struct node
{
int x, y, z;
int step;
node(){}
node(int xx, int yy, int zz, int s)
{
x = xx;
y = yy;
z = zz;
step = s;
}
};
int main(int argc, char const *argv[])
{
//freopen("in.txt", "r", stdin);
while(cin >> L >> R >> C)
{
Find = 0;
if(L==0 && R==0 && C==0)
break;
for(int i=0;i<L;i++)
{
for(int j=0;j<R;j++)
{
for(int k=0;k<C;k++)
{
cin>>board[i][j][k];
vis[i][j][k] = false;
if(board[i][j][k] == 'S')
{
startz = i;
startx = j;
starty = k;
}
if(board[i][j][k] == 'E')
{
endz = i;
endx = j;
endy = k;
board[i][j][k] = '.';
}
}
}
}
queue<node> q;
q.push(node(startx, starty, startz, 0));
while(!q.empty())
{
node first = q.front(); // 取出队首
q.pop();
int x = first.x, y = first.y, z = first.z, step = first.step;
// cout<<z<<" <<< "<<x<< " <<< "<<y<<" <<< "<<board[z][x][y]<<endl;
if(z==endz && x==endx && y==endy)
{
Find = 1;
FindStep = first.step;
break;
}
if(z>0 && vis[z-1][x][y]==false && board[z-1][x][y]=='.')
{
vis[z-1][x][y] = true;
q.push(node(x, y, z-1, step+1));
}
if(z<L-1 && vis[z+1][x][y]==false && board[z+1][x][y]=='.')
{
vis[z+1][x][y] = true;
q.push(node(x, y, z+1, step+1));
}
if(x>0 && vis[z][x-1][y]==false && board[z][x-1][y]=='.')
{
vis[z][x-1][y] = true;
q.push(node(x-1, y, z, step+1));
}
if(y>0 && vis[z][x][y-1]==false && board[z][x][y-1]=='.')
{
vis[z][x][y-1] = true;
q.push(node(x, y-1, z, step+1));
}
if(y<C-1 && vis[z][x][y+1]==false && board[z][x][y+1]=='.')
{
vis[z][x][y+1] = true;
q.push(node(x, y+1, z, step+1));
}
if(x<R-1 && vis[z][x+1][y]==false && board[z][x+1][y]=='.')
{
vis[z][x+1][y] = true;
q.push(node(x+1, y, z, step+1));
}
}
if(Find)
printf("Escaped in %d minute(s).\n", FindStep);
else
printf("Trapped!\n");
}
return 0;
}