CS/알고리즘_삼성 SW 역량 테스트 기출 문제

삼성 SW 역량 테스트 기출 문제 : 구슬 탈출 2

Jedy_Kim 2021. 10. 11. 15:20
728x90

https://www.acmicpc.net/problem/13460

 

13460번: 구슬 탈출 2

첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B'

www.acmicpc.net

 

// 코드

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main{
  
  static int N, M;
  static char[][] map;
  
  static int[] dy = {-1100};
  static int[] dx = {00-11};
  
  static boolean[][][][] visited;
  
  static class Ball {
    int[] Red;
    int[] Blue;
    int move;
    
    public Ball() { this.move = 0; }
    
    public Ball(Ball now) {
      this.Red  = now.Red;
      this.Blue = now.Blue;
      this.move = now.move;
    }
    
  }
  
  // main
  public static void main(String[] args) throws Exception {
    boolean isPossible = false;
    BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br  = new BufferedReader(new InputStreamReader(System.in)); 
    StringTokenizer st = new StringTokenizer(br.readLine());
    StringBuilder sb   = new StringBuilder();
    
    N = Integer.parseInt(st.nextToken());
    M = Integer.parseInt(st.nextToken());
    
    // '.' : 빈 칸
    // '#' : 벽
    // 'O' : 구멍의 위치
    // 'R' : 빨간 구슬의 위치
    // 'B' : 파란 구슬의 위치
    map = new char[N][M];
    Ball b = new Ball();
    
    for(int i=0; i<N; i++) {
      String str = br.readLine();
      for(int j=0; j<M; j++) {
        map[i][j] = str.charAt(j);
        if(map[i][j] == 'R') {
          b.Red = new int[]{i, j};
          map[i][j] = '.';
        } else if(map[i][j] == 'B') {
          b.Blue = new int[]{i, j};
          map[i][j] = '.';
        }
      }
    }
    
    visited = new boolean[N][M][N][M];
    Deque<Ball> deq = new ArrayDeque<>();
    deq.offer(b);
    visited[b.Red[0]][b.Red[1]][b.Blue[0]][b.Blue[1]] = true;
    
    while(!deq.isEmpty()) {
      
      Ball now = deq.pop();
      
      // 실패
      if(map[now.Blue[0]][now.Blue[1]] == 'O'continue;
      else if(now.move == 11continue;
      
      // 성공
      if( map[now.Red[0]][now.Red[1]] == 'O' ) {
        sb.append(now.move + "\n");
        isPossible = true;
        break;
      }
      
      for(int i=0; i<4; i++) {
        Ball next = move(i, now);
        if(visited[next.Red[0]][next.Red[1]][next.Blue[0]][next.Blue[1]]) continue;
        
        visited[next.Red[0]][next.Red[1]][next.Blue[0]][next.Blue[1]] = true;
        deq.offer(next);
      }
    }
    
    if(isPossible) bw.write(sb.toString());
    else bw.write(String.valueOf(-1));
    
    br.close();
    bw.flush();
    bw.close();
  }
  
  static Ball move(int dir, Ball now) {
    
    Ball next = new Ball(now);
    int ny = 0, nx = 0;
    // 빨간공이 더 먼저 움직여야하는 상황이라면 true, 파란공이 먼저인 경우는 false
    boolean flag = false;
    
    switch(dir) { 
    // 상
    case 0:
      // 파란공이 더 위에 있다면 위에있는 파란공이 먼저 움직인다.
      if(next.Red[0> next.Blue[0]) flag = false;
      // 빨간공이 더 위에 있다면 빨간공이 먼저 움직인다.
      else flag = true;
    break;
    
    // 하
    case 1:
      // 빨간공이 더 아래에 있다면 아래에 있는 빨간공이 먼저 움직인다.
      if(next.Red[0> next.Blue[0]) flag = true;
      else flag = false;
    break;
    
    // 좌
    case 2:
      // 파란공이 더 왼쪽에 있으므로, 파란공이 먼저 움직인다.
      if(next.Red[1> next.Blue[1]) flag = false;
      // 빨간공이 더 오른쪽에 있으므로, 빨간공이 먼저 움직인다.
      else flag = true;
    break;
    
    // 우
    case 3:
      // 빨간공이 더 오른쪽에 있으므로, 빨간공이 먼저 움직인다.
      if(next.Red[1> next.Blue[1]) flag = true;
      // 파란공이 더 왼쪽에 있으므로, 파란공이 먼저 움직인다.
      else flag = false;
    break
    } // end of switch
    
    // 파란공이 먼저 움직이는 경우
    if(!flag) {
      // 파란공이 움직인다..
      ny = next.Blue[0+ dy[dir];
      nx = next.Blue[1+ dx[dir];
      
      while(map[ny][nx] == '.') {
        ny += dy[dir];
        nx += dx[dir];
      }
      
      if(map[ny][nx] == 'O') next.Blue = new int[] { ny, nx };
      else next.Blue = new int[] { (ny - dy[dir]), (nx - dx[dir]) };
      
      // 빨간공이 움직인다..
      ny = next.Red[0+ dy[dir];
      nx = next.Red[1+ dx[dir];
      
      while(map[ny][nx] == '.') {
        // 파란공과 위치가 겹치는 경우 탈출한다.
        if (ny == next.Blue[0&& nx == next.Blue[1]) break;
        
        ny += dy[dir];
        nx += dx[dir];
      }
      
      if(map[ny][nx] == 'O') next.Red = new int[] {ny, nx};
      else next.Red = new int[] { (ny - dy[dir]), (nx - dx[dir]) };
      
    }
    // 빨간공이 먼저 움직이는 경우
    else {
      // 빨간공이 움직인다..
      ny = next.Red[0+ dy[dir];
      nx = next.Red[1+ dx[dir];
      
      while(map[ny][nx] == '.') {
        ny += dy[dir];
        nx += dx[dir];
      }
      
      if(map[ny][nx] == 'O')  next.Red = new int[] { ny, nx };
      else next.Red = new int[] { (ny - dy[dir]), (nx - dx[dir]) };
      
      // 파란공이 움직인다..
      ny = next.Blue[0+ dy[dir];
      nx = next.Blue[1+ dx[dir];
      
      while(map[ny][nx] == '.') {
        // 빨간공과 위치가 겹치는 경우 탈출한다.
        if (ny == next.Red[0&& nx == next.Red[1]) break;
        
        ny += dy[dir];
        nx += dx[dir];
      }
      
      if(map[ny][nx] == 'O')  next.Blue = new int[] { ny, nx };
      else next.Blue = new int[] { (ny - dy[dir]), (nx - dx[dir]) };
    }
    
    next.move++;
    return next;
  }
  
}
cs

 

 

반응형