코딩공작소

[bfs]단계설정? 본문

알고리즘/그래프

[bfs]단계설정?

안잡아모찌 2019. 8. 26. 22:37
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
53
54
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
 
struct INFO {
    int r;
    int c;
};
 
int map[20][20];
int isV[20][20];
int dr[] = { 1,-1,0,0 }, dc[] = { 0,0,-1,1 };
queue<INFO> q;
 
void bfs(int r,int c,int step) {
    q.push({ r,c });
    isV[r][c] = 1;
    int cnt = 0;
    while (!q.empty()) {
        int qlen = q.size();
        while (qlen--) {
            int rr = q.front().r, cc = q.front().c;
            q.pop();
 
            for (int i = 0; i < 4; i++) {
                int nr = rr + dr[i], nc = cc + dc[i];
                if (nr < 0 || nc < 0 || nr >= 20 || nc >= 20 ||isV[nr][nc]==1continue;
 
                q.push({ nr,nc });
                isV[nr][nc] = 1;
            }
        }
        if (cnt == step-1return;
        cnt++;
    }
}
 
int main()
{
    int step;
    cin >> step;
    int money = 0;
    money += (step*step) + ((step - 1)*(step - 1));
    printf("step : %d  money : %d\n", step, money);
 
    bfs(1010, step); //취미로 해봤음. step단계까지만 bfs
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            printf("%d ", isV[i][j]);
        }printf("\n");
    }
    return 0;
}
cs

 

'알고리즘 > 그래프' 카테고리의 다른 글

[백준]연구소2  (0) 2019.09.03
[SWEA]홈 방법 서비스  (0) 2019.08.27
[SWEA]벽돌깨기  (0) 2019.08.25
[백준]치즈2  (0) 2019.08.23
[백준]치즈1  (0) 2019.08.23