#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> board;
vector<int> dice(7, 0); 
int n, m, x, y, k;

bool inRange(int row, int col) {
	if (0 <= row && row < n && 0 <= col && col < m)
		return true;

	return false;
}

void rollDice(int dir) {
	if (dir == 1) {
		//동
		int temp1 = dice[3];
		dice[3] = dice[1];
		dice[1] = dice[4];
		dice[4] = dice[6];
		dice[6] = temp1;
	}
	else if (dir == 2) {
		//서
		int temp = dice[4];
		dice[4] = dice[1];
		dice[1] = dice[3];
		dice[3] = dice[6];
		dice[6] = temp;
	}
	else if (dir == 3) {
		//북
		int temp = dice[2];
		dice[2] = dice[1];
		dice[1] = dice[5];
		dice[5] = dice[6];
		dice[6] = temp;
	}
	else if (dir == 4) {
		int temp = dice[6];
		dice[6] = dice[5];
		dice[5] = dice[1];
		dice[1] = dice[2];
		dice[2] = temp;
	}
}

int main() {
	scanf("%d %d  %d %d %d", &n, &m, &y, &x, &k);

	board= vector<vector<int>>(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", &board[i][j]);
		}
	}

	vector<int> command(k);
	for (int i = 0; i < k; i++) {
		scanf("%d", &command[i]);
	}

	////////////////////////////////

	int currentCommand;

	for (int i = 0; i < k; i++) {
		int nextRow = y;
		int nextCol = x;

		currentCommand = command[i];
		

		if (currentCommand == 1) {  //동
			nextCol = x + 1;
		}
		else if (currentCommand == 2) {  //서
			nextCol = x - 1;
		}
		else if (currentCommand == 3) {  //북
			nextRow = y - 1;
		}
		else if (currentCommand == 4) {  //남
			nextRow = y + 1;
		}

		if (inRange(nextRow, nextCol)) {
			//갈 수 있으면
			rollDice(currentCommand);  //주사위 굴리기

			y = nextRow;
			x = nextCol;

			if (board[y][x] == 0) {
				board[y][x] = dice[6];
			}
			else {
				dice[6] = board[y][x];
				board[y][x] = 0;
			}
			printf("%d\n", dice[1]);
		}
		else {  //갈 수 없으면 다시 돌려놓기
			
		}
	}


	return 0;
}

 

주사위를 각 방향으로 굴렸을 때 어떤 모양이 되는지 잡는게 포인트!

중학교떄부터 전개도 이런거 너무 못하고 싫어해서 이런 문제 너무 싫다...... 이거 풀때도 육면체 모양 지우개 굴려가면서 함..

 

1. 실수했던 부분

1) 범위를 넘어가도 주사위는 굴렸음.. 문제에 "주사위는 지도의 바깥으로 이동시킬 수 없다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다."라로 확실히게 명시 되어있었는데 왜 그랬지ㅠ 문제를 잘 읽자....

 

2) 게시판 겨우 찾아가면서 해결했는데 입력을 x y 순서로 받는게 아니라 y x 순서로 받아야한단다.... 문제를 보면 r c / x y 이렇게 각각 r는 x에, c는 y에 대응되는데 문제의 입력에 써있는 x y랑 내 문제 속 x y랑 반대였다

아마 이런걸 코테 중에 마주쳤다면 평생 모르지 않았을까싶음...ㅋ

 

https://www.acmicpc.net/problem/14499

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도

www.acmicpc.net

'Baekjoon' 카테고리의 다른 글

[#16236] 아기상어  (0) 2020.10.12
[#14502] 연구소  (0) 2020.10.12
[#13458] 시험 감독  (0) 2020.10.11
[#12100] 2048(Easy)  (0) 2020.10.11
[#3190] 뱀  (0) 2020.10.10

+ Recent posts