POJ1050

来源:互联网 发布:网络商学院排名 编辑:程序博客网 时间:2024/05/21 17:45

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1050

 思路:穷举+动态规划

 

  1. import java.util.Scanner;
  2. public class Main {
  3.     int[][] a;
  4.     int l;
  5.     int max = Integer.MIN_VALUE;
  6.     int t;
  7.     int t1;
  8.     int t2[];
  9.     public Main() {
  10.         Scanner scan = new Scanner(System.in);
  11.         l = scan.nextInt();
  12.         a = new int[l][l];
  13.         for (int i = 0; i < l; i++) {
  14.             for (int j = 0; j < l; j++) {
  15.                 a[i][j] = scan.nextInt();
  16.             }
  17.         }
  18.         search();
  19.         System.out.println(max);
  20.     }
  21.     public void search() {
  22.         for (int i = 0; i < l; i++) {
  23.             for (int j = 0; j < l; j++) {
  24.                 t1 = 0;
  25.                 t2 = new int[l];
  26.                 for (int k = i; k < l; k++) {
  27.                     t1 += a[k][j];
  28.                     t = t1;
  29.                     if (t > max) {
  30.                         max = t;
  31.                     }
  32.                     for (int m = j + 1; m < l; m++) {
  33.                         t2[m] += a[k][m];
  34.                         t += t2[m];
  35.                         if (t > max) {
  36.                             max = t;
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     public static void main(String[] args) {
  44.         new Main();
  45.     }
  46. }
原创粉丝点击