안녕하세요. 하학생입니다.
오늘 정말로 어려운 문제를 가져왔습니다.
백준 7576 풀어봤습니다. 재미있게 봐주세요!
두 정수 M,N을 입력으로 받고 토마토의 상태를 입력습니다.
#include <iostream>
#include<vector>
#include<queue>
using namespace std;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int N,M,result=0;
int graph[1001][1001];
struct tomato {
int y, x;
};
bool Inside(int ny, int nx) {
return (0 <= nx && 0 <= ny && nx < M && ny < N);
}
queue<tomato> q;
void bfs(void) {
while (!q.empty()) {
int y = q.front().y;
int x = q.front().x;
q.pop();
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (Inside(ny,nx)==1 && graph[ny][nx] == 0) {
graph[ny][nx] = graph[y][x] + 1;
q.push({ ny, nx });
}
}
}
}
int main(void)
{
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)
{
q.push({i,j});
}
}
}
bfs();
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(graph[i][j]==0)
{
cout<<-1<<"\n";
return 0;
}
if(result<graph[i][j])
{
result=graph[i][j];
}
}
}
cout<<result-1<<"\n";
return 0;
}
C++ 백준 7662 (1) | 2023.10.02 |
---|---|
C++ 백준 1927,11279,11286 최소 힙, 최대 힙, 절댓값 힙 (0) | 2023.09.23 |
C++ 백준 1158번 (0) | 2023.09.15 |
C++ 백준 10845번 (0) | 2023.09.14 |
C++ 백준 10828번 (0) | 2023.09.14 |
댓글 영역