반응형
https://www.acmicpc.net/problem/23288
23288번: 주사위 굴리기 2
크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼
www.acmicpc.net
[ 문제풀이 ]
1. 맵을 입력받습니다.
2. 주사위의 초기 상태를 하드 코딩합니다.
3. 각 방향별로 방향 배열을 만듭니다. (0:동쪽, 1:남쪽, 2:서쪽, 3:북쪽)
4. 주사위의 첫 방향은 동쪽이므로 동쪽으로 한 칸 움직입니다.
5. 이어져 있는 같은 숫자 * 그 개수만큼 점수를 더합니다.
6. 주사위 아랫면의 숫자와 맵의 숫자를 비교해 이동 방향을 결정합니다.
7. 해당 이동 방향으로 움직일 수 없을 시 정반대 방향으로 움직입니다.
8. 위 과정을 계속 반복합니다.
[ 소스코드 ]
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include<iostream> #include<queue> using namespace std; int N, M, K; int arr[20][20]; int dy[4] = { 0,1,0,-1 }; int dx[4] = { 1,0,-1,0 }; int dir; int top = 1; int dice[3][3] = { //초기 주사위 0,2,0, 4,6,3, 0,5,0 }; int y, x; int ans; int bfs(int y, int x) //같은 숫자가 이어져 있는 개수 return { queue<pair<int, int>> q; int visited[20][20] = { 0 }; q.push({ y,x }); visited[y][x] = 1; int cnt = 1; while (!q.empty()) { int y = q.front().first; int x = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy >= 0 && yy < N && xx >= 0 && xx < M) { if (visited[yy][xx] != 1 && arr[y][x] == arr[yy][xx]) { visited[yy][xx] = 1; cnt++; q.push({ yy,xx }); } } } } return cnt; } void move(int dir) //주사위 이동 { if (dir == 0) { int temp = top; top = dice[1][0]; for (int i = 0; i < 2; i++) { dice[1][i] = dice[1][i + 1]; } dice[1][2] = temp; } else if (dir == 1) { int temp = top; top = dice[0][1]; for (int i = 0; i < 2; i++) { dice[i][1] = dice[i+1][1]; } dice[2][1] = temp; } else if (dir == 2) { int temp = top; top = dice[1][2]; for (int i = 2; i > 0; i--) { dice[1][i] = dice[1][i - 1]; } dice[1][0] = temp; } else { int temp = top; top = dice[2][1]; for (int i = 2; i > 0; i--) { dice[i][1] = dice[i - 1][1]; } dice[0][1] = temp; } } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%d", &arr[i][j]); } } for (int i = 0; i < K; i++) { int yy = y + dy[dir]; int xx = x + dx[dir]; if (yy >= 0 && yy < N && xx >= 0 && xx < M) { move(dir); ans += arr[yy][xx] * bfs(yy, xx); } else { dir += 2; if (dir > 3) dir %= 4; if (dir < 0) dir += 4; move(dir); yy = y + dy[dir]; xx = x + dx[dir]; ans += arr[yy][xx] * bfs(yy, xx); } y = yy; x = xx; if (arr[y][x] > dice[1][1]) { dir--; } else if (arr[y][x] < dice[1][1]) { dir++; } if (dir > 3) dir = 0; if (dir < 0) dir = 3; } printf("%d", ans); } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 23290번 - 마법사 상어와 복제 (C++) (0) | 2022.10.05 |
---|---|
[ 백준 ] 23289번 - 온풍기 안녕! (C++) (0) | 2022.10.04 |
[ 백준 ] 21609번 - 상어 중학교 (C++) (0) | 2022.10.02 |
[ 백준 ] 19238번 - 스타트 택시 (C++) (0) | 2022.10.01 |
[ 백준 ] 19237번 - 어른 상어 (C++) (0) | 2022.09.30 |