-
Notifications
You must be signed in to change notification settings - Fork 0
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
7-kokeunho #25
Conversation
There was a problem hiding this 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;
}
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}); | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ¬κΈ°μ city 2μ°¨μ λ°°μ΄μ μμ¨λ λμ κ² κ°μμ.
cityμ μ°μμ μ
λ ₯λ§κ³ λ μμΌλκΉ, intλ‘ λ체ν΄λ λ λ― ν©λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ κ·Έλ λ€μ! μ λ 무μμμ μΌλ‘ 2μ°¨μ λ°°μ΄μ μ¬μ©νλλ° μ λ§ νλ²λ μμΌμ΄μ..γ γ
There was a problem hiding this 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;
}
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}); | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ κ·Έλ λ€μ! μ λ 무μμμ μΌλ‘ 2μ°¨μ λ°°μ΄μ μ¬μ©νλλ° μ λ§ νλ²λ μμΌμ΄μ..γ γ
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ¬κΈ°μ μΉν¨μ§μ μ νν λ indexλ‘ μ κ·Όνλλ‘ νλ€μ. μ λ κ·Έλ₯ pairλ₯Ό λλ € λ°μλλ° λκ² μ°Έμ ν μ κ·ΌμΈ κ² κ°μ΅λλ€ π
π λ¬Έμ λ§ν¬
[BOJ] μΉν¨ λ°°λ¬(https://www.acmicpc.net/problem/15686)
βοΈ μμλ μκ°
1h30m
β¨ μλ μ½λ
λΈλ£¨νΈν¬μ€λ‘ νμμ΅λλ€. νΌμμ νΌ κ±΄ μλκ³ μ...
π μλ‘κ² μκ²λ λ΄μ©
λΈλ£¨νΈν¬μ€ μκ³ λ¦¬μ¦μ μ¬μ©νλ λ²μ μ΅ν κ² κ°μ΅λλ€.
μ¬κ·μ μΌλ‘ λ©μλλ₯Ό νΈμΆν΄μ μ‘°ν©μ λ§λ€μ΄λ΄λκ² λ¨λ°μ μ΄ν΄κ° κ°μ§ μμλλ°
μ΄λ»κ² μ΄ν΄κ° λμ΅λλ€..
μ€μν λ΄μ©μ μλλλ€λ§
μ²λΌ μ’ν 1, 2μ 거리λ₯Ό 맨ν΄νΌ 거리λΌκ³ νλ€κ³ ν©λλ€.
math λΌμ΄λΈλ¬λ¦¬μμ μ 곡νλ absλ κ°μ μ λκ°μ ꡬν΄μ£Όμ΄μ
νΈνκ² κ±°λ¦¬λ₯Ό κ³μ°ν μ μμμ΅λλ€.