Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. public static void main(String[] args) {
  2.     Scanner sc = new Scanner();
  3.     int t = sc.nextInt();
  4.     for (int currentCase = 0; currentCase < t; currentCase++) {
  5.       int r = sc.nextInt();
  6.       sc.nextInt();
  7.       char[][] board = sc.nextGrid(r);
  8.       int[][] left = new int[board.length][board[0].length];
  9.       for (int i = 0; i < board.length; i++) {
  10.         int start = 0;
  11.         for (int j = 0; j < board[0].length; j++) {
  12.           if (board[i][j] != '#') {
  13.             start = 0;
  14.             left[i][j] = 0;
  15.           } else {
  16.             left[i][j] = start;
  17.             start++;
  18.           }
  19.         }
  20.       }
  21.       int[][] right = new int[board.length][board[0].length];
  22.       int[][] center = new int[board.length][board[0].length];
  23.       for (int i = 0; i < board.length; i++) {
  24.         int start = 0;
  25.         for (int j = board[0].length - 1; j >= 0; j--) {
  26.           if (board[i][j] != '#') {
  27.             start = 0;
  28.             right[i][j] = 0;
  29.             center[i][j] = 0;
  30.           } else {
  31.             right[i][j] = start;
  32.             start++;
  33.             center[i][j] = 2 * Math.min(left[i][j], right[i][j]) + 1;
  34.           }
  35.         }
  36.       }
  37.       int[][] up = new int[board.length][board[0].length];
  38.       for (int i = 0; i < board.length; i++) {
  39.         int start = 0;
  40.         for (int j = 0; j < board[0].length; j++) {
  41.           if (board[i][j] == '#') {
  42.             if (i == 0 || up[i - 1][j] == 0) {
  43.               up[i][j] = 1;
  44.             } else {
  45.               up[i][j] = Math.min(center[i][j], up[i - 1][j] + 2);
  46.             }
  47.           } else {
  48.             up[i][j] = 0;
  49.           }
  50.         }
  51.       }
  52.       int[][] down = new int[board.length][board[0].length];
  53.       int best = 0;
  54.       for (int i = board.length - 1; i >= 0; i--) {
  55.         int start = 0;
  56.         for (int j = 0; j < board[0].length; j++) {
  57.           if (board[i][j] == '#') {
  58.             if (i == board.length - 1 || down[i + 1][j] == 0) {
  59.               down[i][j] = 1;
  60.             } else {
  61.               down[i][j] = Math.min(center[i][j], down[i + 1][j] + 2);
  62.             }
  63.             best = Math.max(best, Math.min(up[i][j], down[i][j]));
  64.           } else {
  65.             down[i][j] = 0;
  66.           }
  67.         }
  68.       }
  69.       System.out.println(best);
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement