您的位置:首页 > 其它

poj 22515 Dungeon Master

2015-08-06 16:06 232 查看
DungeonMaster

TimeLimit:1000MSMemoryLimit:65536K
TotalSubmissions:20926Accepted:8110
Description
Youaretrappedina3Ddungeonandneedtofindthequickestwayout!Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwithrock.Ittakesoneminutetomoveoneunitnorth,south,east,west,upordown.You
cannotmovediagonallyandthemazeissurroundedbysolidrockonallsides.

Isanescapepossible?Ifyes,howlongwillittake?

Input
Theinputconsistsofanumberofdungeons.EachdungeondescriptionstartswithalinecontainingthreeintegersL,RandC(alllimitedto30insize).

Listhenumberoflevelsmakingupthedungeon.

RandCarethenumberofrowsandcolumnsmakinguptheplanofeachlevel.

ThentherewillfollowLblocksofRlineseachcontainingCcharacters.Eachcharacterdescribesonecellofthedungeon.Acellfullofrockisindicatedbya'#'andemptycellsarerepresentedbya'.'.Yourstartingpositionisindicatedby'S'andthe
exitbytheletter'E'.There'sasingleblanklineaftereachlevel.InputisterminatedbythreezeroesforL,RandC.

Output
Eachmazegeneratesonelineofoutput.Ifitispossibletoreachtheexit,printalineoftheform

Escapedinxminute(s).

wherexisreplacedbytheshortesttimeittakestoescape.

Ifitisnotpossibletoescape,printtheline

Trapped!

SampleInput
345
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

133
S##
#E#
###

000

SampleOutput
Escapedin11minute(s).
Trapped!

bfs:


代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
usingnamespacestd;
structrecord
{
intx,y,z;//坐标;
inttime;//时间;
};
intvis[35][35][35];
chars[35][35][35];
intdx[6]={-1,0,0,1,0,0};//六个方向;
intdy[6]={0,-1,0,0,1,0};
intdz[6]={0,0,-1,0,0,1};
intl,r,c;
intx1,y1,z1;
queue<record>q;
intjudge(recorde)//判断该位置是否符合条件;
{
if(e.x<0||e.x>=l||e.y<0||e.y>=r||e.z<0||e.z>=c||vis[e.x][e.y][e.z]||s[e.x][e.y][e.z]=='#')
{
return0;
}
return1;
}
voidbfs()
{
recorde,temp;
inti;
while(!q.empty())//清空队列;
{
q.pop();
}
vis[x1][y1][z1]=1;//标记为被访问过;
e.x=x1;
e.y=y1;
e.z=z1;
e.time=0;//从(x1,y1,z1)开始,时间首先定为0;
q.push(e);//将可以走的位置插入到队列中;
while(!q.empty())//如果队列不为空;
{
e=q.front();//e设为队列的第一个元素;
q.pop();//取出第一个元素;
for(i=0;i<6;i++)//六个方向进行搜索;
{
temp=e;
temp.x+=dx[i];
temp.y+=dy[i];
temp.z+=dz[i];
temp.time=e.time+1;//时间+1;
if(!judge(temp))//不满足条件跳出本次循环
{
continue;
}
if(s[temp.x][temp.y][temp.z]=='E')//遇到出口输出;
{
printf("Escapedin%dminute(s).\n",temp.time);
return;
}
vis[temp.x][temp.y][temp.z]=1;//标记该位置已访问;
q.push(temp);
}
}
printf("Trapped!\n");
}
intmain()
{
inti,j,k,key;
while(scanf("%d%d%d",&l,&r,&c)!=EOF&&l||r||c)
{
memset(vis,0,sizeof(vis));
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
scanf("%s",s[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
for(k=0;k<c;k++)
{
if(s[i][j][k]=='S')
{
x1=i;y1=j;z1=k;
}
}
}
}
bfs();
}
return0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: