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

7-kokeunho #25

Merged
merged 2 commits into from
Nov 30, 2024
Merged

7-kokeunho #25

merged 2 commits into from
Nov 30, 2024

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ] μΉ˜ν‚¨ 배달(https://www.acmicpc.net/problem/15686)

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h30m

✨ μˆ˜λ„ μ½”λ“œ

브루트포슀둜 ν’€μ—ˆμŠ΅λ‹ˆλ‹€. ν˜Όμžμ„œ ν‘Ό 건 μ•„λ‹ˆκ³ μš”...

1. λ„μ‹œμ˜ 집과 μΉ˜ν‚¨μ§‘μ„ μž…λ ₯ λ°›μœΌλ©΄μ„œ 집과 μΉ˜ν‚¨μ§‘μ˜ μ’Œν‘œλ₯Ό 각각 λ¦¬μŠ€νŠΈμ— λ„£μ–΄μ€λ‹ˆλ‹€.
2. M개의 μΉ˜ν‚¨μ§‘μ΄ μ„ νƒλ˜λŠ” λͺ¨λ“  쑰합을 ν™•μΈν•©λ‹ˆλ‹€.
3. μ‘°ν•©λ§ˆλ‹€ 각 μ§‘μ—μ„œ κ°€κΉŒμš΄ μΉ˜ν‚¨μ§‘κΉŒμ§€μ˜ 거리의 ν•©(total_chicken_distance)λ₯Ό κ΅¬ν•˜κ³ 
   기쑴의 μ΅œλ‹¨ 거리(min_distance)와 λΉ„κ΅ν•©λ‹ˆλ‹€.
5. λͺ¨λ“  쑰합에 λŒ€ν•˜μ—¬ 거리 비ꡐ가 λλ‚˜λ©΄ κ²°κ³Όλ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

브루트포슀 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜λŠ” 법을 읡힌 것 κ°™μŠ΅λ‹ˆλ‹€.
μž¬κ·€μ μœΌλ‘œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ„œ 쑰합을 λ§Œλ“€μ–΄λ‚΄λŠ”κ²Œ 단박에 이해가 가지 μ•Šμ•˜λŠ”λ°
μ–΄λ–»κ²Œ 이해가 λμŠ΅λ‹ˆλ‹€..

μ€‘μš”ν•œ λ‚΄μš©μ€ μ•„λ‹™λ‹ˆλ‹€λ§Œ

distance = |hx-sx| + |hy-sy| 

처럼 μ’Œν‘œ 1, 2의 거리λ₯Ό λ§¨ν•΄νŠΌ 거리라고 ν•œλ‹€κ³  ν•©λ‹ˆλ‹€.

Manhattan Distance = |x1-x2| + |y1-y2|

math λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μ œκ³΅ν•˜λŠ” absλŠ” κ°’μ˜ μ ˆλŒ€κ°’μ„ κ΅¬ν•΄μ£Όμ–΄μ„œ
νŽΈν•˜κ²Œ 거리λ₯Ό 계산할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

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.

κ΅¬ν˜„ μ™„μ „ λΉ‘μ„Έλ„€μš”..

ν•œ 50λΆ„ κ±Έλ €μ„œ μ™„μ„±ν–ˆλŠ”λ° 계속 ν…ŒμŠ€νŠΈλ₯Ό 톡과 λͺ»ν•˜κΈΈλž˜ κ·Όν˜Έλ‹˜ μ½”λ“œ λ΄€μŠ΅λ‹ˆλ‹€.

λ‹€λ₯Έ 뢀뢄은 λ‹€ λΉ„μŠ·ν•œλ° λ‹€λ₯Έμ μ€
μ €λŠ” houseλ₯Ό (제 μ½”λ“œμ—μ„œλŠ” home) 큐둜 λ’€μ—ˆλŠ”λ°,
이걸 μ’€ μ΄μƒν•˜κ²Œ μ²˜λ¦¬ν–ˆμ—ˆλ‚˜λ΄μš”.

그리고 dfsλŒλ¦¬λŠ” 뢀뢄도 2쀑 for문으둜 μ’€ λ³΅μž‘ν–ˆλŠ”λ°,
κ·Όν˜Έλ‹˜ 처럼 λ°”κΎΈλ‹ˆ 잘 λ™μž‘ν•˜λŠ”κ΅°μš”.,.

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€~

CPP CODE
#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

#define EMPTY 0
#define HOME 1
#define CHICKEN 2

using namespace std;

int N, M, result = INT_MAX;
vector<pair<int, int>> chicken;
queue<pair<int, int>> home;

int getDistance(int x, int y, int x_, int y_) {
    return abs(x - x_) + abs(y - y_);
}

int calculateDistance(vector<pair<int, int>>& selected) {
    int sumDistance = 0;
    queue<pair<int, int>> q = home;

    while (!q.empty()) {
        int y_ = q.front().first;
        int x_ = q.front().second;
        q.pop();

        int minDistance = INT_MAX;
        for (auto& c : selected) {
            int distance = getDistance(x_, y_, c.second, c.first);
            minDistance = min(minDistance, distance);
        }
        sumDistance += minDistance;
    }

    return sumDistance;
}

void dfs(vector<pair<int, int>>& selected, int index, int count) {
    if (count == M) result = min(result, calculateDistance(selected));

    for (int i = index; i < chicken.size(); i++) {
        selected.push_back(chicken[i]);
        dfs(selected, i + 1, count + 1);
        selected.pop_back();
    }
}

int main() {
    cin >> N >> M;

    for (int y = 0; y < N; y++) {
        for (int x = 0; x < N; x++) {
            int temp;
            cin >> temp;
            if (temp == HOME) home.push({y, x});
            if (temp == CHICKEN) chicken.push_back({y, x});
        }
    }

    vector<pair<int, int>> selected;
    dfs(selected, 0, 0);

    cout << result;

    return 0;
}

Comment on lines +19 to +32
city = new int[N][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
city[i][j] = sc.nextInt();
if (city[i][j] == 1) {
houses.add(new int[] {i, j});
}
if (city[i][j] == 2) {
stores.add(new int[] {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.

μ—¬κΈ°μ„œ city 2차원 배열은 μ•ˆμ¨λ„ 됐을 것 κ°™μ•„μš”.
city의 μ“°μž„μ€ μž…λ ₯λ§κ³ λŠ” μ—†μœΌλ‹ˆκΉŒ, int둜 λŒ€μ²΄ν•΄λ„ 될 λ“― ν•©λ‹ˆλ‹€!

Copy link
Collaborator

Choose a reason for hiding this comment

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

μ•„ κ·Έλ ‡λ„€μš”! 저도 λ¬΄μ˜μ‹μ μœΌλ‘œ 2차원 배열을 μ‚¬μš©ν–ˆλŠ”λ° 정말 ν•œλ²ˆλ„ μ•ˆμΌμ–΄μš”..γ…‹γ…‹

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.

이 문제λ₯Ό ν’€λ©΄μ„œ 이전에 μ œκ°€ 올린 μ—°κ΅¬μ†Œ? λ¬Έμ œκ°€ μƒκ°λ‚˜λ”κ΅°μš”. κ·Έλž˜μ„œ κ΅¬ν˜„μ΄ 빨리 끝날쀄 μ•Œμ•˜μœΌλ‚˜.. μ‹œκ°„μ΄ˆκ³Όλ‘œ 인해 μ’€ 더 μΌμŠ΅λ‹ˆλ‹€.

λ¬Έμ œμ—μ„œ λ©”λͺ¨λ¦¬ μ œν•œμ΄ 512이길래 λ„ˆλ¬΄ μ—¬μœ λ‘œμ›Œμ„œ city도 2차원 λ°°μ—΄λ‘œ μ„€μ •ν•˜κ³  ν•  수 μžˆλŠ”κ±΄ μ „λΆ€ λ°°μ—΄λ‘œ μ„€μ •ν•΄λ²„λ Έλ„€μš”..γ…‹γ…‹γ…‹ κ·Έλž˜μ„œ dfs λ‚΄λΆ€μ—μ„œ 이쀑 for문을 μ‚¬μš©ν•˜κ²Œ λ˜μ—ˆκ³  μ‹œκ°„μ΄ˆκ³Όκ°€ λ‚¬μŠ΅λ‹ˆλ‹€.

이후엔 μΉ˜ν‚¨ κ°€κ²Œλ“€μ„ 배열에 λ„£μ–΄μ„œ for문을 μ‚¬μš©ν•΄μ„œ κ΅¬ν˜„ν–ˆλŠ”λ° dfsλ₯Ό μ‹€ν–‰ν•  λ•Œλ§ˆλ‹€ 0λΆ€ν„° λκΉŒμ§€ μΉ˜ν‚¨ κ°€κ²Œλ₯Ό μˆœνšŒν•˜κ²Œ λ˜μ–΄ μ€‘λ³΅λ˜λŠ” 쑰합이 λ§Žμ•„μ Έ μ‹œκ°„μ΄ˆκ³Όκ°€ ν•œλ²ˆ 더 λ–΄μŠ΅λ‹ˆλ‹€.

μ΅œμ’…μ μœΌλ‘œ dfsν•  λ•Œ indexλ₯Ό λ„˜κ²¨μ£Όμ–΄ μ„ νƒν•˜λŠ” μΉ˜ν‚¨κ°€κ²Œμ˜ 쑰합이 μ€‘λ³΅λ˜μ§€ μ•Šλ„λ‘ κ΅¬ν—Œν–ˆμŠ΅λ‹ˆλ‹€. (이 κ³Όμ •μ—μ„œ survivedMap은 μ§€μ›Œλ„ λ˜μ—ˆλŠ”λ° 까먹고 μˆ˜μ •μ•ˆν–ˆμŠ΅λ‹ˆλ‹€..)

μ œκ°€ κ΅¬ν˜„ν•œ μ½”λ“œμž…λ‹ˆλ‹€
#include <iostream>
#include <cmath>
#include <vector>
#include <climits>
using namespace std;

#define MAX 50
#define HOUSE 1
#define BHC 2
#define SURVIVED 3
#define NONE 0

int n, m;
int city[MAX + 5][MAX + 5] = { 0, };
int survivedMap[MAX + 5][MAX + 5] = { 0, };
int minimum = INT_MAX;
vector<pair<int, int>> survived;
vector<pair<int, int>> houses;
vector<pair<int, int>> bhcs;

int getChickenLength(pair<int, int> house, pair<int, int> bhc) {
	return abs(house.first - bhc.first) + abs(house.second - bhc.second);
}

void init() {
	cin >> n >> m;
	for (int y = 0; y < n; y++) {
		for (int x = 0; x < n; x++) {
			int k;
			cin >> k;
			city[y][x] = k;
			if (k == HOUSE) houses.push_back({ x, y });
			if (k == BHC) bhcs.push_back({ x, y });
		}
	}
}

void setMinimum() {
	int total = 0;
	for (auto house : houses) {
		int len = INT_MAX;
		for (auto s : survived) {
			//μΉ˜ν‚¨ 거리(집과 κ°€μž₯ κ°€κΉŒμš΄ μΉ˜ν‚¨μ§‘ μ‚¬μ΄μ˜ 거리)λ₯Ό ꡬ함
			len = min(len, getChickenLength(house, s));
		}
		total += len;
	}
	minimum = min(total, minimum);
}

void dfs(int index) {
	if (survived.size() == m) {
		setMinimum();
		return;
	}
	for (int i = index; i < bhcs.size(); i++) {
		//survived에 ν¬ν•¨λ˜μ–΄ μžˆμ§€ μ•Šμ„ λ•Œ
		if (survivedMap[bhcs[i].second][bhcs[i].first] == NONE) {
			survived.push_back(bhcs[i]);
			survivedMap[bhcs[i].second][bhcs[i].first] = SURVIVED;
			dfs(i + 1);
			survived.pop_back();
			survivedMap[bhcs[i].second][bhcs[i].first] = NONE;
		}
	}
}

int main() {
	init();
	dfs(0);
	cout << minimum;
	return 0;
}

Comment on lines +19 to +32
city = new int[N][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
city[i][j] = sc.nextInt();
if (city[i][j] == 1) {
houses.add(new int[] {i, j});
}
if (city[i][j] == 2) {
stores.add(new int[] {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.

μ•„ κ·Έλ ‡λ„€μš”! 저도 λ¬΄μ˜μ‹μ μœΌλ‘œ 2차원 배열을 μ‚¬μš©ν–ˆλŠ”λ° 정말 ν•œλ²ˆλ„ μ•ˆμΌμ–΄μš”..γ…‹γ…‹

Comment on lines +38 to +47
static void choiceStore(int[] selected, int start, int count) {
if (count == M) {
min_distance = Math.min(min_distance, chickenDistance(selected));
return;
}
for (int i = start; i < stores.size(); i++) {
selected[count] = i;
choiceStore(selected, i + 1, count + 1);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ—¬κΈ°μ„œ μΉ˜ν‚¨μ§‘μ„ 선택할 λ•Œ index둜 μ ‘κ·Όν•˜λ„λ‘ ν–ˆλ„€μš”. μ €λŠ” κ·Έλƒ₯ pairλ₯Ό λ•Œλ € λ°•μ•˜λŠ”λ° 되게 μ°Έμ‹ ν•œ 접근인 것 κ°™μŠ΅λ‹ˆλ‹€ πŸ‘

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