您的位置:首页 > 其它

Dungeon Master

2016-04-08 19:59 232 查看
题目链接:http://poj.org/problem?id=2251

本题有两个代码,区别在于输入的处理上面,而这道题在输入上面要特别注意,第一个代码需要在输入时加上“\n",而第二个代码就避免了这个问题,运用了一个temp,if 判断去限制读入的字符的种类。同时在输入过程中由于有三层地图,所以会有回车输入,第一个代码采用了getchar吃掉回车。其次在这个代码中犯了很多低级错误,在将队首元素入队弹出时,没有在while(!q.empty())里面进行,在输出时也要注意格式。还要注意本题三层地图,最里面一层的循环才是x的坐标,不要混淆。这道题由于有三层地图,因此需要开长度为6的数组。

第一个

#include<iostream>
#include<queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int dx[6]={1,-1,0,0,0,0};
const int dy[6]={0,0,1,-1,0,0};
const int dz[6]={0,0,0,0,1,-1};
const int maxn=35;

struct node
{
int x,y,z;
int s;
};
node start,ending;
node New,Now;
queue<node>q;
bool f[maxn][maxn][maxn];
char m[maxn][maxn][maxn];
int i, j, k;
int L,R,C;
int x,y,z;

int bfs()
{

while(!q.empty())
{
Now = q.front();
q.pop();
for( i=0;i<6;++i)
{
New.x=Now.x+dx[i];
New.y=Now.y+dy[i];
New.z=Now.z+dz[i];
New.s=Now.s+1;

if(m[New.z][New.y][New.x]=='#'||f[New.z][New.y][New.x])
continue;
q.push(New);
f[New.z][New.y][New.x]=true;
if(New.x==ending.x&&New.y==ending.y&&New.z==ending.z)
return New.s;

}

}
return -1;
}

int main()
{
while(scanf("%d%d%d\n",&L,&R,&C)&&R&&C&&L)
{
memset(f,0,sizeof(f));
memset(m,'#',sizeof(m));

while(!q.empty())
q.pop();
for( i = 1;i<= L;++i)
{
for(j = 1;j<= R;++j)
{
for( k = 1;k<= C;++k)
{
m[i][j][k] = getchar();
if(m[i][j][k]=='S')
{
f[i][j][k]=true;
start.x = k;
start.y = j;
start.z = i;
start.s = 0;
q.push(start);
}
if(m[i][j][k]=='E')
{
ending.x = k;
ending.y = j;
ending.z = i;
}
}
getchar();
}
getchar();
}

int ans=bfs();
if( ans <0)
puts("Trapped!");
else
cout << "Escaped in " <<ans << " minute(s)." << endl;

}
return 0;
}*/
/*#include <cstdio>
#include <cstring>
#include <queue>
#include<iostream>
using namespace std;
const int dx[6] = {1, -1, 0, 0, 0, 0};
const int dy[6] = {0, 0, 1, -1, 0, 0};
const int dz[6] = {0, 0, 0, 0, 1, -1};
const int maxn = 35;
struct node
{
int x, y, z, step;

} tmp;
node S,T;
node now;
queue<node> q;
int n, m, t, x, y, z, i, j, k;
bool vis[maxn][maxn][maxn];
char mp[maxn][maxn][maxn];

int bfs()
{
while (!q.empty())
{

now = q.front();
q.pop();
for (i = 0; i < 6; ++i)
{

tmp.x = now.x + dx[i];
tmp.y = now.y + dy[i];
tmp.z = now.z + dz[i];
tmp.step = now.step + 1;

if(mp[tmp.z][tmp.y][tmp.x] == '#' ||vis[tmp.z][tmp.y][tmp.x] )
continue;
q.push(tmp);
vis[tmp.z][tmp.y][tmp.x] = true;
if(tmp.x == T.x && tmp.y == T.y && tmp.z == T.z)
return tmp.step;
}
}
return -1;
}

int main()
{
while (scanf("%d%d%d\n", &t, &n, &m)&& t && m && n)
{
memset(vis, 0, sizeof(vis));
memset(mp, '#', sizeof(mp));
while (!q.empty()) q.pop();
for (i = 1; i <= t; ++i)
{
for (j = 1; j <= n; ++j)
{
for (k = 1; k <= m; ++k)
{
mp[i][j][k] = getchar();
if (mp[i][j][k] == 'S')
{
vis[i][j][k] = true;
S.x = k;
S.y = j;
S.z = i;
S.step = 0;
q.push(S);
}
if (mp[i][j][k] == 'E')
{
T.x = k;
T.y = j;
T.z = i;
}
}
getchar();
}
getchar();
}
int ans = bfs();
if (ans<0) puts("Trapped!");
else
cout << "Escaped in " <<ans << " minute(s)." << endl;

}
return 0;
}
第二个

#include<iostream>
using namespace std;
#include<queue>
#include<cstring>
#include<string>
char temp;
bool vis[50][50][50];
bool vist[50][50][50];
const int dx[6] = {1,-1,0,0,0,0};
const int dy[6] = {0,0,1,-1,0,0};
const int dz[6] = {0,0,0,0,-1,1};
struct node
{
int x,y,z;
int step;
};
node now;
node S,T;
int bfs()
{
memset(vis,false,sizeof(vis));
queue<node>q;
q.push(S);
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0; i<6; i++)
{
node New;
New.x = now.x + dx[i];
New.y = now.y + dy[i];
New.z = now.z + dz[i];
New.step = now.step + 1;
if(!vist[New.x][New.y][New.z] || vis[New.x][New.y][New.z])
continue;
q.push(New);
vis[New.x][New.y][New.z] = true;
if(New.x == T.x && New.y == T.y && New.z == T.z)
return New.step;
}
}
return -1;
}
int main()
{
int L, R, C;
while(cin >> L >> R >> C && L)
{
memset(vist,false,sizeof(vist));
for(int i=1; i<=L; i++)
{
for(int j=1; j<=R; j++)
{
for(int k=1; k<=C; k++)
{
cin >> temp;
if(temp == 'S')
{
vist[i][j][k] = true;
S.x = i;
S.y = j;
S.z = k;
S.step = 0;
}
if(temp == 'E')
{
vist[i][j][k] = true;
T.x = i;
T.y = j;
T.z = k;
}
if(temp == '.')
{
vist[i][j][k] = true;
}
}
}
}
int ans = bfs();
if (ans<0) cout <<"Trapped!" << endl;
else
cout << "Escaped in " <<ans << " minute(s)." << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: