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
문제 이해
주사위의 움직임만 잘 정리하면 어렵지 않은 문제이다.
https://chagaun-omija.tistory.com/273?category=867475
[백준] 23288번: 주사위 굴리기 2
https://www.acmicpc.net/problem/23288 23288번: 주사위 굴리기 2 크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개.
chagaun-omija.tistory.com
여기에서 한번 주사위의 움직임을 연구(?) 한 적 있어서 그대로 풀었다.
방향을 받아서 그쪽으로
주사위를 굴리는 작업 (주사위의 바닥, 윗면, 동서남북을 조정하기) + 주사위 위치 변경
을 한 후에 새로운 주사위 위치의 맵 값을 확인한 후에,
주사위에 복사를 하던가, 맵에 복사를 하던가를 정해서 해주면 되고,
윗 면을 한 번 출력해주면 된다.
각 방향 (동서남북)으로의 이동은 별개의 함수들을 만들어줘서 처리했다.
작성 코드
#include <stdio.h>
#include <string.h>
#define MAXMN 20
#define MAXK 1000
#define EAST 0
#define WEST 1
#define SOUTH 3
#define NORTH 2
int N, M, K;
int Y, X; // 행열
int cmd[MAXK + 5];
int map[MAXMN + 5][MAXMN + 5];
int dy[] = { 0, 0, -1, 1 }; // 동서북남
int dx[] = { 1, -1, 0, 0 };
typedef struct st {
int bottom, top, east, west, south, north;
}st;
st DICE;
void Get_Input(void) {
scanf("%d %d %d %d %d", &N, &M, &Y, &X, &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", &cmd[i]);
cmd[i]--;
}
}
void Copy_and_Print(void) {
// 주사위 바닥 복사
if (map[Y][X] == 0) map[Y][X] = DICE.bottom;
else {
DICE.bottom = map[Y][X];
map[Y][X] = 0;
}
// 주사위 윗 면 프린트
printf("%d\n", DICE.top);
}
void Move_East(void) {
Y += dy[EAST], X += dx[EAST];
if (Y < 0 || Y >= N || X < 0 || X >= M) {
Y -= dy[EAST], X -= dx[EAST];
return;
}
int tmp_bottom = DICE.bottom;
int tmp_top = DICE.top;
DICE.bottom = DICE.east;
DICE.top = DICE.west;
DICE.east = tmp_top;
DICE.west = tmp_bottom;
Copy_and_Print();
}
void Move_West(void) {
Y += dy[WEST], X += dx[WEST];
if (Y < 0 || Y >= N || X < 0 || X >= M) {
Y -= dy[WEST], X -= dx[WEST];
return;
}
int tmp_bottom = DICE.bottom;
int tmp_top = DICE.top;
DICE.bottom = DICE.west;
DICE.top = DICE.east;
DICE.east = tmp_bottom;
DICE.west = tmp_top;
Copy_and_Print();
}
void Move_South(void) {
Y += dy[SOUTH], X += dx[SOUTH];
if (Y < 0 || Y >= N || X < 0 || X >= M) {
Y -= dy[SOUTH], X -= dx[SOUTH];
return;
}
int tmp_bottom = DICE.bottom;
int tmp_top = DICE.top;
DICE.bottom = DICE.south;
DICE.top = DICE.north;
DICE.north = tmp_bottom;
DICE.south = tmp_top;
Copy_and_Print();
}
void Move_North(void) {
Y += dy[NORTH], X += dx[NORTH];
if (Y < 0 || Y >= N || X < 0 || X >= M) {
Y -= dy[NORTH], X -= dx[NORTH];
return;
}
int tmp_bottom = DICE.bottom;
int tmp_top = DICE.top;
DICE.bottom = DICE.north;
DICE.top = DICE.south;
DICE.south = tmp_bottom;
DICE.north = tmp_top;
Copy_and_Print();
}
int main(void) {
Get_Input();
for (int i = 0; i < K; i++) {
if (cmd[i] == EAST) Move_East();
else if (cmd[i] == WEST) Move_West();
else if (cmd[i] == SOUTH) Move_South();
else Move_North();
}
return 0;
}
'컴퓨터기본 > 문제풀이' 카테고리의 다른 글
[백준] 12100번: 2048(Easy) (0) | 2021.11.26 |
---|---|
[백준] 3190번: 뱀 (0) | 2021.11.26 |
[백준] 14500번: 테트로미노 (0) | 2021.11.25 |
[백준] 14501번: 퇴사 (0) | 2021.11.25 |
[백준] 14502번: 연구소 (0) | 2021.11.25 |