알고리즘/백준
17086. 아기 상어 2
혬혬
2021. 10. 31. 00:08
728x90
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
freopen("inp.inp", "r", stdin);
freopen("out.out", "w", stdout);
int r, c;
cin >> r >> c;
int map[60][60] = { 0 };
int check[60][60] = { 0 };
queue<pair<int, int>> q;
for(int i=0;i<r;i++){
for (int j = 0; j < c; j++) {
cin >> map[i][j];
if (map[i][j] == 1) {
check[i][j] = 1;
q.push({ i,j });
}
}
}
int dx[] = { -1,-1,0,1,1,1,0,-1 };
int dy[] = { 0,1,1,1,0,-1,-1,-1 };
int max = 0;
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
for (int k = 0; k < 8; k++) {
if (i + dx[k] >= 0 && i + dx[k] < r&&j + dy[k] >= 0 && j + dy[k] < c) {
if (check[i + dx[k]][j + dy[k]] == 0) {
q.push({ i + dx[k],j + dy[k] });
check[i + dx[k]][j + dy[k]] = check[i][j] + 1;
if (max < check[i][j] + 1)
max = check[i][j] + 1;
}
}
}
}
cout << max-1 << endl;
return 0;
}
https://www.acmicpc.net/problem/17086
728x90