Taene's
[백준] 실버1-미로 탐색 C++ 2178번 본문
https://www.acmicpc.net/problem/2178
// 가중치가 같은 그래프의 최단거리 알고리즘 bfs
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int N, M;
int adj[100][100];
int visited[100][100];
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };
void bfs(int y, int x)
{
queue<pair<int, int>> q;
visited[y][x] = 1;
q.push({ y,x });
while (!q.empty())
{
int a, b;
tie(a, b) = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int ny = a + dy[i];
int nx = b + dx[i];
if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue;
if (visited[ny][nx]) continue;
if (!adj[ny][nx]) continue;
visited[ny][nx] = visited[a][b] + 1;
q.push({ ny,nx });
}
}
cout << visited[N - 1][M - 1];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++)
{
string temp;
cin >> temp;
for (int j = 0; j < M; j++)
{
adj[i][j] = (int)(temp[j] - '0');
}
}
bfs(0, 0);
return 0;
}
'백준 > 실버' 카테고리의 다른 글
[백준] 실버1-쿼드트리 C++ 1992번 (0) | 2025.02.23 |
---|---|
[백준] 실버1-영역 구하기 C++ 2583번 (0) | 2025.02.23 |
[백준] 실버1-안전 영역 C++ 2468번 (0) | 2025.02.22 |
[백준] 실버2-유기농 배추 C++ 1012번 (0) | 2025.02.22 |
[백준] 실버3-한국이 그리울 땐 서버에 접속하지 C++ 9996번 (0) | 2025.02.22 |