c,

[C] Algorithm : Backjoon 14499 주사위 굴리기

Software-engineer Software-engineer Follow Dec 22, 2020 · 6 mins read
[C] Algorithm : Backjoon 14499 주사위 굴리기
Share this

Baekjoon Problem 14499 is roll the dice. Please check the details and pictures of the problem in Baekjonn. For uncomplicated algorithmic problems, it is possible to code the problem right away, but it is possible to reduce trial and error by analyzing the problem and thinking about which functions and elements to solve the problem and organizing it.

백준 14499번 문제는 파이프 옮기기 1이다. 문제에 대한 내용과 그림 등은 백준에서 직접 확인하길 바란다. 복잡하지 않은 알고리즘 문제에서는 바로 코딩을 해도 되지만, 문제를 분석하고 어떤 함수와 요소들을 활용하여 문제를 해결할지 고민하고 정리하는 것이 시행착오를 줄일 수 있다.

Analysis of the problem requirements is as follows.

  1. When moving the dice, rearrange the dice value.
  2. When the dice are placed, the value on the dice put on the map according to the conditions, or the map value is recorded on the dice.

문제 요구 사항을 분석하면 아래와 같다.

  1. 주사위를 이동시킬 때, 주사위 값을 재배치해야 한다.
  2. 주사위가 위치하면 조건에 따라 값을 지도에 넣거나, 지도 값을 주사위에 기록해야 한다.

The detailed requirements and analysis of the problem are as follows.

  1. When out of the range of the array, the move and write operations should not be performed.

문제 상세 요건 및 분석사항은 아래와 같다.

  1. 배열의 범위에 벗어날 때는 이동 및 기록 작업이 수행되면 안된다.

In a simple problem, how to record and handle dice is key. The dice rolling method in the code below is implemented in an array, but it can be improved in a better way, and will be updated later.

간단한 문제에서 주사위 값을 어떻게 기록하고 다루는 지가 핵심이다. 아래 코드의 주사위 돌리는 방법은 배열로 구현되어 있으나 더 좋은 방법으로 개량가능하며, 추후 업데이트 예정이다.

#include <stdio.h>

int map[21][21];
int dice[3][2];
int dir[1001];
int dx[5] = {0,0, 0, -1, 1};
int dy[5] = {0,1, -1, 0, 0};

void rotate(int mode) {
	if (mode==1) {
		int a = dice[1][0];
		int b = dice[1][1];
		dice[1][0] = dice[2][1];
		dice[1][1] = dice[2][0];
		dice[2][0] = a;
		dice[2][1] = b;
	}
	else if (mode == 2) {
		int a = dice[1][0];
		int b = dice[1][1];
		dice[1][0] = dice[2][0];
		dice[1][1] = dice[2][1];
		dice[2][0] = b;
		dice[2][1] = a;
	}
	else if (mode == 3) {
		int a = dice[0][0];
		int b = dice[0][1];
		dice[0][0] = dice[2][1];
		dice[0][1] = dice[2][0];
		dice[2][0] = a;
		dice[2][1] = b;
	}
	else if (mode == 4) {
		int a = dice[0][0];
		int b = dice[0][1];
		dice[0][0] = dice[2][0];
		dice[0][1] = dice[2][1];
		dice[2][0] = b;
		dice[2][1] = a;
	}
}


void copy_paste(int x, int y) {
	if (map[x][y] == 0) map[x][y] = dice[2][1];
	else {
		dice[2][1] = map[x][y];
		map[x][y] = 0;
	}
}


void move(int x, int y, int N, int M, int K) {
	for (int i = 0; i < K; i++) {
		int dic = dir[i];
		if (x + dx[dic] < N && y + dy[dic] < M && x + dx[dic] >= 0 && y + dy[dic] >= 0) {
			x += dx[dic];
			y += dy[dic];
			rotate(dic);
			copy_paste(x, y);
			printf("%d\n", dice[2][0]);
		}
	}
}

int main() {
	int N, M;
	int x, y;
	int K;
	scanf("%d %d", &N, &M);
	scanf("%d %d", &x, &y);
	scanf("%d", &K);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			scanf("%d", &map[i][j]);
		}
	}
	for (int i = 0; i < K; i++) {
		scanf("%d", &dir[i]);
	}
	move(x, y, N, M,K);
	return 0;}

Software-engineer
Written by Software-engineer
Software engineer Youn