์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 4963๋ฒˆ: ์„ฌ์˜ ๊ฐœ์ˆ˜ (DFS/BFS, C++)

PROGRAMMING/Algorithm

by koharin 2021. 3. 2. 16:51

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


BOJ:2667 (๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ) ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉด, ์กฐ๊ธˆ๋งŒ ์‘์šฉํ•ด์„œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

์ด ๋ฌธ์ œ์—์„œ๋Š”, ์„ฌ์ด ์žˆ์œผ๋ฉด 1๋กœ ํ‘œ์‹œํ•˜๋Š”๋ฐ, graph[x][y] ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ์˜ 8๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๊ฑธ์–ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

x-1 y-1
x-1 y
x-1 y+1
x y-1
x y+1
x+1 y-1
x+1 y
x+1 y+1

 

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์œ„์˜ 8๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ graph[x][y]์—์„œ x์ขŒํ‘œ์™€ y์ขŒํ‘œ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

8๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ DFS ํ•จ์ˆ˜์—์„œ nx = x + dx[i], ny = y + dy[i]๋กœ ๋‹ค์Œ ๋ฐฉ๋ฌธํ•  ์ •์ ์„ ์ •ํ•˜๊ณ , ๋งŒ์•ฝ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด DFS ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐฉ๋ฌธํ•œ๋‹ค. ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ DFS ํƒ์ƒ‰์ด๋‹ค.

 

์ฒ˜์Œ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ชจ๋“  ์—ฐ๊ฒฐ๋œ ๊ฒฝ๋กœ ๋ฐฉ๋ฌธ์ด ๋๋‚˜๋ฉด main ํ•จ์ˆ˜์—์„œ ๊ฐ€์žฅ ๋จผ์ € DFS ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ main ํ•จ์ˆ˜๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ๋ฆฌํ„ด ํ›„ count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ํ•˜๋‚˜์˜ ์„ฌ์— ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

 

 

 

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


์ฒ˜์Œ์—๋Š” STL์— ์žˆ๋Š” ํ•จ์ˆ˜์ธ deque๋ฅผ ์‚ฌ์šฉํ•ด์„œ graph๋ฅผ ๋‚˜ํƒ€๋ƒˆ๊ณ , ์ดํ›„ ์ผ๋ฐ˜ 2์ฐจ์› ๋ฐฐ์—ด๋กœ๋„ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.

#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int visited[50][50];

void DFS(int x, int y, deque<int> *graph, int w, int h){
    visited[x][y] = 1;
    for(int i=0; i<8; i++){
        int nx = x + dx[i]; int ny = y + dy[i];
        if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
        if(graph[nx][ny] == 1 && !visited[nx][ny]) DFS(nx, ny, graph, w, h);
    }
}

int main(){
    while(true){
        int w, h;
        scanf("%d %d", &w, &h);
        if(w == 0 && h == 0) break;
        deque<int> graph[h];
        int cnt=0;
        
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                int num; scanf("%d", &num);
                graph[i].push_back(num);
                visited[i][j] = 0;
            }
        }
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(graph[i][j] == 1 && !visited[i][j]) { 
                    DFS(i, j, graph, w, h); 
                    cnt++; 
                }
            }
        }
        printf("%d\n", cnt);
    }
}
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int visited[50][50];
int graph[50][50];

void DFS(int x, int y, int w, int h){
    visited[x][y] = 1;
    for(int i=0; i<8; i++){
        int nx = x + dx[i]; int ny = y + dy[i];
        if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
        if(graph[nx][ny] == 1 && !visited[nx][ny]) DFS(nx, ny, w, h);
    }
}

int main(){
    while(true){
        int w, h;
        scanf("%d %d", &w, &h);
        if(w == 0 && h == 0) break;
        int cnt=0;
        
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                int num; scanf("%d", &graph[i][j]);
                visited[i][j] = 0;
            }
        }
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(graph[i][j] == 1 && !visited[i][j]) { 
                    DFS(i, j, w, h); 
                    cnt++; 
                }
            }
        }
        printf("%d\n", cnt);
    }
}

 

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ