#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> G(N, vector<int>(M, 0));
vector<vector<int>> visited(N, vector<int>(M, 0));
queue<pair<int, int>> Q;
pair<int, int> v;
int row, col, nRow, nCol;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> G[i][j];
if (G[i][j] == 2) {
v = { i, j };
}
}
}
Q.push(v);
while (!Q.empty()) {
v = Q.front();
row = v.first;
col = v.second;
Q.pop();
for (int d = 0; d < 4; d++) {
nRow = row + dx[d];
nCol = col + dy[d];
if (0 > nRow || nRow >= N || 0 > nCol || nCol >= M) continue;
if (visited[nRow][nCol]) continue;
if (G[nRow][nCol] == 0 || G[nRow][nCol] == 2) continue;
visited[nRow][nCol] = visited[row][col] + 1;
Q.push({ nRow,nCol });
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (visited[i][j] == 0 && G[i][j] == 1) {
cout << -1 << " ";
}
else {
cout << visited[i][j] << " ";
}
}
cout << endl;
}
return 0;
}
[실수 목록]
1) queue의 pop 연산을 한뒤에 front()를 호출하여 에러 발생
2) G[nRow][nCol] == 2 조건 누락.
3) -1 을 출력 하는 조건 누락.
'알고리즘 > 백준' 카테고리의 다른 글
[C++/Python] 백준 13414 : 수강신청 (1) | 2024.02.13 |
---|---|
[C++] 백준 2210 : 숫자판 점프 (0) | 2024.02.13 |
[C++] 백준 1021 : 회전하는 큐 (1) | 2024.02.07 |
[C++] 백준 2223 : 금화 (0) | 2024.02.06 |
[C++] 백준 17087 : 숨바꼭질 6 (0) | 2024.01.29 |