์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- java
- c++
- Spring
- error
- ์ฌ๊ท
- ๋ฐ์ดํฌ์คํธ๋ผ
- GCP
- BFS
- ์๋ฎฌ๋ ์ด์
- dfs
- JPA
- ๊ตฌํ
- Reversing
- OS
- DP
- ์คํ
- ๋ฌธ์์ด
- CVE
- ๊ทธ๋ฆฌ๋
- ์ด๋ถ ํ์
- ๋ฐฑํธ๋ํน
- ๋์ ํฉ
- ์ฐ์ ์์ ํ
- thymeleaf
- dynamic debugging
- ์์ ์ ๋ ฌ
- ๋งต
- web
- ์ต๋จ ๊ฒฝ๋ก
- ๋ถํ ์ ๋ณต
- Today
- Total
hades
[Baekjoon] 2638๋ฒ: ์น์ฆ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/2638
๐ ์ค๊ณ
์น์ฆ๊ฐ ๋ฟ์์๋ ๋ธ๋ญ์ด 2๊ฐ ์ด์์ด๋ฉด ๋ น์ ์์ ์ด๋ค. ์น์ฆ ๋ด๋ถ์ ์๋ ๊ณต๊ธฐ๋ ์น์ฆ์ ์ํฅ์ ์ค ์ ์๋ค. ๋ชจ๋์ข ์ด์ ๋งจ ๊ฐ์ฅ์๋ฆฌ๋ค์ ์น์ฆ๊ฐ ๋์ด์ง ์๋๋ค๊ณ ํ์ผ๋ฏ๋ก, ๊ทธ ์ค ๋ํ์ ์ธ (0, 0) ์์ ์์ํ์ฌ ์ธ๋ถ ๊ณต๊ธฐ์ ์ํฅ๋ ฅ์ ํ์ฌํ๋ค.
์ธ๋ถ ๊ณต๊ธฐ์ ์ํฅ๋ ฅ์ ํ์ฌํ๋ ๋ฐฉ๋ฒ์ BFS๋ฅผ ์ด์ฉํ์ฌ ์น์ฆ๊ฐ ๋์ธ ๊ณณ์ 2๋ฒ ๋ฐฉ๋ฌธํ์ ๋, 1์๊ฐ ํ์ ๋ น์ผ๋ฉด์ ์๋กญ๊ฒ ์ํฅ๋ ฅ์ ํ์ฌํ ์ ์๋ ์ธ๋ถ ๊ณต๊ธฐ ํ๋ณด๋ค์ ๋ชจ์์ธ next_q์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค. ๋ง์ฝ next_q์ ์๋ฌด๊ฒ๋ ์ถ๊ฐ๋์ง ์๋๋ค๋ฉด ๊ทธ๋๋ก ์ข ๋ฃ๋๊ณ , ์ถ๊ฐ๋ ๊ฒ์ด ์๋ค๋ฉด 1์ด๋ฅผ ์ฆ๊ฐ์ํจ๋ค. next_q์ ์ถ๊ฐํ๋ ์ด์ ๋ ์น์ฆ๊ฐ ๋ น๋ ๊ณผ์ ์ 1์๊ฐ ํ์ ๋์์ ์ด๋ฃจ์ด์ ธ์ผ ํ๊ธฐ ๋๋ฌธ๋ ์๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, result = 0;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
vector<vector<int>> paper(100, vector<int>(100));
vector<vector<int>> visited(100, vector<int>(100));
void bfs() {
queue<pair<int, int>> q;
queue<pair<int, int>> next_q;
q.push({ 0, 0 });
visited[0][0] = 1;
while (true) {
while (!q.empty()) {
int cur_x = q.front().first;
int cur_y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int new_x = cur_x + dx[i];
int new_y = cur_y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < m)) {
continue;
}
if (paper[new_x][new_y] == 0 && visited[new_x][new_y] == 0) {
visited[new_x][new_y] += 1;
q.push({ new_x, new_y });
}
else if (paper[new_x][new_y] == 1) {
visited[new_x][new_y] += 1;
if (visited[new_x][new_y] == 2) {
next_q.push({ new_x, new_y });
}
}
}
}
if (next_q.empty()) {
break;
}
else {
result += 1;
}
while (!next_q.empty()) {
int new_x = next_q.front().first;
int new_y = next_q.front().second;
q.push(next_q.front());
next_q.pop();
paper[new_x][new_y] = 0;
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> paper[i][j];
}
}
bfs();
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1932๋ฒ: ์ ์ ์ผ๊ฐํ (0) | 2024.08.19 |
---|---|
[Baekjoon] 15666๋ฒ: N๊ณผ M (12) (0) | 2024.08.13 |
15654๋ฒ: N๊ณผ M (5) (0) | 2024.08.12 |
[Baekjoon] 9465๋ฒ: ์คํฐ์ปค (0) | 2024.08.11 |
[Baekjoon] 2096๋ฒ: ๋ด๋ ค๊ฐ๊ธฐ (0) | 2024.08.10 |