能够做到这里,就说明有一定的深搜(dfs)、广搜的基础(bfs),dfs用来找到所有的情况,它和bfs的区别就在于此,bfs也可以找到所有情况,但是,第一种永远是最短的,因为是按照称述来计数的。


  1. import java.util.*;
  2. public class Main {
  3. static class ZuoBiao {//存放位置,zxy坐标
  4. int z;
  5. int x;
  6. int y;
  7. public ZuoBiao(int z, int x, int y) {
  8. super();
  9. this.z = z;
  10. this.x = x;
  11. this.y = y;
  12. }
  13. public ZuoBiao() {
  14. }
  15. }
  16. static Scanner sc = new Scanner(System.in);
  17. public static void main(String[] args) {
  18. int z = sc.nextInt();
  19. int x = sc.nextInt();
  20. int y = sc.nextInt();
  21. while(z!=0&&y!=0&&z!=0) {
  22. f(z,x,y);
  23. z = sc.nextInt();
  24. x = sc.nextInt();
  25. y = sc.nextInt();
  26. }
  27. // for (int i = 0; i < z; i++) {//三维数组储存完毕
  28. // for (int j = 0; j < x; j++) {
  29. // for (int k = 0; k < y; k++) {
  30. // System.out.print(arr[i][j][k]);
  31. // }
  32. // System.out.println();
  33. // }
  34. // System.out.println("*****");
  35. // }
  36. }
  37. private static void f(int z,int x,int y) {
  38. // TODO Auto-generated method stub
  39. ZuoBiao qiDian = new ZuoBiao();
  40. char[][][] arr = new char[z][x][y];
  41. int[][][] chek = new int[z][x][y];
  42. for (int i = 0; i < z; i++) {
  43. for (int j = 0; j < x; j++) {
  44. char[] charArray = sc.next().toCharArray();
  45. for (int k = 0; k < y; k++) {
  46. if (arr[i][j][k] == 'S') {
  47. qiDian.z = i;
  48. qiDian.x = j;
  49. qiDian.y = k;
  50. chek[i][j][k] = 1;
  51. }
  52. arr[i][j][k] = charArray[k];
  53. }
  54. }
  55. }
  56. Queue<ZuoBiao> dui = new LinkedList<ZuoBiao>();
  57. dui.offer(qiDian);
  58. int cont = 0;
  59. while (!dui.isEmpty()) {
  60. int size = dui.size();
  61. cont++;//记录层数
  62. for (int i = 0; i < size; i++) {
  63. ZuoBiao poll = dui.poll();
  64. int z1 = poll.z;
  65. int x1 = poll.x;
  66. int y1 = poll.y;
  67. if (arr[z1][x1][y1] == 'E') {
  68. System.out.println("Escaped in " + (cont - 1) + " minute(s).");
  69. return;
  70. }
  71. if (x1 - 1 >= 0 && arr[z1][x1 - 1][y1] != '#' && chek[z1][x1 - 1][y1] != 1) {// 上
  72. chek[z1][x1 - 1][y1] = 1;
  73. dui.offer(new ZuoBiao(z1, x1 - 1, y1));
  74. }
  75. if (y1 + 1 < y && arr[z1][x1][y1 + 1] != '#' && chek[z1][x1][y1 + 1] != 1) {// 右
  76. chek[z1][x1][y1 + 1] = 1;
  77. dui.offer(new ZuoBiao(z1, x1, y1 + 1));
  78. }
  79. if (x1 + 1 < x && arr[z1][x1 + 1][y1] != '#' && chek[z1][x1 + 1][y1] != 1) {// 下
  80. chek[z1][x1 + 1][y1] = 1;
  81. dui.offer(new ZuoBiao(z1, x1 + 1, y1));
  82. }
  83. if (y1 - 1 >= 0 && arr[z1][x1][y1 - 1] != '#' && chek[z1][x1][y1 - 1] != 1) {// 左
  84. chek[z1][x1][y1 - 1] = 1;
  85. dui.offer(new ZuoBiao(z1, x1, y1 - 1));
  86. }
  87. if (z1 + 1 < z && arr[z1 + 1][x1][y1] != '#' && chek[z1 + 1][x1][y1] != 1) {// 下一层
  88. chek[z1 + 1][x1][y1] = 1;
  89. dui.offer(new ZuoBiao(z1 + 1, x1, y1));
  90. }
  91. if (z1 - 1 >= 0 && arr[z1 - 1][x1][y1] != '#' && chek[z1 - 1][x1][y1] != 1) {// 上一层
  92. chek[z1 - 1][x1][y1] = 1;
  93. dui.offer(new ZuoBiao(z1 - 1, x1, y1));
  94. }
  95. }
  96. }
  97. System.out.println("Trapped!");
  98. }

}

点赞(0)
 

9.9 分

1 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 1 条评论

瞿肖 2年前 回复TA
最后减一我到现在也没明白,减一就过了,可能是开头或者结束的一步不算在里面