🔺 문제
1063번: 킹
8*8크기의 체스판에 왕이 하나 있다. 킹의 현재 위치가 주어진다. 체스판에서 말의 위치는 다음과 같이 주어진다. 알파벳 하나와 숫자 하나로 이루어져 있는데, 알파벳은 열을 상징하고, 숫자는
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
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
char[] king = st.nextToken().toCharArray();
char[] stone = st.nextToken().toCharArray();
int N = Integer.parseInt(st.nextToken());
while(N --> 0) {
String cmd = br.readLine();
char[] next_king = move(cmd, king);
if(rangeCheck(next_king)) {
if(next_king[0] == stone[0] && next_king[1] == stone[1]) {
char[] next_stone = move(cmd, stone);
if(rangeCheck(next_stone)) {
king = next_king;
stone = next_stone;
}
else
continue;
}
else {
king = next_king;
}
}
else {
continue;
}
}
// 킹 마지막 위치, 돌 마지막 위치 출력하기
for(char k : king) {
System.out.print(k);
}
System.out.println();
for(char s : stone) {
System.out.print(s);
}
}
static boolean rangeCheck(char[] ch) {
if(ch[0] < 'A' || ch[0] > 'H' || ch[1] < '1' || ch[1] > '8') return false;
return true;
}
static char[] move(String cmd, char[] target) {
char[] result = target.clone();
switch(cmd) {
case "R":
result[0]++;
break;
case "L":
result[0]--;
break;
case "B":
result[1]--;
break;
case "T":
result[1]++;
break;
case "RT":
result[0]++; result[1]++;
break;
case "LT":
result[0]--; result[1]++;
break;
case "RB":
result[0]++; result[1]--;
break;
case "LB":
result[0]--; result[1]--;
break;
}
return result;
}
}
|
cs |
✅ 해결 아이디어
✔ 구현, 시뮬레이션
🔺 다른 풀이들
- 자바에서 배열을 복사하는 Object.clone()
함수와 두 배열의 내용물을 비교하는 Arrays.equals(a, b)
를 사용하심.
[백준] 1063번 : 킹
문제 8*8크기의 체스판에 왕이 하나 있다. 킹의 현재 위치가 주어진다. 체스판에서 말의 위치는 다음과 같이 주어진다. 알파벳 하나와 숫자 하나로 이루어져 있는데, 알파벳은 열을 상징하고, 숫
gyuwon95.tistory.com
- 깰꼼..
로그인
www.acmicpc.net
💬 느낀 점
길구나...
이동시키기 전에 길이가 범위 안에 들어가는지 유효성 검사하려고 했는데
이동시켜놓고 유효성 검사 해도 되는거였네,,
암튼... 이해 완!
담엔 더 빠르게 풀자,,
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V | 240321 |
(+ 240321 2회독 : 16168KB, 156ms)
푸는 데 40분,,, 디버깅 20분..
이게 나에게는 더 편안한 풀이였다.
킹이 이동하려는 칸에 돌이 있고, 그 돌을 한 칸 이동시킬 수 없을 때 킹도 이동하지 못 하게 해야한다는 사실을 망각했다.
더 집중해서 푸는 시간 좀 줄이자ㅠㅠ
코드 확인하기
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
|
import java.util.*;
import java.io.*;
public class Main {
static int[] dx = {1, -1, 0, 0, 1, -1, 1, -1}; // R, L, B, T, RT, LT, RB,LB
static int[] dy = {0, 0, -1, 1, 1, 1, -1, -1};
static int[][] board = new int[8][8]; // 1: 돌
static int[] king, stone;
static int cnt;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
king = new int[] {str[0].charAt(0) -'A', Integer.parseInt(str[0].substring(1)) -1};
stone = new int[] {str[1].charAt(0) -'A', Integer.parseInt(str[1].substring(1)) -1};
cnt = Integer.parseInt(str[2]);
board[stone[0]][stone[1]] = 1; // 돌 두기
while(cnt --> 0) {
move(br.readLine());
}
System.out.println((char)(king[0] + 'A') + "" + (king[1] +1));
System.out.println((char)(stone[0] + 'A') + "" + (stone[1] +1));
}
private static void move(String cmd) {
int dir = 0;
switch(cmd) {
case "R":
dir = 0;
break;
case "L":
dir = 1;
break;
case "B":
dir = 2;
break;
case "T":
dir = 3;
break;
case "RT":
dir = 4;
break;
case "LT":
dir = 5;
break;
case "RB":
dir = 6;
break;
case "LB":
dir = 7;
break;
}
int nx = king[0] + dx[dir];
int ny = king[1] + dy[dir];
if(!inRange(nx, ny)) return; // 킹이 격자 범위 벗어나면 리턴
if(board[nx][ny] != 0) { // 이동한 곳에 돌이 있으면, 돌도 같은 방향으로 한 칸 이동
int sx = stone[0] + dx[dir];
int sy = stone[1] + dy[dir];
if(!inRange(sx, sy)) return; // 💥 돌이 더 이상 이동할 수 없으면, 킹도 이동 못 하고 리턴
board[stone[0]][stone[1]] = 0;
stone[0] = sx;
stone[1] = sy;
board[stone[0]][stone[1]] = 1;
}
king[0] = nx;
king[1] = ny;
}
private static boolean inRange(int x, int y) {
return 0 <= x && x < 8 && 0 <= y && y < 8;
}
}
|
cs |
(참고)
✔ 풀이 참고
백준 1063번( 자바 )
백준 1063번 구현 문제를 풀어보자 ( 자바 )
velog.io
✔ Object.clone() 사용법
[Java] 자바 배열을 복사하는 다양한 방법 (깊은복사, 얕은복사)
자바에서 객체를 복사하는 유형으로 깊은 복사와 얕은 복사가 있습니다. 깊은 복사의 경우 객체의 실제값을 새로운 객체로 복사하는 것이고 얕은 복사는 단순히 객체의 주소 값만을 복사하는
coding-factory.tistory.com
✔ Arrays.equals() 메소드 사용법
[Java]두 배열을 비교하는 방법
두 배열을 비교하는 방법 Java에서 두 배열을 비교해야 하는 경우 다음 조건을 만족하면 동일하다고 판단합니다. - 두 배열은 모두 동일한 타입입니다. - 두 배열은 동일한 수의 요소를 가지고 있
developer-talk.tistory.com
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 1926번: 그림 (0) | 2023.06.26 |
---|---|
[백준/JAVA] 8979번: 올림픽 (0) | 2023.06.24 |
[백준/JAVA] 2980번: 도로와 신호등 (0) | 2023.06.22 |
[백준/JAVA] 1966번: 프린터 큐 (0) | 2023.06.20 |
[백준/JAVA] 2161번: 카드1 (0) | 2023.06.19 |