BFS - KimTaebin-ai/study_posts GitHub Wiki
: ๋ค์ฐจ์ ๋ฐฐ์ด์์ ๊ฐ ์นธ์ ๋ฐฉ๋ฌธํ ๋ ๋๋น๋ฅผ ์ฐ์ ์ผ๋ก ๋ฐฉ๋ฌธํ๋ ์๊ณ ๋ฆฌ์ฆ
๋ฃจํธ ๋ ธ๋(ํน์ ๋ค๋ฅธ ์์์ ๋ ธ๋)์์ ์์ํด์ ์ธ์ ํ ๋ ธ๋๋ฅผ ๋จผ์ ํ์ํ๋ ๋ฐฉ๋ฒ
https://gmlwjd9405.github.io/2018/08/15/algorithm-bfs.html
#include <stdio.h>
#define MAX 502
typedef struct
{
int x;
int y;
} LocationPoint;
typedef struct
{
LocationPoint data[MAX * MAX];
int head;
int tail;
} Queue;
int board[MAX][MAX] = {
{1, 1, 1, 0, 1, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
int n = 7, m = 10;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // ์ํ์ข์ฐ
int visit[MAX][MAX] = {0}; // ํด๋น ์นธ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ์ ์ฅ
void initQueue(Queue *q) // ํ์ head, tail ๊ฐ์ 0 ์ผ๋ก ์ด๊ธฐํ
{
q->head = 0;
q->tail = 0;
}
void push(Queue *q, LocationPoint p)
{
q->data[q->tail++] = p;
}
int empty(Queue *q)
{
return q->head == q->tail;
}
LocationPoint pop(Queue *q)
{
return q->data[q->head++];
}
int main()
{
Queue Q;
initQueue(&Q);
visit[0][0] = 1; // 1. ์๊ณ ๋ฆฌ์ฆ ์์ ์ (0, 0) ๋ฐฉ๋ฌธ ํ์
push(&Q, (LocationPoint){0, 0}); // 2. ํด๋น ์นธ์ ํ์ ๋ฃ๋๋ค.
while (!empty(&Q))
{
LocationPoint current = pop(&Q); // 3. ์ด๊ธฐ ์ธํ
ํ ํ๊ฐ ๋น ๋๊น์ง ํ์ head๋ฅผ ๋นผ์ค
printf("(%d, %d) -> ", current.x, current.y);
// 4. ํด๋น ์ขํ์ ์ํ์ข์ฐ๋ฅผ ์ดํด๋ณด๋ฉฐ ํ์ ๋ฃ๋๋ค
for (int dir = 0; dir < 4; dir++) // ์ํ์ข์ฐ ์ดํด๋ณด๊ธฐ
{
int nX = current.x + dx[dir];
int nY = current.y + dy[dir]; // nx, ny์ dir์์ ์ ํ ๋ฐฉํฅ์ ์ธ์ ํ ์นธ์ ์ขํ๊ฐ ๋ค์ด๊ฐ
if (nX < 0 || nX >= n || nY < 0 || nY >= m) // ๋ฒ์ ๋ฐ์ผ ๊ฒฝ์ฐ ๋์ด๊ฐ
continue;
if (visit[nX][nY] || board[nX][nY] != 1) // ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์นธ์ด๊ฑฐ๋ 0์ธ ๊ฒฝ์ฐ
continue;
visit[nX][nY] = 1; // ์กฐ๊ฑด์ ๊ฑธ๋ฆฌ์ง ์๋๋ค๋ฉด ๋ฐฉ๋ฌธ ๊ฐ๋ฅ, ๋ฐฉ๋ฌธํ์์ ์ ์ฅ
push(&Q, (LocationPoint){nX, nY}); // ํด๋น ์ขํ๋ฅผ ํ์ ๋ฃ์ด์ค
}
}
return 0;
}
bfs ์๊ณ ๋ฆฌ์ฆ์์๋ ์ขํ๋ฅผ ๋ด์ ํ๊ฐ ํ์ํจ. ์๊ณ ๋ฆฌ์ฆ์ด ์์๋๋ฉด ์ฐ์ (0, 0)์ ๋ฐฉ๋ฌธํ๋ค๋ ํ์ -> ํด๋น ์นธ์ ํ์ ๋ฃ๋๋ค. ์ด ์ด๊ธฐ์ธํ ์ด ๋๋ ํ์๋ ํ๊ฐ ๋น ๋๊น์ง ๊ณ์ ํ์ front ๋ฅผ ๋นผ๊ณ ํด๋น ์ขํ์ ์ํ์ข์ฐ๋ฅผ ์ดํด๋ณด๋ฉฐ ํ์ ๋ฃ์ด์ค๋ค
- ์์์ ์ ๋ฐฉ๋ฌธํ๋ค๋ ํ์๋ฅผ ๋จ๊ธฐ์ง ์๋๋ค.
- ํ์ ๋ฃ์ ๋ ๋ฐฉ๋ฌธํ๋ค๋ ํ์๋ฅผ ํ๋ ๋์ ํ์์ ๋นผ๋ผ ๋ ๋ฐฉ๋ฌธํ๋ค๋ ํ์๋ฅผ ๋จ๊ฒผ๋ค.
- ์ด์ํ ์์๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ๋์ง์ ๋ํ ์ฒดํฌ๋ฅผ ์๋ชปํ๋ค.
์ฃผ์ด์ง ๋ํ์ง์์ ๋ชจ๋ ๊ทธ๋ฆผ์ ์ฐพ์์ผํ๊ณ ์์์ ์ด ๊ณ ์ ๋์ด์์ง ์์
๊ทธ๋ฌ๋ ๊ฒฐ๊ตญ ํด์ผํ ์ผ์
- ์ํ์ข์ฐ๋ก ์ฐ๊ฒฐ๋ ๊ทธ๋ฆผ์ ํฌ๊ธฐ๋ฅผ ์์๋ด๊ธฐ
- ๋ํ์ง์ ์๋ ๋ชจ๋ ๊ทธ๋ฆผ์ ์ฐพ์๋ด๊ธฐ
4๊ฐ์ ๊ทธ๋ฆผ ์์์ ์ ์ด์ค for๋ฌธ์ ๋๋ฉด์ ์์์ ์ ์ฐพ์ผ๋ฉด ๋๋ค.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 502
// ํ์์ ์ฌ์ฉํ ๊ตฌ์กฐ์ฒด ์ ์
typedef struct {
int x;
int y;
} Point;
// ํ ๊ตฌํ
typedef struct {
Point* data;
int front;
int rear;
int size;
} Queue;
Queue* createQueue(int size) {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->data = (Point*)malloc(sizeof(Point) * size);
q->front = 0;
q->rear = -1;
q->size = 0;
return q;
}
void enqueue(Queue* q, Point p) {
q->rear = (q->rear + 1) % MAX_SIZE;
q->data[q->rear] = p;
q->size++;
}
Point dequeue(Queue* q) {
Point p = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return p;
}
bool isEmpty(Queue* q) {
return q->size == 0;
}
// ์ ์ญ ๋ณ์ ์ ์ธ
int board[MAX_SIZE][MAX_SIZE];
bool vis[MAX_SIZE][MAX_SIZE];
int n, m;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int max(int a, int b) {
return a > b ? a : b;
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &board[i][j]);
int mx = 0; // ๊ทธ๋ฆผ์ ์ต๋๊ฐ
int num = 0; // ๊ทธ๋ฆผ์ ์
Queue* q = createQueue(MAX_SIZE * MAX_SIZE);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(board[i][j] == 0 || vis[i][j]) continue;
num++; // ๊ทธ๋ฆผ์ ์ 1 ์ฆ๊ฐ
vis[i][j] = true;
Point start = {i, j};
enqueue(q, start);
int area = 0; // ๊ทธ๋ฆผ์ ๋์ด
while(!isEmpty(q)) {
area++;
Point cur = dequeue(q);
for(int dir = 0; dir < 4; dir++) {
int nx = cur.x + dx[dir];
int ny = cur.y + dy[dir];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(vis[nx][ny] || board[nx][ny] != 1) continue;
vis[nx][ny] = true;
Point next = {nx, ny};
enqueue(q, next);
}
}
mx = max(mx, area);
}
}
printf("%d\n%d\n", num, mx);
free(q->data);
free(q);
return 0;
}