-
[백준][자바][14499][주사위굴리기]코딩/알고리즘 2020. 1. 12. 19:41
주사위 동서 북남으로 이동할떄 값이 어떻게 변할지가 제일 핵심인 문제
손으로 그려가면 편하다
잠깐 착각을 해서 디버깅에만 몇시간을 쓴지 모르겠다.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; class Dice{ int y; int x; Dice(int y, int x){ this.y = y; this.x = x; } } public class Main_backjoon_14499_주사위굴리기 { static int[] dice= {0, 0, 0 ,0, 0, 0, 0 }; static int N; static int M; static int X; static int Y; static int K; static int[][] MAP; static int[] ORDER; //동서북남 static int[] dy = {0,0,0,-1,1}; static int[] dx = {0,1,-1,0,0} ; static Queue q = new LinkedList(); public static void main(String[] args) throws IOException { /** 4 2 0 0 8 0 2 3 4 5 6 7 8 4 4 4 1 3 3 3 2 */ 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]; q.add(new Dice(X,Y)); ORDER = 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(), " "); for(int i = 0; i < K; i++ ) { ORDER[i] = Integer.parseInt(st.nextToken()); } solve(); } public static void solve() { for(int i = 0 ; i < K; i++) { Dice d = q.poll(); int yy = d.y + dy[ORDER[i]]; int xx = d.x + dx[ORDER[i]]; //범위 넘은 경우 if(!isRange(yy, xx)) { q.add(new Dice(d.y,d.x)); continue; } else { q.add(new Dice(yy,xx)); } //데굴 changeDiceValue(ORDER[i]); //만약 밑면이 0이 아닌경우 //주사위에 값 복사 if(MAP[yy][xx] != 0 ) { dice[1] = MAP[yy][xx]; MAP[yy][xx] = 0; } else { //아닌경우 바닥에 값 복사 MAP[yy][xx] = dice[1]; } //주사위 값 변경 //changeDiceValue(ORDER[i]); //print(ORDER[i]); System.out.println(dice[6] + " "); } } public static boolean isRange(int yy, int xx) { if(yy >= 0 && yy < N && xx >= 0 && xx < M) { return true; } else { return false; } } public static void swap(int target1, int target2) { int temp = dice[target1]; dice[target1] = dice[target2]; dice[target2] = temp; } //방향에 따라 주사위 값 변경 public static void changeDiceValue(int direction) { if(direction == 1) { // 동 swap(1,4); swap(1,3); swap(3,6); } else if(direction == 2) { // 서 swap(1,4); swap(3,6); swap(4,3); } else if(direction == 3) { //북 swap(1,2); swap(2,5); swap(2,6); } else if(direction == 4) { // 남 swap(1,5); swap(5,6); swap(2,6); } } public static void print(int direction) { System.out.print("방향 = " + direction + "/"); for(int i = 1 ; i < 7; i++) { System.out.print(dice[i] +" "); } System.out.println(); } }
'코딩 > 알고리즘' 카테고리의 다른 글
[mac] mysql 이모지 저장하기 (0) 2021.09.06 [백준][자바][파이프옮기기1]17070 (0) 2019.11.01 [백준][자바][17135]캐슬디펜스 (0) 2019.10.23 [백준][자바][9205]맥주 마시면서 걸어가기 (0) 2019.10.02 [백준][자바][5017]스타트링크 (0) 2019.10.01