递推
View Code
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while (true) { int n, m; m = cin.nextInt(); n = cin.nextInt(); if (n == 0 && m == 0) return; String[] map = new String[n]; for (int i = 0; i < n; i++) map[i] = cin.next(); BigInteger[][] f = new BigInteger[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (map[i].charAt(j) >= '0' && map[i].charAt(j) <= '9') { BigInteger temp = BigInteger.valueOf((int) (map[i] .charAt(j) - '0')); f[i][j] = temp; if (j - 1 >= 0) { BigInteger a = f[i][j - 1].multiply( BigInteger.valueOf(10)).add(temp); if (f[i][j].compareTo(a) < 0) f[i][j] = a; } if (i - 1 >= 0) { BigInteger a = f[i - 1][j].multiply(BigInteger.valueOf(10)) .add(temp); if (f[i][j].compareTo(a) < 0) f[i][j] = a; } // System.out.println(i + " " + j + " " + f[i][j]); } else f[i][j] = BigInteger.ZERO; BigInteger ans = BigInteger.ZERO; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (ans.compareTo(f[i][j]) < 0) ans = f[i][j]; System.out.println(ans); } } }