๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 20057 : ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํ† ๋„ค์ด๋„

by Jaeguk 2022. 5. 30.
๋ฌธ์ œ ๋งํฌ

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<intint>vector<pair<intint>>, greater<pair<intint>>> 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<intint>);
void go_left(pair<int,int>);
void go_right(pair<intint>);
void go_down(pair<intint>);
void go_up(pair<intint>);
 
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<intint> 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<intint> 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<intint> now) {
    if (now.first < 1 || now.first > N || now.second < 1 || now.second > N)
        return false;
    return true;
}
 
void go_left(pair<intint> 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<intint> now) {
    int Origin_Sand = A[now.first][now.second];
    int total = 0;
 
    pair<intint> 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<intint> now) {
    int Origin_Sand = A[now.first][now.second];
    int total = 0;
    pair<intint> 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<intint> now) {
    int Origin_Sand = A[now.first][now.second];
    int total = 0;
 
    pair<intint> 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

728x90