【发布时间】:2021-05-31 03:30:23
【问题描述】:
static int ud[] = new int[] { 0, 0, 1 };
static int lr[] = new int[] { -1, 1, 0 };
static int dirs[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 } };
public static int solve(int r, int c, int dir) {
if (r == n - 1 && c == m - 1)
return v[r][c];
if (dp[r][c][dir] != -INF)
return dp[r][c][dir];
visited[r][c] = true;
int ans = -200000;
for (int i = 0; i < 3; i++) {
int nr = r + dirs[i][0]; # no time out
int nc = c + dirs[i][1]; # no time out
// int nr = r + ud[i]; # time out
// int nc = c + lr[i]; # time out
if (nr < 0 || nc < 0 || nr >= n || nc >= m)
continue;
if (visited[nr][nc])
continue;
ans = Math.max(ans, solve(nr, nc, i) + v[r][c]);
}
visited[r][c] = false;
return dp[r][c][dir] = ans;
}
我想在二维数组中将 row,col 向上、向左、向右移动 所以我像往常一样声明了两个一维数组并提交了我的代码,但答案是失败 我声明了二维数组而不是一维数组中的两个,我提交了我的代码 答案通过了,不知道为什么 二维数组比两个一维数组快吗?
【问题讨论】: