์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- error
- ์๋ฎฌ๋ ์ด์
- CVE
- thymeleaf
- ๊ทธ๋ฆฌ๋
- ์ด๋ถ ํ์
- ๋์ ํฉ
- dfs
- web
- ๊ตฌํ
- DP
- ๋ฐฑํธ๋ํน
- ๋ฌธ์์ด
- ๋ฐ์ดํฌ์คํธ๋ผ
- BFS
- OS
- ์คํ
- java
- ์ฐ์ ์์ ํ
- JPA
- c++
- Reversing
- dynamic debugging
- ์ฌ๊ท
- GCP
- Spring
- ๋งต
- ์ต๋จ ๊ฒฝ๋ก
- ๋ถํ ์ ๋ณต
- ์์ ์ ๋ ฌ
- Today
- Total
hades
[Baekjoon] 16236๋ฒ: ์๊ธฐ ์์ด ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/16236
๐ ์ค๊ณ
์ด๊ธฐ ์ค๊ณ์์๋ ์ด๋ฅผ ๋๋ฆฌ๋ฉด์, ๊ฑฐ๋ฆฌ๊ฐ ๋์ผํ ๋๋ ์, ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋ ์์ผ๋ก ์ฐ์ ์์๊ฐ ๋์ผ๋ฏ๋ก, ํ์ ์์๋ ์, ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋ ์์ผ๋ก ํ์ํ๋ค.
๋จน์ ์ ์๋ ๊ฒ์ด ์์ผ๋ฉด, ๋ฐ๋ก ๋จน๊ณ , ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐํํ๋ค.
๋จน์ ์ ์์ผ๋ฉด, ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์ ํ๊ณ , ํ์ ๋ด๋๋ค.
๋ฐํ๋ ๊ฑฐ๋ฆฌ๊ฐ 0์ด ์๋๋ฉด, ๋จน์ ์ ์๋ ๊ฒ์ด ์๋ค๋ ์๋ฏธ์ด๋ฏ๋ก, ์ต์ข ๊ฒฐ๊ณผ์ ๋ํ๋ค.
๋ฐํ๋ ๊ฑฐ๋ฆฌ๊ฐ 0์ด๋ฉด ๋จน์ ์ ์๋ ๊ฒ์ด ๋ ์ด์ ์๋ค๋ ์๋ฏธ์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ์ข ๋ฃํ๋ค.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, baby_shark_size = 2, eat_count = 0, baby_shark_x, baby_shark_y, plus_dist, result = 0;
int dx[] = { -1,0,0,1 };
int dy[] = { 0,-1,1,0 };
vector<vector<int>> space(20, vector<int>(20));
vector<vector<int>> dist(20, vector<int>(20, 1e9 + 7));
void grow() {
if (eat_count == baby_shark_size) {
baby_shark_size += 1;
eat_count = 0;
}
}
int bfs() {
int cur_dist = 0;
queue<pair<int, int>> q;
q.push({ baby_shark_x, baby_shark_y });
dist[baby_shark_x][baby_shark_y] = 0;
while (!q.empty()) {
int size = q.size();
cur_dist += 1;
for (int i = 0; i < size; i++) {
int cur_x = q.front().first;
int cur_y = q.front().second;
q.pop();
for (int j = 0; j < 4; j++) {
int new_x = cur_x + dx[j];
int new_y = cur_y + dy[j];
if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < n) {
if (dist[new_x][new_y] < cur_dist) {
continue;
}
if ((space[new_x][new_y] == 0) || (space[new_x][new_y] == baby_shark_size)) {
dist[new_x][new_y] = cur_dist;
q.push({ new_x, new_y });
}
else if (space[new_x][new_y] < baby_shark_size) {
eat_count += 1;
baby_shark_x = new_x;
baby_shark_y = new_y;
space[new_x][new_y] = 0;
grow();
return cur_dist;
}
}
}
}
}
return 0;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> space[i][j];
if (space[i][j] == 9) {
baby_shark_x = i;
baby_shark_y = j;
space[i][j] = 0;
}
}
}
while (true) {
dist = vector<vector<int>>(20, vector<int>(20, 1e9 + 7));
plus_dist = bfs();
if (plus_dist == 0) {
break;
}
else {
result += plus_dist;
}
}
cout << result << "\n";
return 0;
}
์ต์ข ์ ์ผ๋ก ์์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์๋ค. ๊ทธ๋ฐ๋ฐ ์์ ์ ๋ ฅ 4๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ค.
์ด์ ๋ฅผ ์์๋ณด๋
5 4 0 0 3 4
4 3 0 3 4 5
3 2 0 5 6 6
2 0 0 3 4 5
3 2 0 6 5 4
6 6 6 6 6 6
5 4 0 0 3 4
4 0 0 3 4 5
3 2 0 5 6 6
2 0 0 3 4 5
3 2 0 6 5 4
6 6 6 6 6 6
์์ ์์น๋ ๋ ธ๋์์ด๊ณ , ํ๋์์ด 0์ด ๋์ด์ผ ํ๋๋ฐ, ๋นจ๊ฐ์์ด 0์ด ๋์๋ค. ๋นจ๊ฐ์๊ณผ ํ๋์๊น์ง์ ๊ฑฐ๋ฆฌ๋ ๊ฐ์ง๋ง, ์, ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋ ์์ผ๋ก ํ์์ ๋ฐ๋ณตํ๋ฏ๋ก, ์ผ์ชฝ+์๋๊ฐ ์ค๋ฅธ์ชฝ+์ค๋ฅธ์ชฝ๋ณด๋ค ๋จผ์ ํ์๋๊ฒ ๋๋ค.
๊ทธ๋์, ๋จ์ํ๊ฒ ์ฒ์ ์๊ธฐ ์์ด์ ์์น์์ ์ ๊ทผํ ์ ์๋ ๋ชจ๋ ๋จน์ด๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ณ , ์ต์ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋ ๋ฐฉ์์ผ๋ก ๋ฐ๊พธ์๋ค.
์ฃผ์ํด์ผ ํ ๊ฒ์ ์๊ธฐ ์์ด๊ฐ ๋จน์ด๋ฅผ ๋จน์ผ๋ฉด, ๊ทธ ์์น๋ 0์ผ๋ก ๊ฐฑ์ ํ๋ฏ๋ก, ์ต์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ๋ ๋จน์ด์ ์์น๋ฅผ ์ฐพ์ ๋, ๊ทธ ์นธ์ ์๋ ์๊ฐ 0 ๋ณด๋ค ์ปค์ผ ํ๋ค๋ ๊ฒ์ ์์ง ๋ง์.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, baby_shark_size = 2, eat_count = 0, baby_shark_x, baby_shark_y, result = 0, min_value, temp_x, temp_y;
int dx[] = { -1,0,0,1 };
int dy[] = { 0,-1,1,0 };
vector<vector<int>> space(20, vector<int>(20));
vector<vector<int>> dist(20, vector<int>(20, 1e9 + 7));
void grow() {
if (eat_count == baby_shark_size) {
baby_shark_size += 1;
eat_count = 0;
}
}
void bfs() {
int cur_dist = 0;
queue<pair<int, int>> q;
q.push({ baby_shark_x, baby_shark_y });
dist[baby_shark_x][baby_shark_y] = 0;
while (!q.empty()) {
int size = q.size();
cur_dist += 1;
for (int i = 0; i < size; i++) {
int cur_x = q.front().first;
int cur_y = q.front().second;
q.pop();
for (int j = 0; j < 4; j++) {
int new_x = cur_x + dx[j];
int new_y = cur_y + dy[j];
if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < n) {
if (dist[new_x][new_y] > cur_dist) {
if (space[new_x][new_y] <= baby_shark_size) {
dist[new_x][new_y] = cur_dist;
q.push({ new_x, new_y });
}
}
}
}
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> space[i][j];
if (space[i][j] == 9) {
baby_shark_x = i;
baby_shark_y = j;
space[i][j] = 0;
}
}
}
while (true) {
dist = vector<vector<int>>(20, vector<int>(20, 1e9 + 7));
bfs();
min_value = 1e9 + 7;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (baby_shark_size > space[i][j] && space[i][j] > 0) {
if (dist[i][j] > 0 && min_value > dist[i][j]) {
baby_shark_x = i;
baby_shark_y = j;
min_value = dist[i][j];
}
}
}
}
if (min_value == 1e9 + 7) {
break;
}
eat_count += 1;
space[baby_shark_x][baby_shark_y] = 0;
grow();
result += min_value;
}
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 15486๋ฒ: ํด์ฌ 2 (0) | 2024.09.10 |
---|---|
[Baekjoon] 30805๋ฒ: ์ฌ์ ์ ์ต๋ ๊ณตํต ๋ถ๋ถ ์์ด (0) | 2024.09.09 |
[Baekjoon] 5639๋ฒ: ์ด์ง ๊ฒ์ ํธ๋ฆฌ (0) | 2024.09.06 |
[Baekjoon] 17144๋ฒ: ๋ฏธ์ธ๋จผ์ง ์๋ ! (0) | 2024.09.05 |
[Baekjoon] 9935๋ฒ: ๋ฌธ์์ด ํญ๋ฐ (0) | 2024.09.04 |