Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

BFS,0MS,1A,附代码

Posted by a798253982 at 2013-07-28 17:54:27 on Problem 2251
#include <iostream> 
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef struct
{
	int x,y,z;
	int n;
}Point;

int dir[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};

char map[31][31][31];
int l,r,c,ans,flag[31][31][31];

int Judge(int x,int y,int z)
{
	if(x>=0 && x<l && y>=0 && y<r && z>=0 && z<c)
	    return 1;
	else
		return 0;  
}

void bfs(int x,int y,int z)
{
	queue<Point> q;
	Point p0,p1,p2;
	
	p0.x=x;p0.y=y;p0.z=z;p0.n=0;
	flag[x][y][z]=1;
	q.push(p0);
	
	while(!q.empty())
	{
		p1=q.front();q.pop();
		for(int i=0;i<6;i++)
		{
			int temp1=p1.x+dir[i][0];
			int temp2=p1.y+dir[i][1];
			int temp3=p1.z+dir[i][2];
			if(Judge(temp1,temp2,temp3) && !flag[temp1][temp2][temp3] && map[temp1][temp2][temp3]!='#')
			{
				if(map[temp1][temp2][temp3]=='E'){
					ans=p1.n+1;return;
				}
				p2.x=temp1;p2.y=temp2;p2.z=temp3;p2.n=p1.n+1;
				flag[temp1][temp2][temp3]=1;
				q.push(p2);
			}
		}
	}
	
}

int main(){
	//freopen("input.txt","r",stdin);
	int i,j,k,si,sj,sk;
	while(~scanf("%d%d%d",&l,&r,&c))
	{
		if(l==0 && r==0 && c==0) break;
		
		for(i=0;i<l;i++)
			for(j=0;j<r;j++){
				scanf("%s",map[i][j]);
				for(k=0;k<strlen(map[i][j]);k++)
					if(map[i][j][k]=='S'){
				    	si=i;sj=j;sk=k;
					}
			}
			
		memset(flag,0,sizeof(flag));
		ans=-1;
		bfs(si,sj,sk);
		
		if(ans!=-1) 
		    printf("Escaped in %d minute(s).\n",ans);
		else
		    printf("Trapped!\n");
	}
	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator