UVa 532 - Dungeon Master
/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
// Algorithm: BFS (3D)
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 203;
char adj[high][high][high];
int cost[high][high][high];
int L , R , C;
int dir_x[6] = {-1, 1, 0, 0, 0, 0} , dir_y[6] = {0, 0, -1, 1, 0 , 0}, dir_z[6] = {0, 0, 0, 0, -1, 1};
// -1, 1 first , middle and last due to 3D
bool visited[high][high][high];
void BFS(int x , int y, int z)
{
queue<int>Q;
Q.push(x);
Q.push(y);
Q.push(z);
cost[x][y][z] = 0;
visited[x][y][z] = true;
while(!Q.empty())
{
int ux = Q.front();
Q.pop();
int uy = Q.front();
Q.pop();
int uz = Q.front();
Q.pop();
for(int i=0; i<6; i++)
{
int vx = ux + dir_x[i];
int vy = uy + dir_y[i];
int vz = uz + dir_z[i];
if((vx>=0 && vx<L) && (vy>=0 && vy<R) && (vz>=0 && vz<C) && (adj[vx][vy][vz]!='#'))
{
if(!visited[vx][vy][vz])
{
visited[vx][vy][vz] = true;
cost[vx][vy][vz] = cost[ux][uy][uz] + 1;
Q.push(vx);
Q.push(vy);
Q.push(vz);
}
}
}
}
}
int main()
{
fast;
char ch;
int i , j , k , s1 , s2, s3 , e1, e2, e3;
while(cin >> L >> R >> C)
{
if(!L && !R && !C) break;
mset(adj , '#');
mset(cost , 0);
mset(visited , false);
for(i=0; i<L; i++)
{
for(j=0; j<R; j++)
{
for(k=0; k<C; k++)
{
cin >> ch;
if(ch == 'S')
{
s1 = i;
s2 = j;
s3 = k;
}
if(ch == 'E')
{
e1 = i;
e2 = j;
e3 = k;
}
adj[i][j][k] = ch;
}
}
}
BFS(s1 , s2 , s3);
//cout << cost[e1][e2][e3] << "\n";
if(cost[e1][e2][e3])
{
cout << "Escaped in " << cost[e1][e2][e3] << " minute(s).\n";
}
else
{
cout << "Trapped!\n";
}
}
return 0;
}
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
// Algorithm: BFS (3D)
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 203;
char adj[high][high][high];
int cost[high][high][high];
int L , R , C;
int dir_x[6] = {-1, 1, 0, 0, 0, 0} , dir_y[6] = {0, 0, -1, 1, 0 , 0}, dir_z[6] = {0, 0, 0, 0, -1, 1};
// -1, 1 first , middle and last due to 3D
bool visited[high][high][high];
void BFS(int x , int y, int z)
{
queue<int>Q;
Q.push(x);
Q.push(y);
Q.push(z);
cost[x][y][z] = 0;
visited[x][y][z] = true;
while(!Q.empty())
{
int ux = Q.front();
Q.pop();
int uy = Q.front();
Q.pop();
int uz = Q.front();
Q.pop();
for(int i=0; i<6; i++)
{
int vx = ux + dir_x[i];
int vy = uy + dir_y[i];
int vz = uz + dir_z[i];
if((vx>=0 && vx<L) && (vy>=0 && vy<R) && (vz>=0 && vz<C) && (adj[vx][vy][vz]!='#'))
{
if(!visited[vx][vy][vz])
{
visited[vx][vy][vz] = true;
cost[vx][vy][vz] = cost[ux][uy][uz] + 1;
Q.push(vx);
Q.push(vy);
Q.push(vz);
}
}
}
}
}
int main()
{
fast;
char ch;
int i , j , k , s1 , s2, s3 , e1, e2, e3;
while(cin >> L >> R >> C)
{
if(!L && !R && !C) break;
mset(adj , '#');
mset(cost , 0);
mset(visited , false);
for(i=0; i<L; i++)
{
for(j=0; j<R; j++)
{
for(k=0; k<C; k++)
{
cin >> ch;
if(ch == 'S')
{
s1 = i;
s2 = j;
s3 = k;
}
if(ch == 'E')
{
e1 = i;
e2 = j;
e3 = k;
}
adj[i][j][k] = ch;
}
}
}
BFS(s1 , s2 , s3);
//cout << cost[e1][e2][e3] << "\n";
if(cost[e1][e2][e3])
{
cout << "Escaped in " << cost[e1][e2][e3] << " minute(s).\n";
}
else
{
cout << "Trapped!\n";
}
}
return 0;
}
Comments
Post a Comment