2469번 사다리 타기
🅰 설계
1. 어떤 방법을 사용할 것인가?
사다리 문제는 가로 줄 하나에 상태 두개가 swap된다는 것을 알고 있으면 좋다.
Wildcard가 되는 줄을 line
이라고 하면,
- 0부터
line
전 까지 map을 돌면서 status를 갱신한 것과 - n-1부터
line
전 까지 map을 돌면서 status를 갱신한 것
이 두가지가 같은 위치에서 상태가 다른지를 보면서 답을 구해나가면 된다.
2. status 구하는 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
String in = br.readLine();
for(int i=0;i<in.length();i++){
toup[i] = in.charAt(i)-'A';
todown[i] = i;
}
for(int i=0;i<n;i++) {
map[i] = br.readLine().toCharArray();
if(map[i][0] == '?') line = i;
}
// todown
for(int i=0;i<line;i++){
for(int j=0;j<k-1;j++){
if(map[i][j] == '-') swap(todown,j,j+1);
}
}
// to up
for(int i=n-1;i>line;i--){
for(int j=0;j<k-1;j++){
if(map[i][j] == '-') swap(toup,j,j+1);
}
}
|
cs |
todown
은 0부터 시작해서 line
까지 status를 계산하고,
toup
은 n-1부터 시작해서 line
까지 status를 계산한다.
그리고 todown
과 toup
이 같지 않으면 swap하고, 같으면 swap하지 않는다.
3. 전체 코드
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
|
import java.util.*;
import java.io.*;
public class 사다리타기 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int k,n,line;
static int[] todown,toup;
static char[][] map;
static char[] ans;
public static void main(String[] args) throws IOException{
k = Integer.parseInt(br.readLine()); // col,start
n = Integer.parseInt(br.readLine()); // row
todown = new int[k];
toup = new int[k];
map = new char[n][k-1];
ans = new char[k-1];
String in = br.readLine();
for(int i=0;i<in.length();i++){
toup[i] = in.charAt(i)-'A';
todown[i] = i;
}
for(int i=0;i<n;i++) {
map[i] = br.readLine().toCharArray();
if(map[i][0] == '?') line = i;
}
for(int i=0;i<line;i++){
for(int j=0;j<k-1;j++){
if(map[i][j] == '-') swap(todown,j,j+1);
}
}
for(int i=n-1;i>line;i--){
for(int j=0;j<k-1;j++){
if(map[i][j] == '-') swap(toup,j,j+1);
}
}
for(int i=0;i<k-1;i++){
if(i>0 && ans[i-1] == '-'){
ans[i] = '*';
continue;
}
if(todown[i] != toup[i]){
ans[i] = '-';
swap(todown,i,i+1);
}else{
ans[i] = '*';
}
}
for(int i=0;i<k;i++){
if(todown[i] != toup[i]){
for(int j=0;j<k-1;j++) ans[j] = 'x';
break;
}
}
for(int i=0;i<k-1;i++) System.out.print(ans[i]);
}
static void swap(int[] arr,int i,int j){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
|
cs |
✅ 후기
처음에 모든 경우를 하는 Bruteforce 방법을 생각했는데, 시간제한도 조금 걸리고 구현도 잘못했는지 답이 안나왔다.
실버문제중에 어려운 것들이 있는데 이것도 그런것중 하나였던 것 같았다.
'Problem Solving > Baekjoon' 카테고리의 다른 글
[ACMICPC] 1717 . 집합의 표현 (0) | 2021.08.12 |
---|---|
[ACMICPC] 3005 . 크로스워드 퍼즐 쳐다보기 (0) | 2021.08.10 |
[ACMICPC] 9019 . DSLR (0) | 2021.08.09 |
[ACMICPC] 14938 . 서강 그라운드 (0) | 2021.08.08 |
[ACMICPC] 10250 . ACM 호텔 (0) | 2021.08.07 |