[백준/JAVA] 20436번: ZOAC 3
📖 문제
20436번: ZOAC 3
첫 번째 줄에는 두 알파벳 소문자 sL, sR이 주어진다. sL, sR은 각각 왼손 검지손가락, 오른손 검지손가락의 처음 위치이다. 그 다음 줄에는 알파벳 소문자로 구성된 문자열이 주어진다. 문자열의
www.acmicpc.net
💡 풀이 방식
• 구현 & 시뮬레이션
1. 키보드 객체 배열을 만든다.
Keyboard(키보드 문자, 어떤 손인지, 행 위치, 열 위치)
2. 두 문자를 입력받는다.
😱주의할 점!
입력받은 두 문자가 무조건 (왼쪽 자판에 있는 값, 오른쪽 자판에 있는 값) 순서로 있는 게 아니므로, 어느 쪽에 있는 문자인지 확인해 두 검지의 위치값을 저장한다.
3. 입력받은 문자열을 돌며 택시 거리를 찾는다.
- 왼쪽 자판에 있는 문자인 경우, 현재까지 걸린 시간에 택시 거리 + 1을 더해 걸린 시간을 갱신한다. 왼쪽 검지 위치도 찾은 문자열 위치로 갱신한다.
- 오른쪽 자판에 있는 문자인 경우도 동일하게 진행한다.
💥 유의사항
자꾸 초반에 틀린다면 이 경우를 생각해야 한다.
손의 위치를 입력받을 때 (왼손 검지, 오른손 검지) 순으로 입력받는 것이 아닐 수도 있다는 것.
글 읽기 - [자바]반례 알려주실 분 구합니다... 어디서 틀린걸까요
댓글을 작성하려면 로그인해야 합니다.
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
|
import java.util.*;
import java.io.*;
public class Main {
static Keyboard[] qwerty = new Keyboard[30];
static String pos;
static char[] input;
static int[] left, right;
static int time = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
makeKeyboard();
pos = br.readLine();
left = new int[2];
right = new int[2];
for(int j = 0 ; j < 30 ; j++) {
if(pos.charAt(0) == qwerty[j].c) {
// 입력받은 두 문자가 무조건 (왼쪽 자판에 있는 값, 오른쪽 자판에 있는 값) 순으로 있는 게 아니므로
// 어느 쪽에 있는 문자인지 확인해 위치값 저장
if(qwerty[j].hand == 'l')
left = new int[] {qwerty[j].x, qwerty[j].y};
else
right = new int[] {qwerty[j].x, qwerty[j].y};
break;
}
}
for(int j = 0 ; j < 30 ; j++) {
if(pos.charAt(2) == qwerty[j].c) {
// 입력받은 두 문자가 무조건 (왼쪽 자판에 있는 값, 오른쪽 자판에 있는 값) 순으로 있는 게 아니므로
// 어느 쪽에 있는 문자인지 확인해 위치값 저장
if(qwerty[j].hand == 'l')
left = new int[] {qwerty[j].x, qwerty[j].y};
else
right = new int[] {qwerty[j].x, qwerty[j].y};
break;
}
}
input = br.readLine().toCharArray();
for(int i = 0 ; i < input.length ; i++) {
// 문자열의 위치와 손가락 찾기
char h = 'x';
int[] findPos = new int[2];
for(int j = 0 ; j < 30 ; j++) {
if(input[i] == qwerty[j].c) {
findPos = new int[] {qwerty[j].x, qwerty[j].y};
h = qwerty[j].hand;
break;
}
}
if(h == 'l') { // 왼쪽 자판에 있는 문자인 경우
int dist = Math.abs(left[0] - findPos[0]) + Math.abs(left[1] - findPos[1]);
time += dist + 1;
left = findPos; // 왼쪽 검지 위치 갱신
}
else if(h == 'r') { // 오른쪽 자판에 있는 문자인 경우
int dist = Math.abs(right[0] - findPos[0]) + Math.abs(right[1] - findPos[1]);
time += dist + 1;
right = findPos; // 오른쪽 검지 위치
}
}
System.out.println(time);
}
private static void makeKeyboard() {
qwerty[0] = new Keyboard('q', 'l', 0, 0);
qwerty[1] = new Keyboard('w', 'l', 0, 1);
qwerty[2] = new Keyboard('e', 'l', 0, 2);
qwerty[3] = new Keyboard('r', 'l', 0, 3);
qwerty[4] = new Keyboard('t', 'l', 0, 4);
qwerty[5] = new Keyboard('y', 'r', 0, 5);
qwerty[6] = new Keyboard('u', 'r', 0, 6);
qwerty[7] = new Keyboard('i', 'r', 0, 7);
qwerty[8] = new Keyboard('o', 'r', 0, 8);
qwerty[9] = new Keyboard('p', 'r', 0, 9);
qwerty[10] = new Keyboard('a', 'l', 1, 0);
qwerty[11] = new Keyboard('s', 'l', 1, 1);
qwerty[12] = new Keyboard('d', 'l', 1, 2);
qwerty[13] = new Keyboard('f', 'l', 1, 3);
qwerty[14] = new Keyboard('g', 'l', 1, 4);
qwerty[15] = new Keyboard('h', 'r', 1, 5);
qwerty[16] = new Keyboard('j', 'r', 1, 6);
qwerty[17] = new Keyboard('k', 'r', 1, 7);
qwerty[18] = new Keyboard('l', 'r', 1, 8);
qwerty[19] = new Keyboard('X', 'x', 0, 0); // null
qwerty[20] = new Keyboard('z', 'l', 2, 0);
qwerty[21] = new Keyboard('x', 'l', 2, 1);
qwerty[22] = new Keyboard('c', 'l', 2, 2);
qwerty[23] = new Keyboard('v', 'l', 2, 3);
qwerty[24] = new Keyboard('b', 'r', 2, 4);
qwerty[25] = new Keyboard('n', 'r', 2, 5);
qwerty[26] = new Keyboard('m', 'r', 2, 6);
qwerty[27] = new Keyboard('X', 'x', 0, 0); // null
qwerty[28] = new Keyboard('X', 'x', 0, 0); // null
qwerty[29] = new Keyboard('X', 'x', 0, 0); // null
}
}
class Keyboard {
char c, hand;
int x, y;
public Keyboard(char c, char hand, int x, int y) {
this.c = c;
this.hand = hand;
this.x = x;
this.y = y;
}
}
|
cs |
➕ 다른 풀이 방식
Map을 사용해 해당 문자의 위치를 입력받으셨다.
이러면 해당 문자 찾을 때 더 빠를 수 있겠구먼
[ 백준 / BOJ 20436 ] ZOAC 3 ( JAVA / 자바 )
https://www.acmicpc.net/problem/20436 20436번: ZOAC 3 첫 번째 줄에는 두 알파벳 소문자 sL, sR이 주어진다. sL, sR은 각각 왼손 검지손가락, 오른손 검지손가락의 처음 위치이다. 그 다음 줄에는 알파벳 소문자
hyeyun.tistory.com
백준 20436 ZOAC 3 (JAVA)
문제 https://www.acmicpc.net/problem/20436 20436번: ZOAC 3 첫 번째 줄에는 두 알파벳 소문자 sL, sR이 주어진다. sL, sR은 각각 왼손 검지손가락, 오른손 검지손가락의 처음 위치이다. 그 다음 줄에는 알파벳 소
goto-pangyo.tistory.com
💦 어려웠던 점
- 이렇게 일일이 문자랑 위치 작성하는 게.. 맞나..? 라는 생각을 하면서 작성했다.
- 반례를 못 찾아서 코드 작성하는 시간만큼 어떤 점을 빠뜨렸는지 고민한 시간이 흐른 것 같다.
- 다음엔 Map을 사용해 풀어보겠다,,,
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
(참고)
힌트
글 읽기 - [자바]반례 알려주실 분 구합니다... 어디서 틀린걸까요
댓글을 작성하려면 로그인해야 합니다.
www.acmicpc.net