https://www.acmicpc.net/problem/7576

#include <iostream>
#include <queue>
#include <utility>
using namespace std;
const int MAX = 1000;
int box[MAX + 1][MAX + 1];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int M, N;
cin >> M >> N;
queue<pair<int, int>> q;
int day = 0;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cin >> box[i][j];
if(box[i][j] == 1) q.push({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 nextX = x + dx[i];
int nextY = y + dy[i];
if(nextX >= 0 && nextX < N && nextY >=0 && nextY < M){
if(box[nextX][nextY] == 0){
//cout << nextX << ' ' << nextY << '\n';
box[nextX][nextY] = box[x][y] + 1;
if(box[nextX][nextY] > day) day = box[nextX][nextY] - 1;
q.push({nextX, nextY});
}
}
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
// 안 익은 토마토 존재하는 경우
if(box[i][j] == 0){
cout << -1;
return 0;
}
}
}
cout << day;
return 0;
}
✅ 교훈: 문제에서 주어진 축의 의미를 끝까지 정확히 이해해야 한다. 행렬을 처리할 때는 행과 열의 순서를 절대 헷갈리지 말자!
✅ 교훈: 실전 대회에서는 '코드가 맞는데 왜 안 되지?' 싶을 때, 가장 기본적인 부분부터 차근차근 점검하기!
int rows, cols;
cin >> cols >> rows;