#include <stdio.h> #include <iostream> #include <vector> #include <queue> using namespace std; typedef struct node { int x; int y; int crash; }node; int row, col; int map[110][110]; int visited[110][110][210]; queue<node> 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, int crash) { q.push({x, y, crash}); visited[x][y][crash] = 1; while(!q.empty()) { x = q.front().x; y = q.front().y; crash = q.front().crash; 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] == 0 && visited[xpos][ypos][crash] == 0 && crash <= row+col-3) { q.push({xpos, ypos, crash}); visited[xpos][ypos][crash] = 1; } else if(safe(xpos, ypos) == 1 && map[xpos][ypos] == 1 && visited[xpos][ypos][crash+1] == 0 && crash+1 <= row+col-3) { q.push({xpos, ypos, crash+1}); visited[xpos][ypos][crash+1] = 1; } } } } int main(void) { // freopen("B1261_input.txt", "r", stdin); cin >> col >> row; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { scanf("%1d", &map[i][j]); } } BFS(0, 0, 0); for(int i = 0; i <= row+col-3; i++) { if(visited[row-1][col-1][i] == 1) { cout << i; break; } } return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 2589] 보물섬 (BFS) (C/C++) (0) | 2020.04.07 |
---|---|
[백준 1938] 통나무 옮기기 (BFS) (C/C++) (★★★) (0) | 2020.02.19 |
[백준 1167] 트리의 지름 (BFS, Tree) (C/C++) (★) (0) | 2020.02.16 |
[백준 1967] 트리의 지름 (BFS, Tree) (C/C++) (0) | 2020.02.16 |
[백준 2146] 다리 만들기 (BFS) (C/C++) (0) | 2020.01.28 |