๋ฌธ์ ๋งํฌ
20057๋ฒ: ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋ (acmicpc.net)
20057๋ฒ: ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋
๋ง๋ฒ์ฌ ์์ด๊ฐ ํ ๋ค์ด๋๋ฅผ ๋ฐฐ์ ๊ณ , ์ค๋์ ํ ๋ค์ด๋๋ฅผ ํฌ๊ธฐ๊ฐ N×N์ธ ๊ฒฉ์๋ก ๋๋์ด์ง ๋ชจ๋๋ฐญ์์ ์ฐ์ตํ๋ ค๊ณ ํ๋ค. ์์น (r, c)๋ ๊ฒฉ์์ rํ c์ด์ ์๋ฏธํ๊ณ , A[r][c]๋ (r, c)์ ์๋ ๋ชจ๋์ ์์
www.acmicpc.net
์ ๋ฒ์ ํ์๋ "๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ" ๋ฌธ์ ์ ๋น์ทํ ์ ํ์ ๋ฌธ์ ์ธ๋ฏํ๋ค.
ํ์ด
์ด๋ฒ์ ๋ง๋ฒ์ฌ ์์ด๊ฐ ํ ๋ค์ด๋๋ฅผ ๋ฐฐ์ ๋ค. ํ ๋ค์ด๋๋ ๊ฒฉ์์ ์ค์์ ์ค์ฌ์ผ๋ก ๋ฐ์๊ณ๋ฐฉํฅ์ผ๋ก ์ปค์ง๋ฉด์ ์์ง์ธ๋ค.
ํ ๋ค์ด๋๋ ์ด๋ํ๋ฉด์ ์นธ์ ์๋ ๋ชจ๋๋ฅผ ๋ถ์ฐ์ํค๋๋ฐ x -> y ๋ก ์ผ์ชฝ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ ๊ฒ์ ์๋ก ๋ค์์ ๋, ์ผ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๋น์จ๋ก ๋ชจ๋๋ค์ด ๋ถ์ฐ๋๋ค.
์ํ๋ ๊ฐ ๋น์จ๋งํผ ๋ชจ๋๊ฐ ๋ ์๊ฐ ํ ๋จ์ ๋ชจ๋๊ฐ ์ํ ๋งํผ์ด๋ค. ์ฆ y์ ์๋ ๋ชจ๋๋ ์ ๋ถ ๊ฐ ์นธ์ผ๋ก ๋ถ์ฐ๋๊ณ y์๋ 0์ ๋ชจ๋๊ฐ ๋จ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋๋ฐฉํฅ์ด ๋ฐ๋๋ฉด ์ผ์ชฝ ๊ทธ๋ฆผ์ ํ์ ์์ผ์ ๋๊ฐ์ด ์ ์ฉ์ํค๋ฉด ๋๋ค.
- ํ ๋ค์ด๋์ ํ์ ์ ์ด๋ป๊ฒ ํํํ ๊ฒ์ธ๊ฐ??
๊ทธ๋ฆผ์ผ๋ก ์ฃผ์ด์ง ํ ๋ค์ด๋์ ์งํ์ ๋ณด๊ณ ๊ท์น์ ์ฐพ์๋๋ค. ์ผ๋จ ์ค์ฌ๋ถ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ๋ฐฉํฅ์ผ๋ก ๋จผ์ ์งํ๋๋ค.
๋ฐฉํฅ์ ํ์ ์ผ์ชฝ, ์๋์ชฝ, ์ค๋ฅธ์ชฝ, ์์ชฝ ์์๋๋ก ์ํํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ ์นธ์๋ฅผ ๋ณด๋ฉด 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, ... ์ด๋ ๊ฒ ์งํ๋๋ ๊ฒ์ ์ ์ ์๋ค.
์ฆ 2ํด์ด ์ง๋ ๋ ๋ง๋ค ๊ทธ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ ์นธ์๊ฐ 1 ๋์ด๋๋ค. ๊ทธ๋ฆฌ๊ณ (1,1)์ ๋๋ฌํ๊ฒ ๋๋ฉด ํ ๋ค์ด๋๋ ์ข ๋ฃ๋๋ค.
- ๋ชจ๋์ ๋ถ์ฐ์ ์ด๋ป๊ฒ ๊ตฌํํ ๊ฒ์ธ๊ฐ???
๋ชจ๋์ ๋ถ์ฐ์๋ ํน๋ณํ ๊ท์น์ด ์๊ณ ๊ทธ๋ฅ ์ฃผ์ด์ง ๊ทธ๋ฆผ์ ์ด์ฉํด์ผํ๋ค.
๋๋ ๊ทธ๋ฅ ๋ ธ๊ฐ๋ค๋ก ๋ชจ๋๋ฅผ ๋ถ์ฐ์์ผ์ฃผ๊ณ ๋ง์ฝ ๋ถ์ฐ์ํค๋ ค๋ ์นธ์ด ๋ฒ์ ๋ฐ์ด๋ฉด ans๋ผ๋ ๋ณ์์ ๊ฐ์ ๋ํด์คฌ๋ค.
๊ทธ๋ฆฌ๊ณ ์ํ ์๋ฆฌ์๋ ๊ธฐ์กด์ ๋ชจ๋์์ ๋ถ์ฐ์ํค๊ณ ๋จ์ ๋ชจ๋๋ฅผ ์ฃผ์๋ค. ์ฌ๊ธฐ ์ฃผ์ํ ์ ์ ๊ฐ ๋น์จ๋งํผ ๋ชจ๋๋ฅผ ๋ถ์ฐํ ๋ ์์์ ์๋๊ฐ์ ๋ค ๋ฒ๋ฆฐ๋ค.
์ต์ข ์ ์ผ๋ก (1,1)๊น์ง ์ด๋ํ ํ ans์ ๋ค์ด์๋ ๊ฐ์ด ์ ๋ต์ด ๋๋ค.
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 500 + 5;
int N;
int A[MAX_N][MAX_N];
int ans;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
bool is_ok(pair<int, int>);
void go_left(pair<int,int>);
void go_right(pair<int, int>);
void go_down(pair<int, int>);
void go_up(pair<int, int>);
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
cin >> A[i][j];
pair<int, int> now = { (N + 1) / 2, (N + 1) / 2 };
int Direction = 0;
for (int i = 1; i <= N; i++) {
if (now.first == 1 && now.second == 1)
break;
for (int j = 1; j <= 2; j++) {
for (int k = 1; k <= i; k++) {
pair<int, int> next = { now.first + dy[Direction], now.second + dx[Direction]};
if (Direction == 0)
go_left(next);
else if (Direction == 1)
go_down(next);
else if (Direction == 2)
go_right(next);
else if (Direction == 3)
go_up(next);
now = next;
}
Direction++;
if (Direction == 4)
Direction = 0;
}
}
cout << ans << "\n";
return 0;
}
bool is_ok(pair<int, int> now) {
if (now.first < 1 || now.first > N || now.second < 1 || now.second > N)
return false;
return true;
}
void go_left(pair<int, int> now) {
int Origin_Sand = A[now.first][now.second];
int total = 0;
pair<int,int> next = { now.first - 1, now.second + 1 };
int Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second + 1 };
Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second};
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second};
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second - 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second - 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 2, now.second};
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 2, now.second };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 2 };
Sand = Origin_Sand / 20;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 1 };
Sand = Origin_Sand - total;
if (is_ok(next)) {
A[next.first][next.second] += Sand;
}
else
ans += Sand;
A[now.first][now.second] = 0;
}
void go_right(pair<int, int> now) {
int Origin_Sand = A[now.first][now.second];
int total = 0;
pair<int, int> next = { now.first - 1, now.second - 1 };
int Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second - 1 };
Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second };
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second };
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second + 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second + 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 2, now.second };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 2, now.second };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 2 };
Sand = Origin_Sand / 20;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 1 };
Sand = Origin_Sand - total;
if (is_ok(next)) {
A[next.first][next.second] += Sand;
}
else
ans += Sand;
A[now.first][now.second] = 0;
}
void go_down(pair<int, int> now) {
int Origin_Sand = A[now.first][now.second];
int total = 0;
pair<int, int> next = { now.first - 1, now.second + 1 };
int Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second - 1 };
Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 1};
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 1 };
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second - 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second + 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 2 };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 2};
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 2, now.second};
Sand = Origin_Sand / 20;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second};
Sand = Origin_Sand - total;
if (is_ok(next)) {
A[next.first][next.second] += Sand;
}
else
ans += Sand;
A[now.first][now.second] = 0;
}
void go_up(pair<int, int> now) {
int Origin_Sand = A[now.first][now.second];
int total = 0;
pair<int, int> next = { now.first + 1, now.second + 1 };
int Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first + 1, now.second - 1 };
Sand = Origin_Sand / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 1 };
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 1 };
Sand = Origin_Sand * 7 / 100;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second - 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second + 1 };
Sand = Origin_Sand / 10;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second - 2 };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first, now.second + 2 };
Sand = Origin_Sand / 50;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 2, now.second };
Sand = Origin_Sand / 20;
if (is_ok(next)) {
total += Sand;
A[next.first][next.second] += Sand;
}
else {
ans += Sand;
total += Sand;
}
next = { now.first - 1, now.second };
Sand = Origin_Sand - total;
if (is_ok(next)) {
A[next.first][next.second] += Sand;
}
else
ans += Sand;
A[now.first][now.second] = 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 21608 : ์์ด ์ด๋ฑํ๊ต (0) | 2022.05.31 |
---|---|
[BOJ] 20058 : ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ (0) | 2022.05.31 |
[BOJ] 20056 : ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ (0) | 2022.05.24 |
[BOJ] 20055 : ์ปจ๋ฒ ์ด์ด ๋ฒจํธ ์์ ๋ก๋ด (0) | 2022.05.23 |
[BOJ] 19238 : ์คํํธ ํ์ (0) | 2022.05.23 |