import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.StringTokenizer;


class Dice {

private int[] face;


static final int Left = 1, Right = 2;

static final int Up = 3, Down = 4;


int top, bottom;

int north, south;

int west, east;


// 전개도를 펼쳤을 때, 가운데를 top이라 생각하고, 각각의 면에 index를 메김

public Dice() {

top = 1;

bottom = 6;

north = 2;

south = 5;

west = 4;

east = 3;


face = new int[7];

}


// 주사위 디버깅

private void printDice() {

Main.log("%2d%2d%2d", 0, north, 0);

Main.log("%2d%2d%2d", west, top, east);

Main.log("%2d%2d%2d", 0, south, 0);

Main.log("%2d%2d%2d", 0, bottom, 0);

}


// 주사위를 해당 방향에 굴린 후, 바닥면을 주사위로 옮긴다.

public int move(int direct, int num) {


// 주사위를 굴리면 전개도에서 4면의 변화가 생김

int temp;

switch (direct) {

case Up:

temp = north;

north = top;

top = south;

south = bottom;

bottom = temp;

break;

case Down:

temp = bottom;

bottom = south;

south = top;

top = north;

north = temp;

break;

case Left:

temp = west;

west = top;

top = east;

east = bottom;

bottom = temp;

break;

case Right:

temp = east;

east = top;

top = west;

west = bottom;

bottom = temp;

break;

}


// 윗면 출력

System.out.println(face[top]);

// printDice();


// 주사위 면 복사

int result;

result = face[bottom];

if (num != 0) {

face[bottom] = num;

}


return result;

}


}


public class Main {


private static boolean Debug = false;

private static int MapSize_Y;

private static int MapSize_X;

private static int[][] Map;

private static int[] Command;

private static Dice Dice;

private static int startY;

private static int startX;


public static void main(String[] args) throws Exception {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st;


// 맵 크기

st = new StringTokenizer(br.readLine());

MapSize_Y = Integer.parseInt(st.nextToken());

MapSize_X = Integer.parseInt(st.nextToken());


// 주사위 시작 위치

startY = Integer.parseInt(st.nextToken());

startX = Integer.parseInt(st.nextToken());

Command = new int[Integer.parseInt(st.nextToken())];


// 맵 읽기

Map = new int[MapSize_Y][];

for (int i = 0; i < MapSize_Y; i++) {

st = new StringTokenizer(br.readLine());

Map[i] = new int[MapSize_X];

for (int j = 0; j < MapSize_X; j++) {

Map[i][j] = Integer.parseInt(st.nextToken());

}

}


// 커맨드 읽기

int clen = Command.length;

st = new StringTokenizer(br.readLine());

for (int i = 0; i < clen; i++) {

Command[i] = Integer.parseInt(st.nextToken());

}


Dice = new Dice();


MoveDice();

// System.out.println(MaxSafeCount);

}


// 1~4 동서북남

static int[] rotY = { 0, 0, 0, -1, 1 };

static int[] rotX = { 0, 1, -1, 0, 0 };


private static void MoveDice() {


int clen = Command.length;

int y = startY;

int x = startX;


int moveY, moveX;


for (int i = 0; i < clen; i++) {

int direct = Command[i];

moveY = y + rotY[direct];

moveX = x + rotX[direct];


if (!isRight(moveY, moveX)) {

continue;

}


// 바닥면의 경우

int mapValue = Map[moveY][moveX];

if (mapValue != 0) {

// 바닥에 0이 아닐때, 숫자는 주사위로 옮겨간다.

Dice.move(direct, mapValue);

Map[moveY][moveX] = 0;

} else {

// 바닥이 0이라면 , 숫자는 맵으로 옮겨간다.

int num = Dice.move(direct, mapValue);

Map[moveY][moveX] = num;

}


y = moveY;

x = moveX;

}

}


private static boolean isRight(int y, int x) {


if (x < 0 || y < 0 || x >= MapSize_X || y >= MapSize_Y) {

return false;

} else {

return true;

}

}


public static void log(String input) {

if (Debug) {

System.out.println(input);

}

}


public static void log(String input, Object... args) {

if (Debug) {

System.out.println(String.format(input, args));

}

}


}



'Knowledge > 알고리즘' 카테고리의 다른 글

백준 12100번: 2048(Easy)  (0) 2017.10.19
백준 3190번: 뱀  (0) 2017.10.19
백준 14502번: 연구소  (0) 2017.10.17
백준 14501번: 퇴사  (0) 2017.10.17
백준 13458번: 시험 감독  (0) 2017.10.17

+ Recent posts