티스토리 뷰

알고리즘/BOJ

[BOJ 2178] 미로탐색

히더 2018. 10. 2. 20:50

백준 :: BOJ :: 2178 :: 미로탐색


출처 : https://www.acmicpc.net/problem/2178


#include <bits/stdc++.h>

using namespace std;


struct a {

int i, j, c;

};

int N, M, ans;

int arr[101][101];

int di[] = { 0,0,1,-1 }, dj[] = {1,-1,0,0};

bool visited[101][101];


inline bool check(int i, int j) { return (i > 0 && j > 0 && i <= N && j <= M) ? true : false; }


int bfs() {

queue<a> q;

q.push({ 1, 1, 1 }); visited[1][1] = 1;

while (!q.empty()) {

auto tmp = q.front(); q.pop();

if (tmp.i == N && tmp.j == M) return tmp.c;

for (int d = 0; d < 4; d++) {

int ni = tmp.i + di[d], nj = tmp.j + dj[d];

if (check(ni, nj) && arr[ni][nj] && !visited[ni][nj]) {

q.push({ ni,nj,tmp.c + 1 }); visited[ni][nj] = 1;

}

}

}

}


int main() {

cin >> N >> M; ans = 0;

for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) scanf("%1d", &arr[i][j]);

cout << bfs();

return 0;

}


'알고리즘 > BOJ' 카테고리의 다른 글

[BOJ 14499] 주사위 굴리기  (0) 2018.10.04
[BOJ 7576] 토마토  (0) 2018.10.02
[BOJ 2468] 안전 영역  (0) 2018.09.18
[BOJ 2589] 보물섬  (0) 2018.09.18
[BOJ 2174] 로봇 시뮬레이션  (0) 2018.08.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함