https://www.acmicpc.net/problem/14499
14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지
www.acmicpc.net
* 주사위 규칙 이해하기
이 문제는 주사위를 동서남북으로 굴렸을 경우 주사위의 위치가 변화 규칙만 이해하면 생각보다 금방 풀리는 문제이다. 동서남북으로 주사위를 굴렸을 경우 4개의 위치만 변화한다.
위치는 int[] 배열의 1-6 이라고 하고, 각각의 값을 넣으면 되겠다.
* 또한 2차원 배열이 나올때 항상 잊지 말아야 할 범위 내 이동
- if(ny < 0 || nx < 0 || ny > n-1 || nx > m-1) return;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n, m, x, y, k;
//지도
static int[][] map;
//주사위
static int[] dice = new int[7];
static int dx[] = {1,-1,0,0};
static int dy[] = {0,0, -1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int[n][m];
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < m; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
//주사위 이동
for(int i = 0; i < k; i++) {
int d = Integer.parseInt(st.nextToken());
move(d);
}
}
static void move(int d) {
int ny = y + dy[d-1];
int nx = x + dx[d-1];
//지도 범위 벗어날 경우
if(ny < 0 || nx < 0 || ny > n-1 || nx > m-1) return;
roll(d, nx, ny);
x = nx; y = ny;
}
private static void roll(int d, int nx, int ny) {
int tmp = dice[3];
switch (d) {
case 1:
dice[3] = dice[4];
dice[4] = dice[6];
dice[6] = dice[2];
dice[2] = tmp;
break;
case 2:
dice[3] = dice[2];
dice[2] = dice[6];
dice[6] = dice[4];
dice[4] = tmp;
break;
case 3:
dice[3] = dice[5];
dice[5] = dice[6];
dice[6] = dice[1];
dice[1] = tmp;
break;
case 4:
dice[3] = dice[1];
dice[1] = dice[6];
dice[6] = dice[5];
dice[5] = tmp;
break;
}
if(map[ny][nx] == 0) {
map[ny][nx] = dice[6];
} else {
dice[6] = map[ny][nx];
map[ny][nx] = 0;
}
System.out.println(dice[3]);
}
}
'코딩테스트 > baekjoon' 카테고리의 다른 글
[baekjoon]회의실 배정 JAVA (0) | 2022.11.24 |
---|---|
[beakjoon] 드래곤 커브 JAVA (0) | 2022.11.10 |
[baekjoon] 로봇 청소기 JAVA (0) | 2022.11.09 |
[beakjoon]퇴사 JAVA (0) | 2022.11.08 |