문제/백준

백준 8972번 미친 아두이노 [JAVA]

javaju 2021. 10. 19. 20:44
 

8972번: 미친 아두이노

요즘 종수는 아두이노를 이용해 "Robots"이라는 게임을 만들었다. 종수는 아두이노 한대를 조정하며, 미친 아두이노를 피해다녀야 한다. 미친 아두이노는 종수의 아두이노를 향해 점점 다가온다.

www.acmicpc.net

 

풀이 방법

입력으로 주어진 방향대로 종수가 움직였을 때, 보드의 상태를 구하는 프로그램을 구하는 문제였습니다.

 

  1. 먼저, 종수가 아두이노를 8가지 방향(수직, 수평, 대각선)으로 이동시키거나, 그 위치에 그대로 놔둔다.
  2. 종수의 아두이노 위치 =  미친 아두이노 위치 게임 오버
  3. 종수의 위치를 (r1,s1), 미친 아두이노의 위치를 (r2, s2)라고 했을 때, |r1-r2| + |s1-s2|가 가장 작아지는 방향으로 이동
  4. 종수의 아두이노 위치 =  미친 아두이노 위치 게임 오버
  5. 2개 또는 그 이상의 미친 아두이노가 같은 칸에 있는 경우에는 큰 폭발이 일어나고, 그 칸에 있는 아두이노는 모두 파괴된다.

 저는 종수의 위치를 넣을 리스트와 미친 아두이노의 위치 정보를 넣을 리스트를 이용해 종수 아두이노 먼저 움직이고 난 뒤, 종수의 아두이노 위치와 미친 아두이노의 위치가 같은 지 확인해 주었습니다.

그 후 종수의 위치를 기반으로 미친 아두이노와 거리가 가장 짧아 지는 방향으로 이동해 시켜 준 뒤, 마찬가지로 종수의 아두이노 위치와 미친 아두이노의 위치가 같은 지 확인해 주었습니다. 미친 아두이노를 다 움직이고 난 뒤, 겹친 아두이노는 폭발시켜 없애주고 입력으로 주어진만큼 이 과정을 반복해주었습니다.

 

코드

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class Main_BJ_8972_미친아두이노 {
    
    static class info{
        int x, y;
 
        public info(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        
    }
    
    static int R,C;
    static char [][] map;
    
    static Queue<info> jongsoo = new LinkedList<>();
    static Queue<info> crazy = new LinkedList<>();
    
    static int [] dx = {0,1,1,1,0,0,0,-1,-1,-1};
    static int [] dy = {0,-1,0,1,-1,0,1,-1,0,1};
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        
        st = new StringTokenizer(br.readLine());
        
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        
        map = new char[R][C];
        
        for(int i=0;i<R;i++) {
            String command = br.readLine();
            for(int j=0;j<C;j++) {
                map[i][j] = command.charAt(j);
                if(map[i][j]=='I') jongsoo.add(new info(i,j));
                else if(map[i][j]=='R') crazy.add(new info(i,j));
            }
        }
        
        int count=1;
        
        boolean flag = true;
        String direction = br.readLine();
        for(int i=0;i<direction.length();i++) {
            if(!jongsoo_move(direction.charAt(i)-48)) {
                flag = false;
                break;
            }
            if(!crazy_move()) {
                flag = false;
                break;
            }
            map();
            count++;
        }
        if(!flag) System.out.println("kraj "+ count);
        else print();
    }
    
    
    public static boolean jongsoo_move(int dir) {
        info temp = jongsoo.poll();
        
        int nx = temp.x + dx[dir];
        int ny = temp.y + dy[dir];
 
        if(map[nx][ny]=='R'return false;
        else {
            jongsoo.add(new info(nx,ny));
            if(dir!=5) {
                map[nx][ny] = map[temp.x][temp.y];
                map[temp.x][temp.y] = '.';
            }
            return true;
        }
    }
    
    public static boolean crazy_move() {
        int[][] temp = new int[R][C];
        
        info js = jongsoo.peek();
        
        int jsX = js.x;
        int jsY = js.y;
        
        while(!crazy.isEmpty()) {
            
            info cr = crazy.poll(); 
            
            int len = Integer.MAX_VALUE;
            int dir = 0;
            for(int j=1;j<10;j++) {
                if(j==5continue;
                int nx = cr.x+dx[j];
                int ny = cr.y+dy[j];
                
                if(nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
                
                int distance = Math.abs(jsX-nx) + Math.abs(jsY-ny);
                
                if(distance<len) {
                    len = distance;
                    dir = j;
                }
            }
            
            int moveX = cr.x+dx[dir];
            int moveY = cr.y+dy[dir];
            
            
            if(map[moveX][moveY]=='I') {
                return false;
            }
            
            temp[moveX][moveY] += 1;
        }
        for(int i = 0; i < R; i++) {
            for(int j = 0; j < C; j++) {
                if(temp[i][j] == 1) {
                    crazy.add(new info(i, j));
                }
            }
        }
        
        return true;
    }
    
    public static void map() {
        for(int i=0;i<R;i++) Arrays.fill(map[i], '.');
        
        info js = jongsoo.peek();
        
        map[js.x][js.y] = 'I';
        
        for(int i=0;i<crazy.size();i++) {
            info cr = crazy.poll();
            map[cr.x][cr.y] = 'R';
            crazy.add(new info(cr.x, cr.y));
        }
    }
    
    public static void print() {
        for(int i=0;i<R;i++) {
            for(int j=0;j<C;j++) {
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
 
}
 
cs