답안 #582483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582483 2022-06-23T22:44:34 Z coderInTraining Tracks in the Snow (BOI13_tracks) Java 11
0 / 100
147 ms 12424 KB
import java.util.*;

class Main {
    public static void main (String[]args) {
        Scanner scan = new Scanner (System.in);

        int rows = scan.nextInt();
        int columns = scan.nextInt();

        char [][] grid = new char [rows][columns];

        for (int i = 0; i < rows; i++) {
            String line = scan.next();

            for (int j = 0; j < columns; j++) {
                grid[i][j] = line.charAt(j);
            }
        }

        int [][] distances = new int [rows][columns];

        for (int i = 0; i < rows; i++)  {
            Arrays.fill(distances[i], Integer.MAX_VALUE);
        }

        distances[0][0] = 1;

        Queue <Pair> queue = new LinkedList <Pair>();
        Pair startingPair = new Pair (0, 0);
        queue.add(startingPair);

        while (queue.isEmpty() == false) {
            Pair curPair = queue.poll();
            int cRow = curPair.row;
            int cColumn = curPair.column;
            int curDist = distances[cRow][cColumn];

            char curChar = grid[cRow][cColumn];

            if (cRow - 1 >= 0 && grid[cRow - 1][cColumn] != '.') {
                if (grid[cRow - 1][cColumn] == curChar) {
                    if (curDist < distances[cRow - 1][cColumn]) {
                        distances[cRow - 1][cColumn] = curDist;
                        Pair addingPair = new Pair (cRow - 1, cColumn);
                        queue.add(addingPair);
                    }
                }
                else {
                    if (curDist + 1 < distances[cRow - 1][cColumn]) {
                        distances[cRow - 1][cColumn] = curDist + 1;
                        Pair addingPair = new Pair (cRow - 1, cColumn);
                        queue.add(addingPair);
                    }
                }
            }

            if (cColumn + 1 < columns && grid[cRow][cColumn + 1] != '.') {
                if (grid[cRow][cColumn + 1] == curChar) {
                    if (curDist < distances[cRow][cColumn + 1]) {
                        distances[cRow][cColumn + 1] = curDist;
                        Pair addingPair = new Pair (cRow, cColumn + 1);
                        queue.add(addingPair);
                    }
                }
                else {
                    if (curDist + 1 < distances[cRow][cColumn + 1]) {
                        distances[cRow][cColumn + 1] = curDist + 1;
                        Pair addingPair = new Pair (cRow, cColumn + 1);
                        queue.add(addingPair);
                    }
                }
            }

            if (cRow + 1 < rows && grid[cRow + 1][cColumn] != '.') {
                if (grid[cRow + 1][cColumn] == curChar) {
                    if (curDist < distances[cRow + 1][cColumn]) {
                        distances[cRow + 1][cColumn] = curDist;
                        Pair addingPair = new Pair (cRow + 1, cColumn);
                        queue.add(addingPair);
                    }
                }
                else {
                    if (curDist + 1 < distances[cRow + 1][cColumn]) {
                        distances[cRow + 1][cColumn] = curDist + 1;
                        Pair addingPair = new Pair (cRow + 1, cColumn);
                        queue.add(addingPair);
                    }
                }
            }

            if (cColumn - 1 >= 0 && grid[cRow][cColumn - 1] != '.') {
                if (grid[cRow][cColumn - 1] == curChar) {
                    if (curDist < distances[cRow][cColumn - 1]) {
                        distances[cRow][cColumn - 1] = curDist;
                        Pair addingPair = new Pair (cRow, cColumn - 1);
                        queue.add(addingPair);
                    }
                }
                else {
                    if (curDist + 1 < distances[cRow][cColumn - 1]) {
                        distances[cRow][cColumn - 1] = curDist + 1;
                        Pair addingPair = new Pair (cRow, cColumn - 1);
                        queue.add(addingPair);
                    }
                }
            }
        }

        int maxDist = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {

                if (grid[i][j] == '.') {
                    continue;
                }

                maxDist = Math.max(maxDist, distances[i][j]);
            }
        }

        System.out.println(maxDist);
    }

    private static class Pair {
        int row;
        int column;

        public Pair (int row, int column) {
            this.row = row;
            this.column = column;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 144 ms 12064 KB Execution failed because the return code was nonzero
2 Runtime error 133 ms 12104 KB Execution failed because the return code was nonzero
3 Runtime error 132 ms 12300 KB Execution failed because the return code was nonzero
4 Runtime error 132 ms 12216 KB Execution failed because the return code was nonzero
5 Runtime error 129 ms 12112 KB Execution failed because the return code was nonzero
6 Runtime error 131 ms 12344 KB Execution failed because the return code was nonzero
7 Runtime error 133 ms 12088 KB Execution failed because the return code was nonzero
8 Runtime error 131 ms 12072 KB Execution failed because the return code was nonzero
9 Runtime error 139 ms 11996 KB Execution failed because the return code was nonzero
10 Runtime error 143 ms 12060 KB Execution failed because the return code was nonzero
11 Runtime error 143 ms 11992 KB Execution failed because the return code was nonzero
12 Runtime error 131 ms 11824 KB Execution failed because the return code was nonzero
13 Runtime error 142 ms 12056 KB Execution failed because the return code was nonzero
14 Runtime error 134 ms 11880 KB Execution failed because the return code was nonzero
15 Runtime error 135 ms 12276 KB Execution failed because the return code was nonzero
16 Runtime error 143 ms 11932 KB Execution failed because the return code was nonzero
17 Runtime error 133 ms 12080 KB Execution failed because the return code was nonzero
18 Runtime error 137 ms 12424 KB Execution failed because the return code was nonzero
# 결과 실행 시간 메모리 Grader output
1 Runtime error 147 ms 11948 KB Execution failed because the return code was nonzero
2 Runtime error 136 ms 12032 KB Execution failed because the return code was nonzero
3 Runtime error 134 ms 12016 KB Execution failed because the return code was nonzero
4 Runtime error 132 ms 12092 KB Execution failed because the return code was nonzero
5 Runtime error 133 ms 12216 KB Execution failed because the return code was nonzero
6 Runtime error 135 ms 12140 KB Execution failed because the return code was nonzero
7 Runtime error 129 ms 12196 KB Execution failed because the return code was nonzero
8 Runtime error 131 ms 12096 KB Execution failed because the return code was nonzero
9 Runtime error 136 ms 12124 KB Execution failed because the return code was nonzero
10 Runtime error 131 ms 12192 KB Execution failed because the return code was nonzero
11 Runtime error 133 ms 12300 KB Execution failed because the return code was nonzero
12 Runtime error 131 ms 12132 KB Execution failed because the return code was nonzero
13 Runtime error 132 ms 11984 KB Execution failed because the return code was nonzero
14 Runtime error 132 ms 12380 KB Execution failed because the return code was nonzero
15 Runtime error 132 ms 12108 KB Execution failed because the return code was nonzero
16 Runtime error 132 ms 12208 KB Execution failed because the return code was nonzero
17 Runtime error 132 ms 12064 KB Execution failed because the return code was nonzero
18 Runtime error 133 ms 12168 KB Execution failed because the return code was nonzero
19 Runtime error 136 ms 12104 KB Execution failed because the return code was nonzero
20 Runtime error 132 ms 12144 KB Execution failed because the return code was nonzero
21 Runtime error 131 ms 12160 KB Execution failed because the return code was nonzero
22 Runtime error 141 ms 12088 KB Execution failed because the return code was nonzero
23 Runtime error 133 ms 12088 KB Execution failed because the return code was nonzero
24 Runtime error 132 ms 12364 KB Execution failed because the return code was nonzero
25 Runtime error 137 ms 12176 KB Execution failed because the return code was nonzero
26 Runtime error 126 ms 12356 KB Execution failed because the return code was nonzero
27 Runtime error 141 ms 12068 KB Execution failed because the return code was nonzero
28 Runtime error 137 ms 12136 KB Execution failed because the return code was nonzero
29 Runtime error 142 ms 11928 KB Execution failed because the return code was nonzero
30 Runtime error 133 ms 12184 KB Execution failed because the return code was nonzero
31 Runtime error 133 ms 11988 KB Execution failed because the return code was nonzero
32 Runtime error 133 ms 12052 KB Execution failed because the return code was nonzero