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

17-kokeunho #64

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

17-kokeunho #64

wants to merge 1 commit into from

Conversation

kokeunho
Copy link
Collaborator

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

[BOJ] ์•ˆ์ „ ์˜์—ญ https://www.acmicpc.net/problem/2468

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

40min

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

์šฐ์„  2์ฐจ์› ๋ฐฐ์—ด์„ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
๊ฐ ์›์†Œ์˜ ๋†’์ด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” map[][]๊ณผ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” visited[][]๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

map์— ํ•ด๋‹น ์ง€์ ์˜ ๋†’์ด๋ฅผ ๋Œ€์ž…ํ•˜๋ฉด์„œ map์—์„œ ๊ฐ€์žฅ ๋†’์€ ์ง€์ ์˜ ๋†’์ด max_height๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

        int max_area = 0;
        for (int h = 0; h <= max_height; h++) {
            visited = new boolean[n][n];
            int safe_area = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (!visited[i][j] && map[i][j] > h) {
                        dfs(i, j, h);
                        safe_area++;
                    }
                }
            }
            max_area = Math.max(max_area, safe_area);
        }

์นจ์ˆ˜ ๋†’์ด๋ฅผ 0์—์„œ max_height๊นŒ์ง€ ๋Š˜๋ ค๊ฐ€๋ฉด์„œ
๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  ์นจ์ˆ˜๋˜์ง€ ์•Š์€(ํ•ด๋‹น ์ง€์  ๋†’์ด > max_height) ์ง€์ ์—์„œ
dfs๋ฅผ ์‹คํ–‰ํ•˜๊ณ  safe_area๋ฅผ +1 ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ฐ ์นจ์ˆ˜ ๋†’์ด์—์„œ ๊ธฐ์กด์˜ max_area(์ตœ๋Œ€ ์•ˆ์ „ ์˜์—ญ)๊ณผ safe_area๋ฅผ ๋น„๊ตํ•˜์—ฌ
max_area๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

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

์ด์ „ ์ฐจ์‹œ์— ์ด์–ด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์˜ ๊ฐ์„ ์ด์–ด๊ฐ€๊ธฐ ์œ„ํ•˜์—ฌ
dfs ๋ฌธ์ œ๋ฅผ ๊ณจ๋ž์Šต๋‹ˆ๋‹ค.
์ฒ˜์Œ์—๋Š” ํ’€๋ฉด์„œ ๋„ˆ๋ฌด ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฝ์ง€ ์•Š๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ ํ’€๋ฆฌ๋”๊ตฐ์š”..
ํ˜น์‹œ ๋” ๋‚˜์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•œ ์ˆ˜ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค~!

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = sc.nextInt();
max_height = Math.max(max_height, map[i][j]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ ํ•ด์„œ h์˜ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ค„์ด์…จ๊ตฐ์š”! ์ข‹์€ ์•„์ด๋””์–ด๋„ค์š”

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.

์‚ด์ง ๋นก์„ธ๋„ค์š”...

dfs ๋˜๋Š” bfs ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ + ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ๋ชจ๋“  h์˜ ๊ฒฝ์šฐ ์กฐ์‚ฌ

์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€ ๋ฌธ์ œ๋ฅผ ์„ž์–ด๋†“์œผ๋‹ˆ๊นŒ ๋‚ด๊ฐ€ ์ž˜ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ธ์ง€๊ฐ€ ์ž˜ ์•ˆ๋˜๋”๋ผ๊ณ ์š”...

1์ฐจ ๊ตฌํ˜„์„ ๋๋‚ด๊ณ  ์‹คํ–‰์„ ์‹œ์ผœ๋ดค์Šต๋‹ˆ๋‹ค.
๊ทผ๋ฐ... 5x5 ๋ฉด 25, 7x7 ์ด๋ฉด 49 ์ฒ˜๋Ÿผ N^2 ์ด ์ถœ๋ ฅ๋˜๋Š” ๋ฒ„๊ทธ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค..
ํ•œ 5๋ถ„ ๋ˆˆ์ด ๋น ์ ธ๋ผ ๊ฒ€์‚ฌํ•ด๋ณด๋‹ˆ ์กฐ๊ฑด๋ฌธ์— ์กฐ๊ฑด์„ ํ•˜๋‚˜ ๋นผ๋จน์—ˆ๋”๋ผ๊ตฌ์š”..

if (!visited[y][x] && graph[y][x] >= level) { // ์—ฌ๊ธฐ!
    visited[y][x] = true;
    bfs(graph, visited, x, y, level);
    cnt++;
}

์ €๊ฒƒ์„ ๊ณ ์น˜๋‹ˆ ์ •๋‹ต์ด์—ˆ์Šต๋‹ˆ๋‹ค!

์ €๋„ ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฌ์šด ๋Š๋‚Œ์ด ์žˆ์–ด์„œ ์ •๋ฆฌํ•œ๋ฒˆ ์‹น ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ทผํ˜ธ๋‹˜ ์ฝ”๋“œ์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
ํ”ผ๋“œ๋ฐฑ ์š”๊ตฌํ•˜์…จ๋Š”๋ฐ ์ œ ๋ˆˆ์—๋Š” ์ž˜ ์งœ์—ฌ์ง„ ์ฝ”๋“œ ๊ฐ™์•„์š”!

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N;
int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void bfs(vector<vector<int>>& graph, vector<vector<bool>>& visited, int cur_x, int cur_y, int level) {
    queue<pair<int, int>> q;
    q.push({cur_x, cur_y});
    visited[cur_y][cur_x] = true;

    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();

        for (int dir = 0; dir < 4; dir++) {
            int x_ = x + offset[dir][1];
            int y_ = y + offset[dir][0];
            if (0 <= x_ && x_ < N && 0 <= y_ && y_ < N && !visited[y_][x_] && graph[y_][x_] >= level) {
                q.push({x_, y_});
                visited[y_][x_] = true;
            }
        }
    }
}

int getSafeArea(vector<vector<int>> graph, int level) {
    vector<vector<bool>> visited(N, vector<bool>(N, false));

    int cnt = 0;
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < N; x++) {
            if (!visited[y][x] && graph[y][x] >= level) {
                visited[y][x] = true;
                bfs(graph, visited, x, y, level);
                cnt++;
            }
        }
    }

    return cnt;
}

int main() {
    cin >> N;

    vector<vector<int>> graph(N, vector<int>(N));
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < N; x++) {
            cin >> graph[y][x];
        }
    }

    int maxArea = 0;
    for (int level = 0; level <= 100; level++) {
        maxArea = max(getSafeArea(graph, level), maxArea);
    }

    cout << maxArea;

    return 0;
}

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.

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์€ ์–ธ์ œ๋‚˜ ํ•œ๋ฒˆ์— ํ’€๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋„ค์š”.. ์ด๋ฒˆ์—๋Š” ๋ชจ๋“  ๋„์‹œ๊ฐ€ ๋ฌผ์— ์ž ๊ธฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•ด ๋†’์ด๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ์ง€์  - 1 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผํ•˜๋Š”๋ฐ ๊ทธ๊ฑธ ๋ชป๋ด์„œ ์ข€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋„ค์š”.

ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์œ ํ˜•์„ ๋งŽ์ด ํ’€๋‹ค๋ณด๋‹ˆ visited๋ฐฐ์—ด์˜ ์œ„์น˜๋‚˜ queue๋ฅผ ์„ ์–ธํ•˜๋Š” ์œ„์น˜๋ฅผ ์ ์ ˆํ•œ ๊ณณ์— ๋ฐ”๋กœ ๋ฐฐ์น˜ํ•˜์—ฌ ์—ฌ๋Ÿฌ ํ…Œ์ŠคํŠธ๋ฅผ ๋น ๋ฅด๊ฒŒ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์š”.

์ฝ”๋“œ๋Š” ๊ทผํ˜ธ๋‹˜๊ณผ ๊ฑฐ์˜ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค

code
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int n, high = -1, low = 1000;
int city[105][105] = {0};
int offset[4][2] = {{1, 0},{0,1},{-1,0},{0,-1}};
bool visited[105][105] = {false};

bool canMove(int x, int y, int level) {
    return 0<=x&&x<n&&0<=y&&y<n
        && !visited[y][x] && city[y][x] > level;
}

int bfs(int level, int x, int y) {    
    queue<pair<int, int>> q;
    q.emplace(x, y);
    visited[y][x] = true;
    
    while(!q.empty()) {
        auto [cx, cy] = q.front(); q.pop();
        for(auto& dir : offset) {
            int nx = cx + dir[0];
            int ny = cy + dir[1];
            if(!canMove(nx, ny, level)) continue;
            q.emplace(nx, ny);
            visited[ny][nx] = true;
        }
    }
    return 1;
}

int main() {
    cin>>n;
    for(int y=0;y<n;y++) for(int x=0;x<n;x++) {
        cin>>city[y][x];
        if(high < city[y][x]) high = city[y][x];
        if(low > city[y][x]) low = city[y][x];
    }
    
    int maxSafe = -1;
    for(int level=low-1; level<=high; level++) {
        memset(visited, false, sizeof(visited));
        int safe = 0;
        for(int y=0;y<n;y++) for(int x=0;x<n;x++) 
            if(city[y][x] > level && !visited[y][x]) safe += bfs(level, x, y);
        if(maxSafe < safe) maxSafe = safe;
    }
    cout<<maxSafe;
    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