-
Notifications
You must be signed in to change notification settings - Fork 1
13-froglike6 #47
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
13-froglike6 #47
Conversation
dohyeondol1
left a comment
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์๊ฐ ์ ๋ ๊ณ ๋ฏผํ ๊ฒ ๊ฐ์๋ฐ
์ง, ์นํจ์ง์ ์ขํ๋ฅผ ๋ฐ์๋ด๊ณ ์จ๋จน๋ ๋ถ๋ถ์์ ๋งํ์ ๊ฒฐ๊ตญ ํ์ด๋ฅผ ๋ณด๊ณ ๋์์ผ ์ดํดํ๋ค์..
๋ฐฑํธ๋ํน์ผ๋ก ํ์ด์ผ๊ฒ ๋ค๋ ์๊ฐ์ ๋จ๋ฒ์ ๋ชปํ์ต๋๋ค.
์กฐ๊ธ ์์ฝ๋ค์... ๋ฐฑํธ๋ํน ๋ฐ์์ ๋นจ๋ฆฌ ํ๋๋ผ๋ฉด..
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;
}ํ์ด๋ฅผ ๋ณด๊ณ ๋๋ ๋ฐฑํธ๋ํน์ ์ต์ํ ์ผ๊ตด์ด ๋ณด์ด๋๊ตฐ์..
๋ฐฑํธ๋ํน ์ ํ์ ์์ ์ตํ์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.
์ด ๋ฌธ์ ๋ ๋ฌตํ๋๋ค๊ฐ ํ์ด๋ฅผ ๊น๋จน์ ์ฏค์ ๋ค์ ๋์ ํด๋ด์ผ๊ฒ ๋ค์.
hadongun
left a comment
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.
์.. ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ์ด๋ป๊ฒ ๊ตฌํํด์ผ ํ ๊น.. ๊ณ์ ์๊ฐํด๋ดค์ด์
์ง๊ณผ ์นํจ์ง์ ์ขํ๋ฅผ ์ ์ฅํ๊ณ , ์นํจ์ง์ m๊ฐ ๋ฝ์ ํ ๊ฐ ์นํจ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ์ฌ ์ต์๊ฐ์ ๊ตฌํ๋ค๋ฉด?
์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ ๊ตฌํ์ ํ๊ธฐ๊ฐ ์ฝ์ง ์๋๊ตฐ์.. ์ฝ๋๋ฅผ ์ฐ๋๋ฐ ์๊พธ ๋งํ๋?
๋ํ๋์ ์๋ ์ฝ๋๋ฅผ ๋ณด๋๊น ๋ฐฉํฅ์ ๋ง์๋ ๊ฒ ๊ฐ๋ค์
์กฐ๊ธ ๋ ์๋ จํด์ ๋ค์ ์นํจ ๋ฐฐ๋ฌ์ ๋์ ํด๋ณด๊ฒ ์ต๋๋ค..!
enumerate๋ ๋งค์ฐ ์ ์ฉํ๋ค์!! ๊ธฐ์ตํ๋ค๊ฐ ์ ์จ๋ณด๊ฒ ์ต๋๋ค
caucsejunseo
left a comment
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.
์ขํ๋ง ๋ณด๋ฉด ๋จธ๋ฆฌ๊ฐ ๋ตํด์ ธ์ ์ด๋ป๊ฒ ํ์ด์ผํ ์ง ๊ฐ์ด ์์ค๋ค์.
๋งจ๋ ์ด๊ฒ ์ ๊ฒ ๋จธ๋ฆฟ์์ผ๋ก ์๊ฐ๋ง ํ๊ณ ์์ด ์์์ง์ฌ์ง๋ค์
๊ฒฝ์ฐ์ ์๊ฐ ๋๋ฌด ๋ง์๋ณด์ฌ์ ์ ๋ถ๋ค ํด๋ณด๋ ๊ฑด ๋น ๋ฅด๊ฒ ํฌ๊ธฐํ์๋๋ฐ ์๊ฐ๋ณด๋ค ์๊ฐ๋ณต์ก๋๊ฐ ํฌ์ง ์์ ๋ฌธ์ ์๋ค์. ์๊ฐ ๋ณต์ก๋ ๊ณ์ฐ์ด ์ต์ํ์ง ์์์ ๊ทธ๋ฅ ๋จธ๋ฆฌ๋ก ๋๊ฒ ๋ค, ์๋๊ฒ ๋ค ์๊ฐํ์๋๋ฐ ์์ผ๋ก ์จ๋ณด๋ฉฐ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ์ ์กฐ๊ธ ๋ ์ต์ํด์ ธ์ผ ํ ๊ฑฐ ๊ฐ์ต๋๋ค.
dfs, ๋ฐฑํธ๋ํน, ์ขํ... ์ ๋ถ ๋ ์ต์ํ๊ฒ ๋ง๋ ๋ค ๋ค์ ํ์ด๋ณด๊ฒ ์ต๋๋ค...!
๐ ๋ฌธ์ ๋งํฌ
์นํจ ๋ฐฐ๋ฌ
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
์๋ ์ฝ๋
์ด ๋ฌธ์ ์์ ์นํจ ๊ฑฐ๋ฆฌ๋, ์ง์์๋ถํฐ ๊ฐ๊น์ด ์นํจ์ง๊น์ง์ ๋งจํดํผ ๊ฑฐ๋ฆฌ์ ๋๋ค. ๋์์ ์ฌ๋ฌ ์ง๊ณผ ์ฌ๋ฌ ์นํจ์ง์ด ์๋๋ฐ, ์ฌ๊ธฐ์ ๋ง๋ค์ด์ง ์ ์๋ ์นํจ ๊ฑฐ๋ฆฌ์ ์ด ํฉ์ด ๋์์ ์นํจ ๊ฑฐ๋ฆฌ์ ๋๋ค.$M$ ๊ฐ ์์จ ๋์ ์ต์ ๋์์ ์นํจ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ผ๋ ๋ฌธ์ ์
๋๋ค.
๋ฐ๋ผ์ ๋ฌธ์ ์์ ๊ตฌํ๊ณ ์ ํ๋ ๊ฒ์, ๋์์์ ์นํจ์ง์ ์ต๋
์ ๋ ์ผ๋จ ์ขํ๋ค์ ์ ๋ ฅ ๋ฐ๊ณ ,
enumerate๋ฅผ ์ด์ฉํด ๊ฐ ์ง๊ณผ ์นํจ์ง์ ์ขํ๋ฅผ ์ ์ฅํ์ต๋๋ค. ๊ทธ๋ฐ ๋ค์combination์ ์ฌ์ฉํด ๋ชจ๋ ๊ฐ๋ฅํ "๋จ์ ์นํจ์ง์ ๋ํ ์นํจ ๊ฑฐ๋ฆฌ"๋ฅผ ๊ตฌํ์ฌ ์ต์ ๋์์ ์นํจ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ์ต๋๋ค.๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
Enumerate๋ฅผ ์๋กญ๊ฒ ์๊ฒ ๋์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ฉด, ์์์ ๊ฐ๊ณผ ํจ๊ป ๊ทธ ์ธ๋ฑ์ค๋ ๊ฐ์ด ๋ฐํํด์ฃผ๋ ์ญํ ์ ๋๋ค.