- 문제 링크 : boj.kr/7576
- 난이도 : G5
- 태그 : BFS
7576번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토
www.acmicpc.net
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int board[1001][1001];
int m, n;
bool mm(int x, int y) {
if(x < 0 || x > n-1 || y < 0 || y > m-1) return false;
if(board[x][y] != 0) return false;
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> m >> n;
int cnt = 0;
queue<pair<int, pii>> _q;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> board[i][j];
if(board[i][j] != -1) cnt++;
if(board[i][j] == 1) _q.push({0,{i,j}});
}
}
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int ans = 0;
while(!_q.empty()) {
cnt--;
auto u = _q.front(); _q.pop();
ans = max(ans, u.first);
pii pos = u.second;
for(int i = 0; i < 4; i++) {
if(mm(pos.first+dx[i],pos.second+dy[i])) {
board[pos.first+dx[i]][pos.second+dy[i]] = 1;
_q.push({u.first+1,{pos.first+dx[i],pos.second+dy[i]}});
}
}
}
if(cnt == 0) cout << ans;
else cout << -1;
return 0;
}
풀이
전형적인 BFS문제이다.
728x90
'PS' 카테고리의 다른 글
BOJ 5430 : AC (0) | 2024.03.23 |
---|---|
BOJ 7569 : 토마토 (0) | 2024.03.22 |
BOJ 30961 : 최솟값, 최댓값 (0) | 2024.03.20 |
BOJ 11108 : TV 전쟁 (0) | 2024.03.19 |
BOJ 30878 : 약속 시간 (0) | 2024.03.18 |