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

12-mjj111 #216

Merged
merged 1 commit into from
Sep 24, 2024
Merged

12-mjj111 #216

merged 1 commit into from
Sep 24, 2024

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Aug 9, 2024

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

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

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

20๋ถ„

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

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

์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฃผ์–ด์ง„ ์น˜ํ‚จ ์ง‘์ค‘ M๊ฐœ๋ฅผ ํƒํ•˜์—ฌ,
ํƒํ•จ์„ ๋ฐ›์€ ์น˜ํ‚จ์ง‘๊ณผ ๊ฐ ์ง‘๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๊ฐ€์žฅ ์ ์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ์น˜ํ‚จ ์ง‘์—์„œ M๊ฐœ์˜ ์น˜ํ‚จ์ง‘์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐฑํŠธ๋ ˆํ‚น์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    public static void DFS(int start, int cnt) {
        if(cnt != M) {
            for (int i = start; i < chicken.size(); i++) {
                open[i] = true;
                DFS(i + 1, cnt + 1);
                open[i] = false;
            }
            return;
        }

๋งŒ์•ฝ M๊ฐœ๋ฅผ ๋ชจ๋‘ ํƒํ–ˆ์„ ๊ฒฝ์šฐ,
์—ด๋ฆฐ ์น˜ํ‚จ์ง‘๊ณผ ๊ฐ ์ง‘์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ •๋‹ต์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

        int res = 0;
        for (int i = 0; i < person.size(); i++) {
            int minimum = Integer.MAX_VALUE;

            for (int j = 0; j < chicken.size(); j++) {
                if (open[j]) {
                    int distance = getDistance(i, j);
                    minimum = Math.min(minimum, distance);
                }
            }

            res += minimum;
        }
        answer = Math.min(answer, res);

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

๋ฐฑํŠธ๋ ˆํ‚น์œผ๋กœ M๊ฐœ์˜ ์น˜ํ‚จ์ง‘์„ ํƒํ•˜๊ณ ,
๊ทธ๋ž˜ํ”„ํƒ์ƒ‰์œผ๋กœ ๋˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผํ• ๊นŒ ํ–ˆ๋”๋‹ˆ ์ฒ˜์Œ ๋ฐ›์€ ๊ฐ’์—์„œ ์น˜ํ‚จ์ง‘๊ณผ ์ง‘์„ ์ €์žฅํ•˜๋ฉด ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋”๊ตฐ์š”!
๋ฌธ์ œ๋ฅผ ์ž์„ธํžˆ ๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹ ใ…Ž

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

์˜คํ˜ธ 1๋…„๋„ ๋” ์ „์— ํ’€์—ˆ์—ˆ๋˜ ๋ฌธ์ œ๋„ค์š” :)
๋ฐฑํŠธ๋ž˜ํ‚น์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ์น˜ํ‚จ์ง‘์„ ํ•˜๋‚˜์”ฉ ํ์—…ํ•ด๋ณด๋ฉฐ M๊ฐœ๋งŒ ๋‚จ๊ฒผ์„ ๋•Œ์˜ ๊ฑฐ๋ฆฌ ํ•ฉ์„ ๊ตฌํ•ด ๊ทธ ์ค‘ ์ตœ์†Œ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ฃ  :p

์ง‘๋“ค์˜ ์œ„์น˜, ์น˜ํ‚จ์ง‘๋“ค์˜ ์œ„์น˜, ํ˜„์žฌ ํ์—…ํ•œ ์น˜ํ‚จ์ง‘ ์ธ๋ฑ์Šค <- 3๊ฐ€์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์บ์น˜ํ–ˆ๋‹ค๋ฉด ๋ฌด๋‚œํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋„ค์š”!

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

enum class EMap
{
    Empty = 0,
    House = 1,
    Chicken = 2,
};

#define EMPTY static_cast<int>(EMap::Empty)
#define HOUSE static_cast<int>(EMap::House)
#define CHICKEN static_cast<int>(EMap::Chicken)

constexpr int INF = 987654321;

int N, M;
vector<vector<int>> City;
vector<pair<int, int>> Houses, Chickens;
vector<bool> Closed;

int Backtracking(int Level, int Remain)
{
    if(Remain == 0)
    {
        int TotalDistance = 0;
        for(const auto& [hr, hc] : Houses)
        {
            int MinimumDistance = INF;
            for(int l = 0; l < Chickens.size(); ++l)
            {
                if(Closed[l]) continue;
                
                const auto& [cr, cc] = Chickens[l];
                MinimumDistance = min(MinimumDistance, abs(hr - cr) + abs(hc - cc));
            }
            
            TotalDistance += MinimumDistance;
        }
        
        return TotalDistance;
    }
    
    int MinimumDistance = INF;
    for(int l = Level; l < Chickens.size(); ++l)
    {
        if(Closed[l]) continue;
        
        Closed[l] = true;
        MinimumDistance = min(MinimumDistance, Backtracking(l, Remain - 1));
        Closed[l] = false;
    }
    
    return MinimumDistance;
}

int main()
{
    cin >> N >> M;
    City.assign(N, vector(N, 0));
    
    for(int r = 0; r < N; ++r)
    {
        for(int c = 0; c < N; ++c)
        {
            cin >> City[r][c];
            if(City[r][c] == HOUSE)
            {
                Houses.emplace_back(r, c);
            }
            else if(City[r][c] == CHICKEN)
            {
                Chickens.emplace_back(r, c);
            }
        }
    }
    
    Closed.assign(Chickens.size(), false);
    
    cout << Backtracking(0, Chickens.size() - M);

    return 0;
}

@9kyo-hwang 9kyo-hwang merged commit 302e01a into main Sep 24, 2024
4 checks passed
@9kyo-hwang 9kyo-hwang deleted the 12-mjj111 branch September 24, 2024 11:16
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.

2 participants