💡 구현/시뮬레이션
Memory 4824KB Time 108ms Code Length 3631B
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 방향 맵 만들기
//입력 : N, 모래맵
//출력 : 토네이도가 소멸되었을때 격자밖으로 나간 모래의 양
#include<iostream>
#include<cstring>
using namespace std;
int N;
int result = 0;
int A[499][499];
struct info {
int y, x, dir;
};
int dy[] = { 0,1,0,-1 }; // 오 아래 왼 위
int dx[] = { 1,0,-1,0 };
int LocMap[499][499];
int percentMap[5][5] = { // 왼쪽으로 갈때
{0,0,2,0,0},
{0,10,7,1,0},
{5,0,0,0,0},
{0,10,7,1,0},
{0,0,2,0,0},
};
void Rotate() { // precentMap 왼쪽으로 돌리기
int tmp[5][5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
tmp[i][j] = percentMap[j][5 - 1 - i]; // 주의!!!
}
}
memcpy(percentMap, tmp, sizeof(percentMap));
}
void printLocMap() {
cout << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << LocMap[i][j] << " ";
}
cout << endl;
}
}
void printpercentMap() {
cout << endl;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << percentMap[i][j] << " ";
}
cout << endl;
}
}
void printAMap(info now) {
cout << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (now.y == i && now.x == j) {
cout << "@" << " ";
}
else {
cout << A[i][j] << " ";
}
}
cout << endl;
}
}
void gamestart() {
info now = { N / 2, N / 2,LocMap[N / 2][N / 2] };
int pastdir = LocMap[now.y][now.x];
while (1) {
if (now.y == 0 && now.x == 0) break;
// 토네이도의 이동
int nextdir = LocMap[now.y][now.x];
int ny = now.y + dy[nextdir];
int nx = now.x + dx[nextdir];
if (nextdir != pastdir) Rotate();
pastdir = nextdir;
// 모래 더해주기
int nowSand = A[ny][nx];
int moved_Sand = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int targety = ny + i - 2;
int targetx = nx + j - 2;
if (targety >= N || targetx >= N || targety < 0 || targetx < 0) { // 범위를 초과하면
result = result + (nowSand * percentMap[i][j] / 100);
moved_Sand += (nowSand * percentMap[i][j] / 100);
}
else {
A[targety][targetx] = A[targety][targetx] + (nowSand * percentMap[i][j] / 100);
moved_Sand += (nowSand * percentMap[i][j] / 100);
}
}
}
// 알파 처리
int alpay = ny + dy[nextdir];
int alpax = nx + dx[nextdir];
if (alpay >= N || alpax >= N || alpay < 0 || alpax < 0) { // 범위를 초과하면
result = result + nowSand - moved_Sand;
}
else {
A[alpay][alpax] = A[alpay][alpax] + nowSand - moved_Sand;
}
A[ny][nx] = 0; // 비워주기
// 이동 완료
now = { ny,nx,nextdir };
//printpercentMap();
//printAMap(now);
}
}
int dirReverse(int dir) {
int changed;
if (dir == 0)changed = 2;
if (dir == 1)changed = 3;
if (dir == 2)changed = 0;
if (dir == 3)changed = 1;
return changed;
}
void LocMapMake() {
int visited[499][499];
memset(visited, 0, sizeof(visited));
int nowy = 0, nowx = 0;
int nowdir = 0;
LocMap[nowy][nowx] = dirReverse(nowdir);
visited[nowy][nowx] = 1;
while (1) {
int ny = nowy + dy[nowdir];
int nx = nowx + dx[nowdir];
if (ny == N / 2 && nx == N / 2) {
LocMap[ny][nx] = dirReverse(nowdir);
break;
}
if (ny >= N || nx >= N || ny < 0 || nx < 0 || visited[ny][nx] == 1) { // 벽을 만나면 방향바꾸기
nowdir = (nowdir + 1) % 4;
}
else { // 이동하기
visited[ny][nx] = 1;
LocMap[ny][nx] = dirReverse(nowdir);
nowy = ny;
nowx = nx;
}
}
}
void init() {
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
LocMapMake();
result = 0;
}
int main() {
init();
gamestart();
cout << result;
}
이 코드는 N x N 크기의 격자와 모래 맵을 입력받아, 토네이도가 움직이면서 모래를 흩뿌리고 소멸시키는 시뮬레이션을 수행하는 프로그램이다.
먼저 토네이도의 이동 방향을 나타내는 LocMap을 만든다. 그 후, 토네이도가 중앙에서 시작하여 이동하면서 모래를 흩뿌리고 소멸시킨다.
토네이도가 이동할 때마다 해당 위치의 모래를 주변으로 흩뿌리고, 격자 밖으로 나간 모래의 양을 계산한다. 토네이도가 이동한 후에는 해당 위치를 비워주고, 알파 위치에 남은 모래를 처리한다.
이렇게 토네이도가 이동하면서 모래를 흩뿌리고 소멸시킨 후, 격자 밖으로 나간 모래의 총 양을 결과로 출력한다.