본문 바로가기
Computer Science

BFS Algo

by OKOK 2019. 1. 16.

1. 코드 분석

2. 큐가 어떻게 작동되고 있는지 파악

3. 오케이

4. 링크드 리스트

5. 공부 ㄱ 


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
 
#define MAX_N 10
 
int MAP[MAX_N + 2][MAX_N + 2];
int queue[MAX_N * MAX_N][3];
int row;
int column;
int head;
int rear;
 
int isEmpty()
{
    return (head <= rear) ? 1 : 0;
}
 
int enqueue(int x, int y, int c)
{
    queue[head][0= x;
    queue[head][1= y;
    queue[head][2= c;
    head++;
    return 1;
}
 
int dequeue(int *x, int *y, int *c)
{
    if (isEmpty())
    {
        return 0;
    }
    *= queue[rear][0];
    *= queue[rear][1];
    *= queue[rear][2];
    rear++;
    return 1;
}
 
int breadthFirstSearch()
{
    int x;
    int y;
    int c;
 
    enqueue(110);
    MAP[1][1= 0;
    while (!isEmpty())
    {
        dequeue(&x, &y, &c);
        if (x == column && y == row)
        {
            return c;
        }
        if (x + 1 <= column && MAP[x + 1][y])
        {
            enqueue(x + 1, y, c + 1);
            MAP[x + 1][y] = 0;
        }
        if (y + 1 <= row && MAP[x][y + 1])
        {
            enqueue(x, y + 1, c + 1);
            MAP[x][y + 1= 0;
        }
        if (x - 1 > 0 && MAP[x - 1][y])
        {
            enqueue(x - 1, y, c + 1);
            MAP[x - 1][y] = 0;
        }
        if (y - 1 > 0 && MAP[x][y - 1])
        {
            enqueue(x, y - 1, c + 1);
            MAP[x][y - 1= 0;
        }
    }
    return -1;
}
 
 
int main(void)
{
    freopen("input.txt""r", stdin);
    int test_case;
    int T;
 
    scanf("%d"&T);
 
    for (test_case = 1; test_case <= T; test_case++)
    {
        head = 0;
        rear = 0;
        scanf("%d %d"&row, &column);
 
        for (int i = 1; i <= row; i++)
        {
            for (int j = 1; j <= column; j++)
            {
                scanf("%d"&MAP[j][i]);
            }
        }
        printf("#%d %d\n", test_case, breadthFirstSearch());
    }
    return 0;
}
cs

 


'Computer Science' 카테고리의 다른 글

Minimum Spanning Tree  (0) 2019.01.16
Dijkstra  (0) 2019.01.16
DFS Algo  (0) 2019.01.16
Permutation & Combination  (0) 2019.01.16
Dynamic programming  (0) 2019.01.16

댓글