Skip to content

Conversation

@froglike6
Copy link
Collaborator

@froglike6 froglike6 commented May 13, 2025

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

์น˜ํ‚จ ๋ฐฐ๋‹ฌ

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

30๋ถ„

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

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

    N, M โ† ์ •์ˆ˜ ์ž…๋ ฅ                    โ–ธ ๊ทธ๋ฆฌ๋“œ ํฌ๊ธฐ N, ์„ ํƒํ•  ์น˜ํ‚จ์ง‘ ์ˆ˜ M

    houses โ† ๋นˆ ๋ฆฌ์ŠคํŠธ                   โ–ธ ์ง‘ ์ขŒํ‘œ ์ €์žฅ์šฉ
    chickens โ† ๋นˆ ๋ฆฌ์ŠคํŠธ                 โ–ธ ์น˜ํ‚จ์ง‘ ์ขŒํ‘œ ์ €์žฅ์šฉ

    for r from 0 to Nโˆ’1                  โ–ธ ๊ฐ ํ–‰ ์ˆœํšŒ
        row โ† ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ ์ž…๋ ฅ          โ–ธ r๋ฒˆ์งธ ํ–‰ ์ฝ๊ธฐ
        for ๊ฐ i, v in enumerate(row)    โ–ธ ๊ฐ ์—ด ๊ฐ’ ๊ฒ€์‚ฌ
            ๋งŒ์•ฝ v = 1์ด๋ฉด
                houses์— (r, i) ์ถ”๊ฐ€    โ–ธ ์ง‘ ์œ„์น˜ ๊ธฐ๋ก
            ์•„๋‹ˆ๋ฉด ๋งŒ์•ฝ v = 2์ด๋ฉด
                chickens์— (r, i) ์ถ”๊ฐ€  โ–ธ ์น˜ํ‚จ์ง‘ ์œ„์น˜ ๊ธฐ๋ก

    min_city_dist โ† โˆž                    โ–ธ ์ตœ์†Œ ๋„์‹œ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ ์ดˆ๊ธฐํ™”

    for comb in combinations(chickens, M) โ–ธ ์น˜ํ‚จ์ง‘ M๊ฐœ ์กฐํ•ฉ ์ƒ์„ฑ
        city_dist โ† 0                   โ–ธ ์ด ์กฐํ•ฉ์˜ ๊ฑฐ๋ฆฌ ํ•ฉ ์ดˆ๊ธฐํ™”
        for ๊ฐ (hr, hc) in houses       โ–ธ ๋ชจ๋“  ์ง‘์— ๋Œ€ํ•ด
            dist โ† min(                โ–ธ ์ง‘โ†’์น˜ํ‚จ์ง‘ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
                |hrโˆ’cr| + |hcโˆ’cc| 
                for ๊ฐ (cr, cc) in comb
            )
            city_dist โ† city_dist + dist
            ๋งŒ์•ฝ city_dist โ‰ฅ min_city_dist์ด๋ฉด
                break

        ๋งŒ์•ฝ city_dist < min_city_dist์ด๋ฉด
            min_city_dist โ† city_dist   โ–ธ ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ 

    min_city_dist ์ถœ๋ ฅ                   โ–ธ ๊ฒฐ๊ณผ ์ถœ๋ ฅ

ChatGPT Image 2025๋…„ 5์›” 13์ผ ์˜คํ›„ 10_55_26

์ด ๋ฌธ์ œ์—์„œ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋Š”, ์ง‘์—์„œ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์น˜ํ‚จ์ง‘๊นŒ์ง€์˜ ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋„์‹œ์— ์—ฌ๋Ÿฌ ์ง‘๊ณผ ์—ฌ๋Ÿฌ ์น˜ํ‚จ์ง‘์ด ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์˜ ์ด ํ•ฉ์ด ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋ฌธ์ œ์—์„œ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€, ๋„์‹œ์—์„œ ์น˜ํ‚จ์ง‘์„ ์ตœ๋Œ€ $M$๊ฐœ ์—†์•จ ๋•Œ์˜ ์ตœ์†Œ ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ผ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ €๋Š” ์ผ๋‹จ ์ขŒํ‘œ๋“ค์„ ์ž…๋ ฅ ๋ฐ›๊ณ , enumerate๋ฅผ ์ด์šฉํ•ด ๊ฐ ์ง‘๊ณผ ์น˜ํ‚จ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ combination์„ ์‚ฌ์šฉํ•ด ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ "๋‚จ์€ ์น˜ํ‚จ์ง‘์— ๋Œ€ํ•œ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ"๋ฅผ ๊ตฌํ•˜์—ฌ ์ตœ์†Œ ๋„์‹œ์˜ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค.

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

Enumerate๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด, ์š”์†Œ์˜ ๊ฐ’๊ณผ ํ•จ๊ป˜ ๊ทธ ์ธ๋ฑ์Šค๋„ ๊ฐ™์ด ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

ํ›„....2์‹œ๊ฐ„ ์ •๋„ ๊ณ ๋ฏผํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ
์ง‘, ์น˜ํ‚จ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋ฐ›์•„๋‚ด๊ณ  ์จ๋จน๋Š” ๋ถ€๋ถ„์—์„œ ๋ง‰ํ˜€์„œ ๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๋‚˜์„œ์•ผ ์ดํ•ดํ–ˆ๋„ค์š”..

๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ํ’€์–ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์€ ๋‹จ๋ฒˆ์— ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.
์กฐ๊ธˆ ์•„์‰ฝ๋„ค์š”... ๋ฐฑํŠธ๋ž˜ํ‚น ๋ฐœ์ƒ์„ ๋นจ๋ฆฌ ํ–ˆ๋”๋ผ๋ฉด..

C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int N, M;
vector<pair<int, int>> houses, chickens;
bool selected[14];
int answer = 1000000000;

void calculateChickenDistance() {
    int total = 0;
    for(auto [hx, hy] : houses) {
        int distance = 1000000000;
        for(int i = 0; i < chickens.size(); i++) {
            if(selected[i]) {
                int cx = chickens[i].first;
                int cy = chickens[i].second;
                int d = abs(hx - cx) + abs(hy - cy);
                if(d < distance) distance = d;
            }
        }
        total += distance;
    }
    if(total < answer) answer = total;
}

void dfs(int idx, int cnt) {
    if(cnt == M) {
        calculateChickenDistance();
        return;
    }

    for(int i = idx; i < chickens.size(); i++) {
        selected[i] = true;
        dfs(i + 1, cnt + 1);
        selected[i] = false;
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    cin >> N >> M;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            int val;
            cin >> val;
            if(val == 1) houses.push_back({i, j});
            else if(val == 2) chickens.push_back({i, j});
        }
    }

    dfs(0, 0);
    cout << answer << '\n';

    return 0;
}

ํ’€์ด๋ฅผ ๋ณด๊ณ  ๋‚˜๋‹ˆ ๋ฐฑํŠธ๋ž˜ํ‚น์˜ ์ต์ˆ™ํ•œ ์–ผ๊ตด์ด ๋ณด์ด๋”๊ตฐ์š”..
๋ฐฑํŠธ๋ž˜ํ‚น ์œ ํ˜•์„ ์†์— ์ตํ˜€์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋ฌตํ˜€๋’€๋‹ค๊ฐ€ ํ’€์ด๋ฅผ ๊นŒ๋จน์„ ์ฏค์— ๋‹ค์‹œ ๋„์ „ํ•ด๋ด์•ผ๊ฒ ๋„ค์š”.

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

์Œ.. ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผ ํ• ๊นŒ.. ๊ณ„์† ์ƒ๊ฐํ•ด๋ดค์–ด์š”

์ง‘๊ณผ ์น˜ํ‚จ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•˜๊ณ , ์น˜ํ‚จ์ง‘์„ m๊ฐœ ๋ฝ‘์€ ํ›„ ๊ฐ ์น˜ํ‚จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•œ๋‹ค๋ฉด?
์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ตฌํ˜„์„ ํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๋”๊ตฐ์š”.. ์ฝ”๋“œ๋ฅผ ์“ฐ๋Š”๋ฐ ์ž๊พธ ๋ง‰ํžˆ๋Š”?
๋„ํ˜„๋‹˜์˜ ์ˆ˜๋„ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋ฐฉํ–ฅ์€ ๋งž์•˜๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”

์กฐ๊ธˆ ๋” ์ˆ˜๋ จํ•ด์„œ ๋‹ค์‹œ ์น˜ํ‚จ ๋ฐฐ๋‹ฌ์— ๋„์ „ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค..!

enumerate๋Š” ๋งค์šฐ ์œ ์šฉํ•˜๋„ค์š”!! ๊ธฐ์–ตํ–ˆ๋‹ค๊ฐ€ ์ž˜ ์จ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

Base automatically changed from 12-froglike6 to main May 18, 2025 08:07
Copy link
Collaborator

@caucsejunseo caucsejunseo left a comment

Choose a reason for hiding this comment

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

์ขŒํ‘œ๋งŒ ๋ณด๋ฉด ๋จธ๋ฆฌ๊ฐ€ ๋ตํ•ด์ ธ์„œ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ•  ์ง€ ๊ฐ์ด ์•ˆ์˜ค๋„ค์š”.
๋งจ๋‚  ์ด๊ฒƒ ์ €๊ฒƒ ๋จธ๋ฆฟ์†์œผ๋กœ ์ƒ๊ฐ๋งŒ ํ•˜๊ณ  ์†์ด ์•ˆ์›€์ง์—ฌ์ง€๋„ค์š”

๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„๋ณด์—ฌ์„œ ์ „๋ถ€๋‹ค ํ•ด๋ณด๋Š” ๊ฑด ๋น ๋ฅด๊ฒŒ ํฌ๊ธฐํ–ˆ์—ˆ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํฌ์ง€ ์•Š์€ ๋ฌธ์ œ์˜€๋„ค์š”. ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ณ„์‚ฐ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ๊ทธ๋ƒฅ ๋จธ๋ฆฌ๋กœ ๋˜๊ฒ ๋‹ค, ์•ˆ๋˜๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ์†์œผ๋กœ ์จ๋ณด๋ฉฐ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ์— ์กฐ๊ธˆ ๋” ์ต์ˆ™ํ•ด์ ธ์•ผ ํ•  ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

dfs, ๋ฐฑํŠธ๋ž˜ํ‚น, ์ขŒํ‘œ... ์ „๋ถ€ ๋” ์ต์ˆ™ํ•˜๊ฒŒ ๋งŒ๋“  ๋’ค ๋‹ค์‹œ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค...!

@froglike6 froglike6 merged commit 34347bb into main May 20, 2025
1 check passed
@froglike6 froglike6 deleted the 13-froglike6 branch May 20, 2025 02:40
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.

5 participants