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 |
用stl里面queue刷的第一道题目!附代码!#include <iostream> #include <string.h> #include <stdio.h> #include <queue> using namespace std; const int size = 50; struct Node { int x,y,z,time; }; int sx,sy,sz; int drection[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; class poj2251 { private: char maze[size][size][size]; int visited[size][size][size]; public: int X,Y,Z; void input(); int result(); }; void poj2251::input() { memset(visited,0,sizeof(visited)); for(int i=1;i<=X;i++) { for(int j=1;j<=Y;j++) { for(int k=1;k<=Z;k++) { cin>>maze[i][j][k]; if(maze[i][j][k]=='S') { sx=i; sy=j; sz=k; } } } } } int poj2251::result() { Node t; t.x=sx; t.y=sy; t.z=sz; t.time=0; visited[t.x][t.y][t.z]=1; queue<Node> p; p.push(t); while(!p.empty()) { Node temp1,temp2; temp1=p.front(); p.pop(); for(int i=0;i<6;i++) { temp2.x=temp1.x+drection[i][0]; temp2.y=temp1.y+drection[i][1]; temp2.z=temp1.z+drection[i][2]; temp2.time=temp1.time+1; if(!visited[temp2.x][temp2.y][temp2.z]&&maze[temp2.x][temp2.y][temp2.z]!='#' &&temp2.x>=1&&temp2.x<=X&&temp2.y>=1&&temp2.y<=Y&&temp2.z>=1&&temp2.z<=Z) { if(maze[temp2.x][temp2.y][temp2.z]=='E') return temp2.time; visited[temp2.x][temp2.y][temp2.z]=1; p.push(temp2); } } } return -1; } int main() { poj2251 t; while(scanf("%d %d %d",&t.X,&t.Y,&t.Z)!=EOF) { if(t.X==0&&t.Y==0&&t.Z==0) break; t.input(); int times=t.result(); if(times==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",times); } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator