poj1050 二维数组最大子序列矩阵和

 1 public class Main {
 2 
 3     public int findMaxSumOneDimensions(int[] arr) {
 4         int len = arr.length;
 5         int max = arr[0], sum = max;
 6         for (int i = 1; i < len; i++) {
 7             sum += arr[i];
 8             if (sum < 0)
 9                 sum = arr[i];
10             if (max <= sum)
11                 max = sum;
12         }
13         return max;
14     }
15 
16     public int findMaxSumTwoDimensions(int[][] arr, int N) {
17         int max = 0;
18         int[] b = new int[N];
19         for (int i = 0; i < N; i++) {
20             for (int j = 0; j < N; j++)
21                 b[j] = 0;
22             for (int j = i; j < N; j++) {
23                 for (int k = 0; k < N; k++)
24                     b[k] += arr[j][k];
25                 int temp = findMaxSumOneDimensions(b);
26                 if (temp >= max)
27                     max = temp;
28             }
29         }
30         return max;
31     }
32 
33     public static void main(String[] args) throws FileNotFoundException {
34         // Scanner scanner = new Scanner(new BufferedInputStream(new
35         // FileInputStream(new File("E:\\input.txt"))));
36         Main main = new Main();
37         Scanner scanner = new Scanner(new BufferedInputStream(System.in));
38         int N = scanner.nextInt();
39         int[][] arr = new int[N][N];
40         for (int i = 0; i < N; i++) {
41             for (int j = 0; j < N; j++) {
42                 arr[i][j] = scanner.nextInt();
43             }
44         }
45         System.out.println(main.findMaxSumTwoDimensions(arr, N));
46     }
47 }
posted @ 2012-08-07 21:47  苦逼程序猴  阅读(261)  评论(0编辑  收藏  举报