#include <stdio.h> #include <iostream> #include <set> #include <queue> #include <vector> #include <string> #include <algorithm> using namespace std; typedef struct node { int x; int y; int crash; }node; int map[110][110]; int row, col; int Min = 9999999; queue<node> q; int visited[110][110][210]; 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() { visited[0][0][0] = 1; q.push({0, 0, 0}); while(!q.empty()) { int x = q.front().x; int y = q.front().y; int crash = q.front().crash; q.pop(); if(x == row-1 && y == col-1) { if(crash < Min) { Min = crash; } } 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-2) { visited[xpos][ypos][crash] = 1; q.push({xpos, ypos, crash}); } else if(safe(xpos, ypos) == 1 && map[xpos][ypos] == 1 && visited[xpos][ypos][crash+1] == 0 && crash+1 <= row+col-2) { visited[xpos][ypos][crash+1] = 1; q.push({xpos, ypos, crash+1}); } } } } int main(void) { // freopen("B1261_input.txt", "r", stdin); scanf("%d %d", &col, &row); for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { scanf("%1d", &map[i][j]); } } BFS(); printf("%d", Min); return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 2573] 빙산 (BFS) (C/C++) (★) (0) | 2019.12.06 |
---|---|
[백준 1389] 케빈 베이컨의 6단계 법칙 (BFS) (C/C++) (0) | 2019.12.06 |
[백준 14395] 4연산 (BFS) (C/C++) (0) | 2019.12.06 |
[백준 7576] 토마토 (BFS) (C/C++) (0) | 2019.12.06 |
[백준 5427] 불 (BFS) (C/C++) (★) (0) | 2019.12.05 |