POJ-2251

#include <stdio.h>
#include <string.h>

#define MAX 30000

char dungeon[30][30][30];
int visits[30][30][30];
int queue[MAX], front, rear;
int move[6][3] = {{-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 1, 0}, {0, 0, -1}, {1, 0, 0}}; // 移动方位 
int l, r, c;
int sL, sR, sC;

int bfs(int x, int y, int z)
{
     int i, xt, yt, zt;
     front = rear = 0;
     queue[rear++] = x*r*c+y*c+z;
     visits[x][y][z] =  1;
     
     while (front < rear) {
           x=queue[front]/(r*c);
		   y=(queue[front]%(r*c))/c;
		   z=(queue[front]%(r*c))%c;
           front++;
           for (i = 0; i < 6; i++) {
               xt = x+move[i][0];
               yt = y+move[i][1];
               zt = z+move[i][2];
               if (xt>=0 && xt<l && yt>=0 && yt<r && zt>=0 && zt<c && dungeon[xt][yt][zt] != '#' && !visits[xt][yt][zt]) {
                  if (dungeon[xt][yt][zt] == 'E')
                     return visits[x][y][z];
                  visits[xt][yt][zt] = visits[x][y][z]+1;
                  queue[rear++] = xt*r*c + yt*c + zt;                 
               }
           }
     }
     return 0;
}

int main()
{
    int i, j, k, minute;
    while (scanf("%d%d%d\n", &l, &r, &c), l, r, c) {
          memset(dungeon, '#', sizeof(dungeon));
		  for (i = 0; i < l; i++) {
              for (j = 0; j < r; j++) {
                  for (k = 0; k < c; k++) {
                      visits[i][j][k] = 0;
                      dungeon[i][j][k] = getchar();
                      if (dungeon[i][j][k] == 'S') {
                         sL = i;
                         sR = j;
                         sC = k;
                      }
                  }
                  getchar();
              }
			  getchar();
         }
		 minute = bfs(sL, sR, sC);
		 if (minute)
            printf("Escaped in %d minute(s).\n", minute);
         else
            printf("Trapped!\n");
    }
    return 0;
}


你可能感兴趣的:(c,include)