| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
广搜,无压力轻松过。最顺的一道题,欣赏一下宏伟的代码群#include <fstream>
#include <iostream>
#include <deque>
using namespace std;
#define INF 60000
typedef unsigned short DIS_T;
enum Color { white, grey, black };
struct id {
short x;
short y;
short z;
id(short a, short b, short c)
: x(a), y(b), z(c) { }
id() { }
};
int main()
{
//ifstream ifs("in.txt");
char data[30][30][30];
DIS_T distance[30][30][30];
Color color[30][30][30];
deque<id> Q;
id u;
short p, q, r;
int L, R, C;
while (cin >> L >> R >> C && L != 0)
{
memset(data, 0, sizeof(data));
memset(distance, 0, sizeof(distance));
memset(color, 0, sizeof(color));
Q.clear();
cin.get();
for (short i = 0; i < L; ++i)
{
for (short j = 0; j < R; ++j)
{
for (short k = 0; k < C; ++k)
{
data[i][j][k] = cin.get();
if (data[i][j][k] != 'S')
distance[i][j][k] = INF;
else
{
color[i][j][k] = grey;
Q.push_back(id(i, j, k));
}
if (data[i][j][k] == 'E')
{
data[i][j][k] = '.';
p = i;
q = j;
r = k;
}
}
cin.get();
}
cin.get();
}
while (!Q.empty())
{
u = Q.front();
if (u.x == p && u.y == q && u.z == r)
break;
Q.pop_front();
if (u.x > 0 && color[u.x - 1][u.y][u.z] == white && data[u.x - 1][u.y][u.z] == '.')
{
distance[u.x - 1][u.y][u.z] = distance[u.x][u.y][u.z] + 1;
color[u.x - 1][u.y][u.z] = grey;
Q.push_back(id(u.x - 1, u.y, u.z));
}
if (u.x < L - 1 && color[u.x + 1][u.y][u.z] == white && data[u.x + 1][u.y][u.z] == '.')
{
distance[u.x + 1][u.y][u.z] = distance[u.x][u.y][u.z] + 1;
color[u.x + 1][u.y][u.z] = grey;
Q.push_back(id(u.x + 1, u.y, u.z));
}
if (u.y > 0 && color[u.x][u.y - 1][u.z] == white && data[u.x][u.y - 1][u.z] == '.')
{
distance[u.x][u.y - 1][u.z] = distance[u.x][u.y][u.z] + 1;
color[u.x][u.y - 1][u.z] = grey;
Q.push_back(id(u.x, u.y - 1, u.z));
}
if (u.y < R - 1 && color[u.x][u.y + 1][u.z] == white && data[u.x][u.y + 1][u.z] == '.')
{
distance[u.x][u.y + 1][u.z] = distance[u.x][u.y][u.z] + 1;
color[u.x][u.y + 1][u.z] = grey;
Q.push_back(id(u.x, u.y + 1, u.z));
}
if (u.z > 0 && color[u.x][u.y][u.z - 1] == white && data[u.x][u.y][u.z - 1] == '.')
{
distance[u.x][u.y][u.z - 1] = distance[u.x][u.y][u.z] + 1;
color[u.x][u.y][u.z - 1] = grey;
Q.push_back(id(u.x, u.y, u.z - 1));
}
if (u.z < C - 1 && color[u.x][u.y][u.z + 1] == white && data[u.x][u.y][u.z + 1] == '.')
{
distance[u.x][u.y][u.z + 1] = distance[u.x][u.y][u.z] + 1;
color[u.x][u.y][u.z + 1] = grey;
Q.push_back(id(u.x, u.y, u.z + 1));
}
color[u.x][u.y][u.z] = black;
}
if (distance[p][q][r] < INF)
cout << "Escaped in " << distance[p][q][r] << " minute(s)." << endl;
else
cout << "Trapped!" << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator