【发布时间】:2016-04-19 12:33:43
【问题描述】:
我遇到了这个问题。
让我们考虑一个游戏,它有一个 3 格宽、N 格高的棋盘。每个单元格都有一个 0 到 99 之间的数字写在上面。您可以在任何 顶行的单元格并从底部的任何单元格退出。你 可以从一个单元格移动到下一行中的任何相邻单元格(对角线或正下方)。 当你在一个单元格时,你必须加上或减去所写的数字 在细胞上。 对于给定值 X,大于或等于的最小数是多少 X,你可以从这个过程中获得?
输入 输入的第一行包含一个整数,数字 的测试用例。每个测试用例的第一行包含两个整数,N 和 X。 接下来的 N 行每行包含三个 空格分隔的数字,必须添加或减去的单元格值。
输出
对于每组输入打印大于或等于的最小数 十。
示例输入
2
3 0
83 86 77
15 93 35
86 92 49
3 59
83 86 77
15 93 35
86 92 49
样本输出
2 59
注意:
2 = 86 - 35 - 49
59 = 86 - 93 + 86
这是我的解决方案:
import java.util.Scanner;
public class n3 {
static int min;
static int result;
static int X;
int go = 0;
public static void main(String args[]) throws Exception {
{
Scanner sc = new Scanner(System.in);
// sc = new Scanner(new FileInputStream("input.txt"));
int T = sc.nextInt();
for (int tc = 0; tc < T; tc++) {
int N = sc.nextInt();
min = sc.nextInt();
result = Integer.MAX_VALUE;
int m[][] = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 3; j++) {
m[i][j] = sc.nextInt();
}
}
n3 game = new n3();
game.start(m, N);
System.out.println(result);
}
}
}
private void start(int[][] m, int N) {
int x = min;
compute(m, -1, 0, 0, N);
// compute(m, -1, 0, -x, N);
compute(m, -1, 1, 0, N);
compute(m, -1, 2, 0, N);
}
private boolean isSafe(int[][] m, int row, int col, int N) {
if (row >= 0 && col >= 0 && row < N && col < 3) {
return true;
}
return false;
}
private void compute(int[][] m, int x, int y, int value, int N) {
// System.out.println(value + " " + x + " " + y);
if (go == 1) {
return;
}
if (x == N - 1 && value >= min) {
if (value < result) {
result = value;
}
if (value == min) {
result = min;
go = 1;
}
return;
}
if (isSafe(m, x + 1, y - 1, N)) {
compute(m, x + 1, y - 1, value + m[x + 1][y - 1], N);
compute(m, x + 1, y - 1, value - m[x + 1][y - 1], N);
}
if (isSafe(m, x + 1, y, N)) {
compute(m, x + 1, y, value + m[x + 1][y], N);
compute(m, x + 1, y, value - m[x + 1][y], N);
}
if (isSafe(m, x + 1, y + 1, N)) {
compute(m, x + 1, y + 1, value + m[x + 1][y + 1], N);
compute(m, x + 1, y + 1, value - m[x + 1][y + 1], N);
}
}
}
但是,当 N 很大时,这会失败。有没有其他方法可以解决这个问题?
【问题讨论】:
-
什么是 n 3 方板?
-
当您描述这款游戏时,请考虑 (a) 我们不了解这款游戏,并且 (b) 我们无法读懂您的想法。
-
失败怎么办?堆栈跟踪或您期望和接收的输出会很好。
标签: algorithm recursion dynamic-programming