题目:长宽高为ABC的三维立方体,其中每个小立方体的状态为可行,不可行两种状态。起始点为(0,0,0),终点为(a-1,b-1,c-1),时间t:如果在时间T内可以走到终点,则胜利,反之,如果在时间T内不可到达终点,或者无法到达终点,则失败。
输入:第一行为k(测试样例数)
第二行ABCT
第三行到结束为立方体的每个格的状态,为1说明不可走,为0说明可通。
代码总是说RTL,本地运行做了部分测试没问题,不知道怎么回事,反正大概思路对了,实在找不到问题,就这样了。
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Maze {
public static int mincount = 1001;
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
int k = scanner.nextInt();
for (int i = 0; i < k; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int t = scanner.nextInt();
int maze[][][] = new int[a][b][c];
for (int i1 = 0; i1 < a; i1++) {
for (int i2 = 0; i2 < b; i2++) {
for (int i3 = 0; i3 < c; i3++) {
maze[i1][i2][i3] = scanner.nextInt();
}
}
}
maze[0][0][0] = 1;
escape(maze, 0, 0, 0, 0);
if (mincount < t)
System.out.println(mincount);
else
System.out.println(-1);
}
}
public static void escape(int[][][] in, int x, int y, int z, int count) {
int[][][] maze = in.clone();
int a = maze.length;
int b = maze[0].length;
int c = maze[0][0].length;
int[][] dir = { { 0, 0, 1 }, { 0, 0, -1 }, { 0, 1, 0 }, { 0, -1, 0 }, { 1, 0, 0 }, { -1, 0, 0 } };
if ((x == a - 1) && (y == b - 1) && (z == c - 1)) {
if (count < mincount)
mincount = count;
} else {
for (int i = 0; i < 6; i++) {
if (inbox(x, y, z, a, b, c, dir[i][0], dir[i][1], dir[i][2])) {
if (maze[x + dir[i][0]][y + dir[i][1]][z + dir[i][2]] == 0) {
maze[x + dir[i][0]][y + dir[i][1]][z + dir[i][2]] = 1;
count++;
escape(maze, x + dir[i][0], y + dir[i][1], z + dir[i][2], count);
count--;
maze[x + dir[i][0]][y + dir[i][1]][z + dir[i][2]] = 0;
}
}
}
}
}
public static boolean inbox(int x, int y, int z, int a, int b, int c, int dirx, int diry, int dirz) {
if ((x + dirx) >= 0 && (x + dirx) < a && (y + diry) >= 0 && (y + diry) < b && (z + dirz) >= 0 && (z + dirz) < c)
return true;
else
return false;
}
}