728x90
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | import java.io.*; import java.util.*; public class Main { static int N,M; static int x,y; static int K; static int[][] map = new int[21][21]; static final int EAST=1; static final int WEST=2; static final int NORTH=3; static final int SOUTH=4; static int[] horizon = new int[3]; static int[] vertical= new int[4]; 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()); x=Integer.parseInt(st.nextToken()); y=Integer.parseInt(st.nextToken()); K=Integer.parseInt(st.nextToken()); 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()); } } /* 입력값 mapping 체크 * System.out.println(N +""+ M +""+ x +""+ y +""+ K); * * for(int i=1; i<=N; i++) { for(int j=1; j<=M; j++) { * System.out.print(map[i][j]); } System.out.println(); } */ st = new StringTokenizer(br.readLine()); while(st.hasMoreTokens()) { int dir = Integer.parseInt(st.nextToken()); rollingDice(dir); } } static void rollingDice(int dir) { if(dir==1 && y<M-1) { y+=1; rotateEast(); } else if (dir == 2 && 0<y) { y-=1; rotateWest(); } else if (dir == 3 && 0<x) { x-=1; rotateNorth(); } else if (dir == 4 && x<N-1) { x+=1; rotateSouth(); } } static void rotateEast() { int temp = horizon[2]; horizon[2] = horizon[1]; horizon[1] = horizon[0]; horizon[0] = vertical[3]; vertical[3] = temp; vertical[1] = horizon[1]; copyNum(); } static void rotateWest() { int temp = horizon[2]; horizon[2] = vertical[3]; vertical[3] = horizon[0]; horizon[0] = horizon[1]; horizon[1] = temp; vertical[1] = horizon[1]; copyNum(); } static void rotateNorth() { int temp = vertical[1]; vertical[1] = vertical[2]; vertical[2] = vertical[3]; vertical[3] = vertical[0]; vertical[0] = temp; horizon[1] = vertical[1]; copyNum(); } static void rotateSouth() { int temp = vertical[1]; vertical[1] = vertical[0]; vertical[0] = vertical[3]; vertical[3] = vertical[2]; vertical[2] = temp; horizon[1] = vertical[1]; copyNum(); } static void copyNum() { if(map[x][y]==0) map[x][y]=vertical[3]; else { vertical[3] = map[x][y]; map[x][y] = 0; } // System.out.println("xy" + x + " " +y +" 밑" +vertical[3] + "위" + vertical[1]); System.out.println(vertical[1]); } } |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 14719번 빗물 (0) | 2019.12.14 |
---|---|
[백준][Java] 2493번 탑 (0) | 2019.12.11 |
[백준][Java] 9935번 문자열 폭발 (0) | 2019.03.25 |
[백준][Java] 9933번 민균이의 비밀번호 (0) | 2019.03.24 |
[백준][Java] 3986번 좋은 단어 (0) | 2019.03.24 |