BOJ-7576
12 Aug 2021๐
BFS
๐
๋ฌธ์ ์ธ๋ฐ, ๊ฐ๋
์ ์๋๋ฐ ์ฝ๋๋ก ๊ตฌํํ๋๊ฒ ํท๊ฐ๋ ค์ Geeks for Geeks์ ๊ตฌ๊ธ๋ง์ ์ฐธ๊ณ ํ๋คโฆ ์๋ ์ ๋ง ๋ ์ ์ด๋ ๊ฒ ๋ฉ์ฒญํ๊ฑฐ๋โฆ
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
pair<int, int> p;
queue<pair<int, int>> q;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0 , -1 };
int n, m, result = 0;
int graph[1001][1001];
bool inside(int ny, int nx) {
return (0 <= nx && 0 <= ny && nx < m && ny < n);
}
void bfs(void) {
while (!q.empty()) {
int y = q.front().first;
int x = q.front().second;
q.pop();
for (int i=0; i<4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (inside(ny,nx) && graph[ny][nx] == 0) {
graph[ny][nx] = graph[y][x] + 1;
pair<int, int> temp = make_pair(ny, nx);
q.push(temp);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int m,n;
cin >> m >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> graph[i][j];
if (graph[i][j] == 1) {
pair<int, int> temp = make_pair(i,j);
q.push(temp);
}
}
}
bfs();
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (graph[i][j] == 0) {
cout << "-1" << endl;
return 0;
}
if (result < graph[i][j]) {
result = graph[i][j];
}
}
}
cout << result-1 << endl;
return 0;
}
์ค๋์ TMI
- GitHub Repo์ ๋ค์ด๊ฐ๋ค
- Repo ํ๋ฉด์์
.
ํค๋ฅผ ๋๋ฅธ๋ค - Online VSCode Editor๋ฅผ ํตํด ์ฝ๋ ์์ ์ด ๊ฐ๋ฅํ๋ค
LinkedIn์์ ์ด ๊ธ์ ๊ณ์ ๋ดค์๋๋ฐ, ์ ๊ฒ ๋ญ์ง(?) ํ๋ฉด์ ํธ๋ํฐ ๋ณด๋ค๊ฐ ์ง์ ์์ ํด๋ณด๋ ์ด๊ฒ ๋๋คโฆ!!!๐ฑโจ ๋๋ฌด ์ ๊ธฐํดโฆ ๊ฐ๋ ๊ท์ฐฎ์ ๋๋ GitHub Web์์ ์ฝ๋ ์์ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋๋ฐ, ์ด๋ ๊ฒ ํธํ๊ฒ ์์ ํ ์ ์๋ค๋ ๋๋ฌด ์ ๊ธฐํ ๊ฒโฆ..!!! ์ด์จ๋ Online GitHub VS Code Editor๋ก ์จ๋ณด๋ ์ฒซ ๋ธ๋ก๊ทธ ๊ธ! ์๋ ๐๐ผ๐๐๐ผ๐๐ผ