반응형
[광고 누르면 오늘의 행운 상승!!]
https://www.acmicpc.net/problem/14499
주사위 싫당
1. 주사위가 이동한 방향대로 하드코딩한다.
2. 바닥과 맵의 값을 잘 확인한다.
import java.io.*;
import java.util.*;
class Dice {
int top, bottom, west, east, south, north;
public void moveEast() {
int temp = top;
top = west;
west = bottom;
bottom = east;
east = temp;
}
public void moveWest() {
int temp = top;
top = east;
east = bottom;
bottom = west;
west = temp;
}
public void moveSouth() {
int temp = top;
top = north;
north = bottom;
bottom = south;
south = temp;
}
public void moveNorth() {
int temp = top;
top = south;
south = bottom;
bottom = north;
north = temp;
}
}
class 주사위굴리기 {
public static int N, M, x, y, k;
public static int[][] map;
public static int[] dir;
public static int[] dx = {0,0,0,-1,1}; // 동서북남
public static int[] dy = {0,1,-1,0,0};
public static Queue<Integer> q;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int[N][M];
dir = new int[k];
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());
q = new LinkedList<>();
for(int i=0; i<k; i++) {
q.add(Integer.parseInt(st.nextToken()));
}
Start();
}
static void Start() {
Dice dice = new Dice();
int nx, ny;
while(!q.isEmpty()) {
int dir = q.poll();
nx = x + dx[dir];
ny = y + dy[dir];
if(0 <= nx && nx < N && 0 <= ny && ny < M) {
if (dir == 1) dice.moveEast();
else if(dir == 2) dice.moveWest();
else if(dir == 3) dice.moveNorth();
else if(dir == 4) dice.moveSouth();
if(map[nx][ny] == 0)
map[nx][ny] = dice.bottom;
else {
dice.bottom = map[nx][ny];
map[nx][ny] = 0;
}
System.out.println(dice.top);
x = nx;
y = ny;
}
}
}
}
반응형
'2. 알고리즘사이트 > 1. 백준' 카테고리의 다른 글
만취한 상범[백준 6359][브론즈2][Java] (0) | 2020.03.11 |
---|---|
이분 그래프 [백준 1707][골드4][Java] (1) | 2020.03.11 |
연산자 끼워넣기 [백준 14888][실버1][Java] (0) | 2020.03.08 |
스타트와 링크 [백준14889][실버3][Java] (0) | 2020.03.08 |
오 나의 여신님 [SWEA 7793][D5][Java] (0) | 2020.03.08 |