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 124 125 126 127 128 129 130 131 132 133
| package Samsung.done;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*;
public class 주사위굴리기_14499번 { static int n,m,x,y,k; static int[][] map; static int[][] dir = {{0,1},{0,-1},{-1,0},{1,0}}; static List<Integer> commandList; static int upCopy; static int topCopy; static int rightCopy; static int leftCopy; static int bottomCopy; static int downCopy;
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()); map = new int[n][m]; commandList = new ArrayList<>(); 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++){ commandList.add(Integer.parseInt(st.nextToken())); } Dice dice = new Dice(x,y,0,0,0,0,0,0);
for (int i = 0; i < commandList.size(); i++) { int dx = dice.currX; int dy = dice.currY; int currDir = commandList.get(i)-1; int mx = dx + dir[currDir][0]; int my = dy + dir[currDir][1];
if(isCheckRange(mx,my)) continue; diceStateSave(dice); rollDice(dice, currDir);
if(map[mx][my] == 0){ map[mx][my] = dice.down; } else { dice.down = map[mx][my]; map[mx][my] = 0; } System.out.println(dice.up); dice.currX = mx; dice.currY = my; } }
private static void rollDice(Dice dice, int currDir) { if(currDir == 0){ dice.right = upCopy; dice.down = rightCopy; dice.left = downCopy; dice.up = leftCopy; }else if(currDir == 1){ dice.left = upCopy; dice.down = leftCopy; dice.right = downCopy; dice.up = rightCopy; }else if(currDir == 2){ dice.top = upCopy; dice.down = topCopy; dice.bottom = downCopy; dice.up = bottomCopy; }else if(currDir == 3){ dice.up = topCopy; dice.bottom = upCopy; dice.down = bottomCopy; dice.top = downCopy; } }
private static void diceStateSave(Dice dice) { upCopy = dice.up; topCopy = dice.top; rightCopy = dice.right; leftCopy = dice.left; bottomCopy = dice.bottom; downCopy = dice.down; }
private static boolean isCheckRange(int mx, int my) { if(mx < 0 || mx >= n || my < 0 || my >= m) return true; return false; } private static class Dice { int currX; int currY;
int up; int top; int right; int left; int bottom; int down;
public Dice(int currX, int currY, int up, int top, int right, int left, int bottom, int down) { this.currX = currX; this.currY = currY; this.up = up; this.top = top; this.right = right; this.left = left; this.bottom = bottom; this.down = down; } } }
|