http://hihocoder.com/problemset/problem/1664
求01间隔矩阵的最大边长
思路:转移方程 dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))
1 import java.math.BigInteger; 2 import java.util.Arrays; 3 import java.util.Map; 4 import java.util.Scanner; 5 6 public class Main { 7 8 public static void main(String[] args) { 9 Scanner cin = new Scanner(System.in); 10 int [][]cube = new int[1002][1002]; 11 int [][]dp = new int[1002][1002]; 12 String s; 13 int m,n; 14 m = cin.nextInt(); 15 n = cin.nextInt(); 16 for(int i = 1;i<=m;i++){ 17 s = cin.next(); 18 for(int j = 1;j<=n;j++) 19 cube[i][j] = s.charAt(j-1)-‘0‘; 20 } 21 for(int i = 1;i<=m;i++) 22 dp[i][1] = 1; 23 for(int i = 1;i<=n;i++) 24 dp[1][i] = 1; 25 for(int i = 2;i<=m;i++) 26 for(int j = 2;j<=n;j++){ 27 if(cube[i-1][j-1]==cube[i][j]&&cube[i-1][j]!=cube[i][j]&&cube[i][j-1]!=cube[i][j]){ 28 int val = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])); 29 if(val==0) 30 dp[i][j] = 2; 31 else 32 dp[i][j] = val+1; 33 } 34 } 35 int ans = 0; 36 for(int i = 1;i<=m;i++) 37 for(int j = 1;j<=n;j++) 38 if(dp[i][j]>ans) 39 ans = dp[i][j]; 40 System.out.println(ans); 41 } 42 }