Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner sc = new Scanner();
- int t = sc.nextInt();
- for (int currentCase = 0; currentCase < t; currentCase++) {
- int r = sc.nextInt();
- sc.nextInt();
- char[][] board = sc.nextGrid(r);
- int[][] left = new int[board.length][board[0].length];
- for (int i = 0; i < board.length; i++) {
- int start = 0;
- for (int j = 0; j < board[0].length; j++) {
- if (board[i][j] != '#') {
- start = 0;
- left[i][j] = 0;
- } else {
- left[i][j] = start;
- start++;
- }
- }
- }
- int[][] right = new int[board.length][board[0].length];
- int[][] center = new int[board.length][board[0].length];
- for (int i = 0; i < board.length; i++) {
- int start = 0;
- for (int j = board[0].length - 1; j >= 0; j--) {
- if (board[i][j] != '#') {
- start = 0;
- right[i][j] = 0;
- center[i][j] = 0;
- } else {
- right[i][j] = start;
- start++;
- center[i][j] = 2 * Math.min(left[i][j], right[i][j]) + 1;
- }
- }
- }
- int[][] up = new int[board.length][board[0].length];
- for (int i = 0; i < board.length; i++) {
- int start = 0;
- for (int j = 0; j < board[0].length; j++) {
- if (board[i][j] == '#') {
- if (i == 0 || up[i - 1][j] == 0) {
- up[i][j] = 1;
- } else {
- up[i][j] = Math.min(center[i][j], up[i - 1][j] + 2);
- }
- } else {
- up[i][j] = 0;
- }
- }
- }
- int[][] down = new int[board.length][board[0].length];
- int best = 0;
- for (int i = board.length - 1; i >= 0; i--) {
- int start = 0;
- for (int j = 0; j < board[0].length; j++) {
- if (board[i][j] == '#') {
- if (i == board.length - 1 || down[i + 1][j] == 0) {
- down[i][j] = 1;
- } else {
- down[i][j] = Math.min(center[i][j], down[i + 1][j] + 2);
- }
- best = Math.max(best, Math.min(up[i][j], down[i][j]));
- } else {
- down[i][j] = 0;
- }
- }
- }
- System.out.println(best);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement