Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Network
- 완전탐색
- Effective Java
- JUnit 5
- mst
- 그리디
- 위상정렬
- 에라토스테네스의 체
- java
- 수학
- dfs
- 시뮬레이션
- 구현
- 알고리즘
- swea
- 백준
- 투 포인터
- 이분탐색
- 후니의 쉽게 쓴 시스코 네트워킹
- 스택
- 플로이드-와샬
- 백트래킹
- 문자열
- 유니온 파인드
- CS
- BFS
- 프로그래머스
- Kotlin
- 동적계획법
- 세그먼트 트리
Archives
반갑습니다!
[백준] 14499 주사위 굴리기 본문
풀이
시뮬레이션 문제이므로 문제에서 주어진대로 구현하면 해결할 수 있다. 이 문제에서는 주사위를 굴리는 것을 구현하면 쉽게 해결할 수 있다. 상하좌우 4방향을 구현함으로써 쉽게 해결할 수 있다.
위와 같은 주사위는 int dice[] = {1, 5, 6, 2, 4, 3}
으로 저장해서 표현하였다.
코드
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int n, m, x, y, k;
int map[20][20];
const int dx[] = { 0, 0, -1, 1 }, dy[] = { 1, -1, 0 ,0 };
int dice[] = { 0, 0, 0, 0, 0, 0 };
bool inRange(int x, int y) {
return (0 <= x && x < n) && (0 <= y && y < m);
}
void up() {
int tmp[6];
memcpy(tmp, dice, sizeof(dice));
for (int i = 0; i < 4; i++)
dice[i] = tmp[(i + 1) % 4];
}
void down() {
int tmp[6];
memcpy(tmp, dice, sizeof(dice));
for (int i = 0; i < 4; i++)
dice[i] = tmp[(i - 1 + 4) % 4];
}
void left() {
int tmp[6];
memcpy(tmp, dice, sizeof(dice));
dice[0] = tmp[5]; dice[5] = tmp[2]; dice[2] = tmp[4]; dice[4] = tmp[0];
}
void right() {
int tmp[6];
memcpy(tmp, dice, sizeof(dice));
dice[0] = tmp[4]; dice[4] = tmp[2]; dice[2] = tmp[5]; dice[5] = tmp[0];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> x >> y >> k;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> map[i][j];
for (int i = 0; i < k; i++) {
int go;
cin >> go;
if (inRange(x + dx[go - 1], y + dy[go - 1])) {
x += dx[go - 1]; y += dy[go - 1];
if (go == 1) right();
if (go == 2) left();
if (go == 3) up();
if (go == 4) down();
if (map[x][y] != 0) {
dice[2] = map[x][y];
map[x][y] = 0;
}
else map[x][y] = dice[2];
cout << dice[0] << '\n';
}
}
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 16197 두 동전 (0) | 2020.04.29 |
---|---|
[백준] 14500 테트로미노 (0) | 2020.04.29 |
[프로그래머스] 튜플 (0) | 2020.04.29 |
[프로그래머스] 단체사진 찍기 (0) | 2020.04.29 |
[백준] 16985 Maaaaaaaaaze (0) | 2020.04.29 |