λ°μν
https://www.acmicpc.net/problem/20436
λ¬Έμ μ€λͺ
μ±μ°λ λ μ리 νλ²
- λ μ리 νλ²μ΄λ μ μμ κ²μ§ μκ°λ½λ§ μ΄μ©ν΄ νμλ₯Ό μΉλ νλ²
- νκΈ μμ μͺ½ μν π μΌμ, νκΈ λͺ¨μ μͺ½ μν π μ€λ₯Έμ
- aμ μ’ν $(x_1,y_1)$, bμ μ’νκ° $(x_2,y_2)$μΌ λ, aμμ bλ‘ μ΄λνλλ° κ±Έλ¦¬λ μκ°μ $|x_1-x_2|+|y_1-y_2|$
- κ° ν€λ₯Ό λλ₯΄λ λ°μλ 1μ μκ°
- λ μμ λμμ μμ§μΌ μ μμ
- ν€λ³΄λ
λ¬Έμμ΄μ μΉλλ° κ±Έλ¦¬λ μκ°μ μ΅μκ°
λ¬Έμ νμ΄
1. makeKeyboard()
Map<String, int[]> λ κ°μ κ°κ° μνμ κ°κ³Ό μ’νλ₯Ό μ μ₯νλ€.
νλλ conKey → μΌμμ΄ μΉλ μν
λ€λ₯Έ νλλ vowelKey → μ€λ₯Έμμ΄ μΉλ μν
μ΄λ€.
2. CalTime(startLeft, startRight, str)
μκ°μ κ³μ° !!
strμ λ¬Έμλ€μ΄ ν¬ν¨λμ΄μλ μμΉμ λ°λΌ κ³μ°νλ©΄λλ€.
λ§μ½ conKeyμ ν¬ν¨λμ΄μλ€λ©΄, startLeftμ κ°μμ μ΄λνλ μκ°μ λν΄μ£Όκ³ , λλ₯΄λ μκ° += 1 ν΄μ€λ€.
ν¬ν¨λμ΄μμ§ μλ€λ©΄, startRight κ°μΌλ‘ κ³μ°ν΄μ£Όλ©΄ λ¨
~
μ½λ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main_BOJ_20436_ZOAC_3 {
static Map<String, int[]> conKey, vowelKey;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
conKey = new HashMap<>();
vowelKey = new HashMap<>();
makeKeyBoard();
String startLeft = stringTokenizer.nextToken();
String startRight = stringTokenizer.nextToken();
String str = br.readLine();
int ans = calTime(startLeft, startRight, str);
System.out.println(ans);
}
private static int calTime(String startLeft, String startRight, String str) {
int time = 0;
for (char c : str.toCharArray()) {
int[] s, d;
if (conKey.containsKey(c + "")) {
s = conKey.get(startLeft);
d = conKey.get(c + "");
startLeft = c + "";
} else {
s = vowelKey.get(startRight);
d = vowelKey.get(c + "");
startRight = c + "";
}
time += Math.abs(s[0] - d[0]) + Math.abs(s[1] - d[1]);
time += 1;
}
return time;
}
private static void makeKeyBoard() {
conKey.put("q", new int[]{0, 0});
conKey.put("w", new int[]{0, 1});
conKey.put("e", new int[]{0, 2});
conKey.put("r", new int[]{0, 3});
conKey.put("t", new int[]{0, 4});
vowelKey.put("y", new int[]{0, 5});
vowelKey.put("u", new int[]{0, 6});
vowelKey.put("i", new int[]{0, 7});
vowelKey.put("o", new int[]{0, 8});
vowelKey.put("p", new int[]{0, 9});
conKey.put("a", new int[]{1, 0});
conKey.put("s", new int[]{1, 1});
conKey.put("d", new int[]{1, 2});
conKey.put("f", new int[]{1, 3});
conKey.put("g", new int[]{1, 4});
vowelKey.put("h", new int[]{1, 5});
vowelKey.put("j", new int[]{1, 6});
vowelKey.put("k", new int[]{1, 7});
vowelKey.put("l", new int[]{1, 8});
conKey.put("z", new int[]{2, 0});
conKey.put("x", new int[]{2, 1});
conKey.put("c", new int[]{2, 2});
conKey.put("v", new int[]{2, 3});
vowelKey.put("b", new int[]{2, 4});
vowelKey.put("n", new int[]{2, 5});
vowelKey.put("m", new int[]{2, 6});
}
}
728x90
λ°μν
'μκ³ λ¦¬μ¦ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ BOJ / λ°±μ€ 20165 ] μΈλ΄μ λλ―Έλ Έ μ₯μΈ νΈμ ( μλ° / JAVA ) (0) | 2022.02.21 |
---|---|
[ BOJ / λ°±μ€ 5212 ] μ§κ΅¬ μ¨λν ( μλ° / JAVA ) (0) | 2022.02.15 |
[ λ°±μ€ / BOJ 21609 ] μμ΄ μ€νκ΅ ( μλ° / JAVA ) (0) | 2022.02.14 |
[ BOJ / λ°±μ€ 20061 ] λͺ¨λ Έλ―Έλ Έλλ―Έλ Έ 2 ( μλ° / JAVA ) (0) | 2022.02.12 |
[ BOJ / λ°±μ€ 17143 ] λμμ ( μλ° / JAVA ) (0) | 2022.02.09 |