[BOJ] 7576 토마토

Time Lapse :NONE

7576.cpp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string>
#include <queue>
using namespace std;

int map[1000][1000];
int N,M;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int ans = -1;
void BFS()
{
queue<pair<int,int>> q;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
if(map[i][j]==1) {
q.push(make_pair(i, j));
}

while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int next_x = x+dx[i];
int next_y = y+dy[i];
if(next_x<0||next_x>=N||next_y<0||next_y>=M)
continue;
if(map[next_x][next_y]==0)
{
map[next_x][next_y] = map[x][y]+1;
q.push(make_pair(next_x,next_y));
}
}
}
}

int main(void)
{

cin>>M>>N;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>map[i][j];
BFS();


for(int i=0;i<N;i++)
for(int j=0;j<M;j++){
if(map[i][j]==0){
cout<<"-1"<<endl;
return 0;
}
else{
ans = max(ans,map[i][j]);
}
}

cout<<ans-1<<endl;
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/7576/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.