#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include <string.h> #include <math.h> #include <queue> #include <vector> using namespace std; int row, col; int map[110][110]; int visited[110][110]; queue<pair<int, int>> q; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int safe(int x, int y) { if(x >= 0 && y >= 0 && x < row && y < col) { return 1; } else { return 0; } } void BFS(int x, int y) { q.push({x, y}); visited[x][y] = 1; while(!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); for(int i = 0; i < 4; i++) { int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1 && map[xpos][ypos] == 1 && visited[xpos][ypos] == 0) { q.push({xpos, ypos}); visited[xpos][ypos] = visited[x][y] + 1; } } } } int main(void) { // freopen("B2178_input.txt", "r", stdin); cin >> row >> col; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { scanf("%1d", &map[i][j]); } } BFS(0, 0); cout << visited[row-1][col-1]; return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 1967] 트리의 지름 (BFS, Tree) (C/C++) (0) | 2020.02.16 |
---|---|
[백준 2146] 다리 만들기 (BFS) (C/C++) (0) | 2020.01.28 |
[백준 7576] 토마토 (BFS) (C/C++) (0) | 2020.01.26 |
[백준 1707] 이분 그래프 (BFS) (C/C++) (★) (0) | 2020.01.26 |
[백준 1525] 퍼즐 (BFS) (C/C++) (★★) (0) | 2019.12.13 |