poj2251



package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class A2251C {
 static class Node {
  int z;
  int x;
  int y;
  int step;
 }

 static int L, R, C;
 static int[] Dx = { -1, 1, 0, 0, 0, 0 };
 static int[] Dy = { 0, 0, -1, 1, 0, 0 };
 static int[] Dz = { 0, 0, 0, 0, -1, 1 };
 static char[][][] map;
 static int[][][] data;
 static int sx, sy, sz;
 static int ex, ey, ez;
 static boolean isFind;
 static Node[] queue;// 队列里数组node[x,y,z,step]

 public static void main(String[] args) throws FileNotFoundException {
  /* Scanner sc= new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/2251"));
  while (true) {
   L = sc.nextInt();
   R = sc.nextInt();
   C = sc.nextInt();
   map = new char[L][R][C];
   data = new int[L][R][C];
   queue = new Node[L * R * C];
   if (L == 0 && R == 0 && C == 0) {
    break;
   }
   sc.nextLine();
   for (int z = 0; z < L; z++) {
    for (int x = 0; x < R; x++) {
     char[] line = sc.nextLine().toCharArray();
     for (int y = 0; y < line.length; y++) {
      map[z][x][y] = line[y];
      if (map[z][x][y] == 'S') {
       data[z][x][y] = 2;
       sx = x;
       sy = y;
       sz = z;
      }
      if (map[z][x][y] == 'E') {
       data[z][x][y] = 3;
       ex = x;
       ey = y;
       ez = z;
      }
      if (map[z][x][y] == '.') {
       data[z][x][y] = 0;
      }
      if (map[z][x][y] == '#') {
       data[z][x][y] = 1;
      }
     }
    }
    sc.nextLine();
   }
   isFind = false;
   bfs(sz, sx, sy);
   if (!isFind) {
    System.out.println("Trapped!");
   }
  }
 }

 private static void bfs(int z, int x, int y) {
  int head = 0;// 头指针
  int tail = 0;// 尾指针
  Node node = new Node();
  node.z = z;
  node.x = x;
  node.y = y;
  node.step=data[z][x][y];
  queue[tail++] = node;
  while (head < tail) {
   for (int i = 0; i < 6; i++) {
    int dx = queue[head].x + Dx[i];
    int dy = queue[head].y + Dy[i];
    int dz = queue[head].z + Dz[i];
    if (dx >= 0 && dx < R && dy < C && dy >= 0 && dz >= 0 && dz < L) {
     if (data[dz][dx][dy] == 0) {
      data[dz][dx][dy] = 4;
      Node node1 = new Node();// 满足传染的node
      node1.z =queue[head].z + Dz[i];
      node1.x =queue[head].x + Dx[i];
      node1.y =queue[head].y + Dy[i];
      node1.step=queue[head].step+1;
      queue[tail++] = node1;
     }
     if (dx == ex && dy == ey && dz == ez) {
      System.out.println("Escaped in " + (queue[head].step+1 - 2)
        + " minute(s).");
      head = tail;
      isFind = true;
      break;
     }
    }
   }
          head++;
  }
 }
}

//

3 4 5
S....
.###.
.##..
###.#

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

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

1 3 3
S##
#E#
###

1 3 3
S.#
#E#
###

0 0 0

//

Escaped in 11 minute(s).
Trapped!
Escaped in 2 minute(s).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值