-
Notifications
You must be signed in to change notification settings - Fork 2
20-Seol-Munhyeok #73
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
20-Seol-Munhyeok #73
Conversation
|
dp๋ฌธ์ ์ ๋ํ ์ฐ์ต๊ณผ ํ๋ ๋ง์ด ์ป๊ณ ๊ฐ๋๋ค. ๋๋ถ์ dp์ ๋ํ ๋๋ ค์์ด ์กฐ๊ธ ์ฌ๋ผ์ง ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ์ฝ๋ฉํ ์คํธ ๊ณต๋ถ#include<bits/stdc++.h>
using namespace std;
int solution(int a, int c, vector<vector<int>> pv) {
int maxA = 0, maxC = 0;
for(int i = 0; i < pv.size(); i++) {
maxA = max(maxA, pv[i][0]);
maxC = max(maxC, pv[i][1]);
}
vector<vector<int>> dp(maxA + 1, vector<int> (maxC + 1, 21437900));
a = min(a, maxA); c = min(c, maxC);
dp[a][c] = 0;
for(int i = a; i <= maxA; i++) {
for(int j = c; j <= maxC; j++) {
int ni = min(maxA, i + 1); int nj = min(maxC, j + 1);
dp[ni][j] = min(dp[ni][j], dp[i][j] + 1);
dp[i][nj] = min(dp[i][nj], dp[i][j] + 1);
for(int k = 0; k < pv.size(); k++) {
if(pv[k][0] <= i && pv[k][1] <= j) {
ni = min(maxA, i + pv[k][2]); nj = min(maxC, j + pv[k][3]);
dp[ni][nj] = min(dp[ni][nj], dp[i][j] + pv[k][4]);
}
}
}
}
return dp[maxA][maxC];
}
|
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.
์ค๋๋ง์ ๋ณด๋ knapsack ๋ฌธ์ ์ด๊ตฐ์!
(์๊ณ ๋ ฅ, ์ฝ๋ฉ๋ ฅ) ๋ ๊ฐ์ผ๋ก๋ง ์ ์๋๊ณ , ์ด๋ค ํ๋์ ํ๋ , ๊ฒฐ๊ตญ ์๋ก์ด (์๊ณ ๋ ฅ, ์ฝ๋ฉ๋ ฅ) ์ํ๋ก ์ฎ๊ฒจ๊ฐ๋ ๊ฑฐ๋๊น ์ํ๊ณต๊ฐ ํ์ ๋ฌธ์ ๋ผ๊ณ ์๊ฐใ
ํด์ ์ต์ ๋น์ฉ์ผ๋ก ํน์ ์ํ์ ๋๋ฌํ๋ ๊ฒ์ด ๋ชฉํ๋ผ๊ณ ๋ดค์ต๋๋ค. ๊ทธ๋์ DP๋ฅผ ์ ํํ์ต๋๋ค.
๋จ์ ํ์๋ ์ ๊น ์๊ฐํ์๋๋ฐ, ์์ ํ์์ผ๋ก ๋ชจ๋ ์ ํ์ ๋ฐ๋ผ๊ฐ๋ฉด ์๊ฐ ๋ณต์ก๋๊ฐ ๋๋ฌด ์ปค์ง ๊ฒ ๊ฐ์์ต๋๋ค
๋ชจ๋ ํ๋์ ๋น์ฉ์ด 1์ด์์ผ๋ฉด BFS๋ก ํ์์ ๊ฒ ๊ฐ๋ค์!
๋ฌธ์ ํธ๋๋ผ ๊ณ ์ํ์ จ์ต๋๋ค!
์ฝ๋ฉ ํ ์คํธ ๊ณต๋ถ
```cpp #include #include #include using namespace std;int solution(int alp, int cop, vector<vector> problems) {
int targetA = 0;
int targetC = 0;
for (size_t k = 0; k < problems.size(); k++) {
targetA = max(targetA, problems[k][0]);
targetC = max(targetC, problems[k][1]);
}
alp = min(alp, targetA);
cop = min(cop, targetC);
const int INF = 1e9;
vector<vector<int>> dp(targetA + 1, vector<int>(targetC + 1, INF));
dp[alp][cop] = 0;
for (int i = alp; i <= targetA; i++) {
for (int j = cop; j <= targetC; j++) {
if (dp[i][j] == INF) {
continue;
}
if (i + 1 <= targetA) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
}
if (j + 1 <= targetC) {
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
}
for (size_t k = 0; k < problems.size(); k++) {
int reqA = problems[k][0];
int reqC = problems[k][1];
int rwdA = problems[k][2];
int rwdC = problems[k][3];
int cost = problems[k][4];
if (i >= reqA && j >= reqC) {
int ni = min(targetA, i + rwdA);
int nj = min(targetC, j + rwdC);
dp[ni][nj] = min(dp[ni][nj], dp[i][j] + cost);
}
}
}
}
return dp[targetA][targetC];
}

๐ ๋ฌธ์ ๋งํฌ
์ฝ๋ฉ ํ ์คํธ ๊ณต๋ถ
โ๏ธ ์์๋ ์๊ฐ
50๋ถ
โจ ์๋ ์ฝ๋
์ ๊ทผ๋ฒ
์ด์ ์ ํผ ์์ ๋ฒ์ฃ ๋ฌธ์ ์ ์ ์ฌํ ํ์์ DP ๋ฌธ์ ์ ๋๋ค.
๋ฌธ์ ๋ฅผ ํฌ๊ฒ ๋ณด๋ฉด ๊ฒฐ๊ตญ ์์(์ฉ๋) ์ ํ + ์์ดํ ์ ํ ์ ํ์ ๋ฐฐ๋ญ ๋ฌธ์ ์ ๋น์ทํฉ๋๋ค.
maxA)๊ณผ ์ฝ๋ฉ๋ ฅ(maxC)a, ํ์ฌ ์ฝ๋ฉ๋ ฅccost)๋ฐ๋ผ์ ์์(์ฉ๋) ์ ํ + ์์ดํ ์ ํ ์ ํ์ ๋ฌธ์ ๋ผ๋ฉด ์ด ๋ฌธ์ ์ํฉ์ ์ด๋ป๊ฒ ๋ฐฐ๋ญ ๋ฌธ์ ์ ํํ๋ก ๋ณํํ ์ง๋ฅผ ๊ณ ๋ฏผํด๋ณด๋ฉด ๋ฌธ์ ๊ฐ ํ๋ฆฝ๋๋ค!
์ํ ์ ์ (State)
2์ฐจ์ DP๋ฅผ ์ฌ์ฉํด์ ์ํ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํฉ๋๋ค.
์ด๊ธฐ๊ฐ (Initialization)
dp ํ ์ด๋ธ์ ์ด๊ธฐ๊ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํฉ๋๋ค.
alp,cop๋ ๋ฌธ์ ์์ ์ฃผ์ด์ง ์ฒ์์ ๊ฐ์ง ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ ๋๋ค. ๋น์ฐํ ์ด๊ธฐ ์ํ์์๋ ๊ฑธ๋ฆฐ ์๊ฐ์ด 0์ ๋๋ค.์ ๋ต ์์น (Answer)
์ด ๋ฌธ์ ์์ ๊ตฌํ๋ ๊ฒ์ ๋ชจ๋ ๋ฌธ์ ๋ค์ ํ ์ ์๋ ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ ์ป๋ ์ต๋จ์๊ฐ์ ๋๋ค. ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ด ๋ฌธ์ ์ ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ ์ต๋๊ฐ ์ด์์ด๋ฉด ๋ชจ๋ ๋ฌธ์ ๋ฅผ ํ ์ ์์ต๋๋ค.
์ ์ด (Transition)
์ํ๋ฅผ ์ ์ ์ํ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ ํ์์ด ๋์ถ๋ฉ๋๋ค.
์ปท(๊ฒฝ๊ณ ์ฒ๋ฆฌ, Pruning)
์ ์ ํ์์ ๊ทธ๋๋ก ์ฝ๋๋ก ์์ฑํ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ ๋๋ค.
๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ์ ์ด๋ ๋ฌด์ํ๋๋ก ๊ฒฝ๊ณ ์ฒ๋ฆฌ๋ฅผ ํด์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
์ด๊ธฐ์ ์๊ณ ๋ ฅ(์ฝ๋ฉ๋ ฅ) ์์ฒด๊ฐ
maxA(maxC)๋ณด๋ค ๋์ ๊ฒฝ์ฐ๋ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ผํฉ๋๋ค.์ด๋ ๊ฒ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ๋๋ฉด dp ํ ์ด๋ธ์ ํฌ๊ธฐ๋ฅผ ์ ํ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ dp ํ ์ด๋ธ์ ์ฑ์ธ ๋, ๋ฌธ์ ๋ฅผ ํธ๋ ๊ฒฝ์ฐ, ์ฑ์ฐ๋ ค๋
a๊ฐalp_req์ด์,c๊ฐcop_req์ด์์ผ ๋๋ง dp ํ ์ด๋ธ์ ์ฑ์์ผํฉ๋๋ค.๊ทธ๋ฆฌ๊ณ ํญ์
a๋๋c๋ฅผ ์ฑ์ธ ๋ ๊ทธ ๊ฐ์ดmaxA๊ณผmaxC๋ฅผ ์ด๊ณผํ์ง ์๋๋ก ์ฒ๋ฆฌํด์ฃผ์ด์ผํฉ๋๋ค.์ ์ฒด ์ฝ๋๋ Files changed๋ฅผ ํ์ธํด์ฃผ์ธ์!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด์๋ ๋ฌธ์ ๊ฐ ์๊ตฌํ๋ ์ผ์ ์ด์์ ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ด ํ์ํ๋ค์ฒ๋ผ ์ถ๊ฐ์ ์ธ ์ ํ์ ๊ฑธ์ด์ ๋๋๋ฅผ ์ฌ๋ฆฌ๋ ๊ฑฐ ๊ฐ์ต๋๋ค. ์ด๋ ๊ฒ ์ ํ์ด ์๋ ๊ฒฝ์ฐ๋ for ๋ฌธ ์์์ ์ ์ ํ if ๋ฌธ์ผ๋ก ์ฒ๋ฆฌํด์ ์กฐ๊ฑด์ ๋ง์กฑํ ๋๋ง dp ํ ์ด๋ธ์ ์ฑ์ฐ๋ ๋ฐฉ์์ผ๋ก ํ๋ฉด ๋ฉ๋๋ค.