2251Dungeon Master

因为是三维,所以在变量上老是出问题,最后还是用了xyz来辨别,总算思维清晰了很多,一次性ACCEPT

#include "iostream"
#include "queue"
#include "algorithm"
#include "string.h"
#define N 35
using namespace std;
struct Point{
  int x,y,z;
};
int dir[6][3]={0,0,1, 0,0,-1, 0,1,0, 0,-1,0, 1,0,0, -1,0,0};
int min(int a,int b){return a>b?b:a;}

int main(){
  Point pt;
  int L,R,C,i,j,k,cnt,count[N][N][N];
  char list[N],map[N][N][N];
  while(cin>>L>>R>>C&&L&&R&&C){
    for(i=1;i<=L;i++){
     for(j=1;j<=R;j++){
       cin>>list;
      for(k=0;k<C;k++){
        map[j][k+1][i]=list[k];
        if(map[j][k+1][i]=='S'){pt.x=j;pt.y=k+1;pt.z=i;}
      }
    }
   }
   memset(count,0,sizeof(count));
   queue<Point> q;
   q.push(pt);
   cnt=10000;
   while(!q.empty()){
     Point tem=q.front();
     q.pop();
     for(i=0;i<6;i++){
       Point ans;
       ans.x=tem.x+dir[i][0];
       ans.y=tem.y+dir[i][1];
       ans.z=tem.z+dir[i][2];
       if(ans.x>0&&ans.x<=R&&ans.y>0&&ans.y<=C&&ans.z>0&&ans.z<=L&&count[ans.x][ans.y][ans.z]==0){
         if(map[ans.x][ans.y][ans.z]=='E'){cnt=min(cnt,count[tem.x][tem.y][tem.z]+1);break;}
         if(map[ans.x][ans.y][ans.z]=='.'){
           count[ans.x][ans.y][ans.z]=count[tem.x][tem.y][tem.z]+1;
           q.push(ans);
         }
       }
     }
   }
   if(cnt==10000)cout<<"Trapped!"<<endl;
   else cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
 }
}

 

posted @ 2013-09-29 14:00  龙城星  阅读(168)  评论(0编辑  收藏  举报