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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> a(101);
int check[102][102];
int dr[4= { 1-100 };
int dc[4= { 001-1 };
int main()
{
    int n, m;
    //입력을 받아서 동적배열 a에 값들을 채워넣음
    cin >> n >> m;
    cin.get();
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            //char로 받는다는 것 주의! 개행문자도 처리해줘야함
            int input = cin.get() - '0';
            a[i].push_back(input);
        }
        cin.get();
    }
 
 
    queue<pair<intint>> q;
    check[0][0= 1;
    q.push(make_pair<int>(00));
    //bfs 시작
    while (!q.empty())
    {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();
        for (int k = 0; k < 4++k)
        {
            int nr = dr[k] + r;
            int nc = dc[k] + c;
            if (nr >= 0 && nr < n && nc >= 0 && nc < m)
            if (check[nr][nc] == 0 && a[nr][nc] == 1)
            {
                check[nr][nc] = check[r][c] + 1;
                q.push(make_pair(nr, nc));
                //이전 check에서 1씩 증가해 나감!
            }
        }
    }
    cout << check[n - 1][m - 1<< endl;
    // 0,0범위부터이므로 1씩 줄여줌!
    return 0;
}
cs


'백준 온라인 저지' 카테고리의 다른 글

백준 7576: 토마토  (0) 2018.07.16
백준 2146: 다리 만들기  (0) 2018.07.16
백준 4963: 섬의 개수  (0) 2018.07.16
2667번 : 단지번호붙이기  (0) 2018.07.16
백준 1707: 이분 그래프  (0) 2018.07.16

+ Recent posts