Lined Notebook

[Gold 5] [Java] 인내의 도미노 장인 호석 (20165번)

by HeshAlgo

Java Code

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
package com.baekjoon.java;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
    static String[] input;
    static int N, M, R, answer;
    static int[][] map;
    static boolean[][] check;
    static int[] dx = {001-1};
    static int[] dy = {1-100};
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);        // 행 개수
        M = Integer.parseInt(input[1]);        // 열 개수
        R = Integer.parseInt(input[2]);        // 라운드 횟수
        map = new int[N + 1][M + 1];                // 도미노 맵
        check = new boolean[N + 1][M + 1];
        
        // 도미노 정보 입력
        for (int row = 1; row <= N; row++) {
            input = br.readLine().split(" ");
            for (int col = 1; col <= M; col++) {
                map[row][col] = Integer.parseInt(input[col - 1]);
            }
        }
        
        for (int i = 0; i < R * 2; i++) {
            input = br.readLine().split(" ");
            // 공격 정보
            if (i % 2 == 0) {
                int attack_X = Integer.parseInt(input[0]);
                int attack_Y = Integer.parseInt(input[1]);
                int dir = 0;
                
                switch (input[2]) {
                case "W":
                    dir = 1;
                    break;
                case "S":
                    dir = 2;
                    break;
                case "N":
                    dir = 3;
                    break;
                };
                
                // 도미노가 이미 쓰러져 있는 곳을 공격할 경우
                if (check[attack_X][attack_Y]) continue;
                        
                answer++;
                check[attack_X][attack_Y] = true;
                attack_domino(attack_X, attack_Y, dir, 1, map[attack_X][attack_Y]);
            
            }
            
            // 방어 정보
            else {
                int defense_X = Integer.parseInt(input[0]);
                int defense_Y = Integer.parseInt(input[1]);    
                
                // 넘어지지 않은 도미노를 세우려고 하는 경우
                if (!check[defense_X][defense_Y]) continue;
                check[defense_X][defense_Y] = false;
                
            }
            
        }
        
        // 결과 출력
        print();
        
    }
 
    private static void print() {
        System.out.println(answer);
        
        for (int row = 1; row <= N; row++) {
            for (int col = 1; col <= M; col++) {
                // 도미노가 넘어진 것
                if (check[row][col]) {
                    System.out.print("F ");
                } 
                // 도미노가 세워진 것
                else {
                    System.out.print("S ");
                }
            }
            System.out.println();
        }
    }
 
    private static void attack_domino(int x, int y, int dir, int cnt, int height) {
        if (cnt == height) {
            return;
        }
        
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        
        if (0 < nx && nx <= N && 0 < ny && ny <= M) {
            attack_domino(nx, ny, dir, cnt + 1, height);
            
            // 연쇄 작용이 일어나기 위한 재귀
            if (!check[nx][ny]) {
                attack_domino(nx, ny, dir, 1, map[nx][ny]);
            }
 
            // 도미노가 쓰러져 있지 않은 경우
            if (!check[nx][ny]) {
                check[nx][ny] = true;
                answer++;
                
            }
        }
        
    }
 
 
}
 
 
cs

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기