본문 바로가기

BOJ/JAVA

보물섬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
 
public class Main {
    static int n, m, ans = 0;
    static int map[][] = new int[51][51];
    static boolean visited[][] = new boolean[51][51];
    static int dp[][] = new int[51][51];
    static final int INF = 987654321;
    static int dx[] = {-1,1,0,0}, dy[] = {0,0,-1,1};
 
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st= new StringTokenizer(br.readLine());
 
        n = Integer.parseInt(st.nextToken()); //물건의 개수 
        m = Integer.parseInt(st.nextToken()); //비교쌍의 개수 
 
        for (int i = 0; i < n; i++) {
            String s = br.readLine();
            for (int j = 0; j < m; j++) {
                char c = s.charAt(j);
                if (c == 'L')
                    map[i][j] = 1;
                else map[i][j] = 0;
            }
        }
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                dp[i][j] = INF;
            }
        }
 
        //        for (int i = 0; i < n; i++) {
        //            for (int j = 0; j < m; j++) {
        //                System.out.print(map[i][j] + " ");
        //            }
        //            System.out.println();
        //        }
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 1) {
                    dijkstra(i, j);
                    calc();
                }
            }
        }
 
        //        for (int i = 0; i < n; i++) {
        //            for (int j = 0; j < m; j++) {
        //                if (dp[i][j] == INF) System.out.print("- ");
        //                else System.out.print(dp[i][j] + " ");
        //            }
        //            System.out.println();
        //        }
        System.out.println(ans);
 
    }
    static void calc() {
        //         System.out.println();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
//                                if (dp[i][j] == INF) System.out.print("- ");
//                                else System.out.print(dp[i][j] + " ");
                if (dp[i][j] != INF)
                    ans = ans < dp[i][j] ? dp[i][j] : ans;
                dp[i][j] = INF;
                visited[i][j] = false;
            }
//                        System.out.println();
        }
//                 System.out.println();
    }
 
    static void dijkstra(int x, int y) {
        PriorityQueue<Pos> q = new PriorityQueue<>();
 
        q.add(new Pos(x, y, 0));
 
        dp[x][y] = 0;
 
        while(!q.isEmpty()) {
            int cx = q.peek().x;
            int cy = q.peek().y;
            q.poll();
 
            if (visited[cx][cy]) continue;
            visited[cx][cy] = true;
 
            for (int k = 0; k < 4; k++) {
                int nx = cx + dx[k];
                int ny = cy + dy[k];
 
                if (nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny] || map[nx][ny] == 0continue;
 
                if (dp[nx][ny] > dp[cx][cy] + 1) {
                    dp[nx][ny] = dp[cx][cy] + 1;
                    q.add(new Pos(nx, ny, dp[nx][ny]));
                }
            }
        }
    }
 
    static class Pos implements Comparable<Pos>{
        int x, y;
        int dist;
 
        public Pos(int x, int y, int dist) {
            this.x =x;
            this.y = y;
            this.dist = dist;
        }
 
        @Override
        public int compareTo(Pos o) {
            // TODO Auto-generated method stub
            if (this.dist > o.dist) return 1;
            else if (this.dist < o.dist) return -1;
            else return 0;
        }
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'BOJ > JAVA' 카테고리의 다른 글

숨바꼭질  (0) 2020.02.17
영역구하기  (0) 2020.02.17
부분합  (0) 2020.02.17
한 줄로 서기  (0) 2020.02.16
좋은 수열  (0) 2020.02.16