用java写的 超级慢。菜鸟级

import java.util.Scanner;
public class Main {
public int[][] m = new int[501][501];
public int[][] dp = new int[501][501];
public int[] mx = { 0, 1, 0, 0, -1 };
public int[] my = { 0, 0, 1, -1, 0 };
public int r, c;

public Main() {
int i = 0, j = 0;
Scanner sc = new Scanner(System.in);
r = sc.nextInt();
c = sc.nextInt();
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++) {
m[i][j] = sc.nextInt();
dp[i][j] = 1;
}
}

public int dpSearch(int x, int y) {
if (dp[x][y] > 1)
return dp[x][y];
int i = 0, j = 0;
boolean flag = false;
for (i = 1; i <= 4; i++) {
int k = x + mx[i];
int v = y + my[i];
if (k >= 1 && k <= r && v >= 1 && v <= c && m[k][v] < m[x][y]) {
dp[x][y] = Math.max(dp[x][y], dpSearch(k, v) + 1);
}
}
return dp[x][y];

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Main dpClass = new Main();
int max = -1;
for (int i = 1; i <= dpClass.r; i++)
for (int j = 1; j <= dpClass.c; j++)
max = Math.max(max, dpClass.dpSearch(i, j));
System.out.println(max);
}
}

0 条评论

目前还没有评论...

信息

ID
1011
难度
6
分类
动态规划 点击显示
标签
递交数
10384
已通过
2952
通过率
28%
被复制
29
上传者