반응형
https://www.acmicpc.net/problem/23288
23288번: 주사위 굴리기 2
크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int N, M, K, Map[21][21], pointMap[21][21], ans;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int moveDirection = 0;
//dice는 문제의 나온 전개도랑 다릅니다. 제가 문제에 제시된걸 안보고 풀어서 그렇습니다..
int dice[6] = {6, 5, 1, 2, 4, 3};
int floorX = 1, floorY = 1;
void drawPointMap() {
queue<pair<int, int> > Q;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (pointMap[i][j] == 0) {
vector<pair<int, int> > v;
Q.push({i, j});
//방문체크
pointMap[i][j] = -1;
v.push_back({i, j});
while (!Q.empty()) {
int x = Q.front().first;
int y = Q.front().second;
Q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx <= 0 || nx > N || ny <= 0 || ny > M) continue;
if (Map[nx][ny] == Map[i][j] && pointMap[nx][ny] != -1) {
Q.push({nx, ny});
v.push_back({nx, ny});
pointMap[nx][ny] = -1;
}
}
}
for (int k = 0; k < v.size(); k++) {
pointMap[v[k].first][v[k].second] = Map[i][j] * v.size();
}
}
}
}
}
void turnMoveDirection() {
if (dice[0] > Map[floorX][floorY]) {
moveDirection = (moveDirection + 1) % 4;
} else if (dice[0] < Map[floorX][floorY]) {
moveDirection = (moveDirection + 3) % 4;
}
}
void diceToRight() {
int tmp = dice[0];
dice[0] = dice[5];
dice[5] = dice[2];
dice[2] = dice[4];
dice[4] = tmp;
}
void diceToDown() {
int tmp = dice[3];
dice[3] = dice[0];
dice[0] = dice[1];
dice[1] = dice[2];
dice[2] = tmp;
}
void checkAndMove() {
// 아랫면 좌표 이동
int x = floorX + dx[moveDirection];
int y = floorY + dy[moveDirection];
if (x <= 0 || x > N || y <= 0 || y > M) {
moveDirection = (moveDirection + 2) % 4;
x = floorX + dx[moveDirection];
y = floorY + dy[moveDirection];
}
floorX = x;
floorY = y;
//주사위 이동
if (moveDirection == 0) {
diceToRight();
} else if (moveDirection == 2) {
for (int i = 0; i < 3; i++) diceToRight();
} else if (moveDirection == 1) {
diceToDown();
} else {
for (int i = 0; i < 3; i++) diceToDown();
}
ans += pointMap[floorX][floorY];
turnMoveDirection();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> K;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
cin >> Map[i][j];
}
}
drawPointMap();
for (int i = 1; i <= K; i++) checkAndMove();
cout << ans << '\n';
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 23290번 : 마법사 상어와 복제 - 시뮬레이션 C++ (0) | 2022.08.28 |
---|---|
백준 23289번 : 온풍기 안녕! - 시뮬레이션 C++ (0) | 2022.08.27 |
백준 21611번: 마법사 상어와 블리자드 - C++ (0) | 2022.08.25 |
백준 21610번 : 마법사 상어와 비바라기 (0) | 2022.08.24 |
백준 21608번 : 상어 초등학교 - 구현, 시뮬레이션 C++ (0) | 2022.07.31 |