Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-kokeunho #63

Open
wants to merge 1 commit into
base: 15-kokeunho
Choose a base branch
from
Open

16-kokeunho #63

wants to merge 1 commit into from

Conversation

kokeunho
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

[BOJ] DFS์™€ BFS https://www.acmicpc.net/problem/1260

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

DFS์™€ BFS ๊ธฐ์ดˆ๋ถ€ํ„ฐ ๊ณต๋ถ€ํ•ด๋ณด๋ ค๊ณ  ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋”ฐ๋กœ ์ˆ˜๋„ ์ฝ”๋“œ๊ฐ€ ์—†์–ด๋„ ์ดํ•ดํ•˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ ˆ๋Œ€ ๊ท€์ฐฎ์•„์„œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š”
๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธ์ด๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ด๋•Œ ์ž๋ฐ” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” Collections๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.
Collections๋Š” List, Map ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ •๋ ฌํ•˜๋Š” .sort()
์ตœ๋Œ€/์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” .max() / .min()
ํŠน์ • ๊ฐ’์œผ๋กœ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฑ„์šฐ๋Š” .fill()
๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธฐ๋ณธ์„ ๋‹ค์งˆ์ˆ˜์žˆ๋Š” ์ข‹์€ ๋ฌธ์ œ๋„ค์š”!

์‹œ๊ฐ„์„ ์žฌ๋ฉฐ ๋นจ๋ฆฌ ํ’€์–ด๋ณด๊ณ  ์‹ถ์€ ๋งˆ์Œ์— ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ฝ์ง€ ์•Š๊ณ  ํ’€์ด๋ฅผ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.
๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„์ด ๋๋‚˜๊ณ  ์ œ์ถœ์„ ํ–ˆ๋Š”๋ฐ.... ์‹คํŒจ๊ฐ€ ๋–ณ์Šต๋‹ˆ๋‹ค...

๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋‹ˆ

๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค.

์ด๋Ÿฐ ์กฐ๊ฑด์ด ์žˆ๋”๊ตฐ์š”... ๊ทธ๋ž˜์„œ ์ •๋ ฌ์„ ํ•˜๊ณ  ์ œ์ถœํ•˜๋‹ˆ ๋งž์•˜์Šต๋‹ˆ๋‹ค!

์ฝ”๋“œ๋Š” ๊ทผํ˜ธ๋‹˜ ์ฝ”๋“œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธฐ์ดˆ๋ฅผ ๋‹ค์ง€๊ธฐ์— ์ข‹์€ ๋ฌธ์ œ๋„ค์š”! ์ €๋Š” ๊ทผํ˜ธ๋‹˜๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ ํ–‰๋ ฌ์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋ผ๊ธธ๋ž˜ ์ •๋ ฌํ•˜๊ธด ์‹ซ๊ณ  ๋…ธ๋“œ์˜ ์ˆ˜๋„ ์ž‘์•„์„œ ์ด ๋ฐฉ๋ฒ•์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค.

code
int n, m, v;
int s, e;
bool graph[1001][1001] = {0};
bool visited[1001] = {false};

void dfs(int cur) {
    visited[cur] = true;
    cout << cur << ' ';
    for (int i = 1; i < 1001; i++) {
        if (graph[cur][i] == true && !visited[i])
            dfs(i);
    }
    return;
}

void bfs(int s) {
    memset(visited, false, sizeof(visited));
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while (!q.empty()) {
        int size = q.size();
        while (size--) {
            int cur = q.front();
            q.pop();
            cout << cur << ' ';
            for (int i = 1; i < 1001; i++) {
                if (graph[cur][i] == true && !visited[i]) {
                    visited[i] = true;
                    q.push(i);
                }
            }
        }
    }
}

int main() {
    cin >> n >> m >> v;
    for (int i = 0; i < m; i++) {
        cin >> s >> e;
        graph[s][e] = 1;
        graph[e][s] = 1;
    }
    dfs(v);
    cout << '\n';
    bfs(v);
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants