-
Notifications
You must be signed in to change notification settings - Fork 0
[동적계획법] 10월 4일 #8
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uB3D9\uC801\uACC4\uD68D\uBC95"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int min(int a, int b, int c){ | ||
| return b > a ? c > a ? a : c : c > b ? b : c; | ||
| } | ||
|
|
||
| int find(vector<vector<int>> dp, vector<vector<int>> cost, int n){ | ||
|
|
||
| dp[0][0] = cost[0][0]; | ||
| dp[0][1] = cost[0][1]; | ||
| dp[0][2] = cost[0][2]; | ||
|
|
||
| for(int i=1; i<n; i++){ | ||
| dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + cost[i][0]; | ||
| dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + cost[i][1]; | ||
| dp[i][2] = min(dp[i-1][1], dp[i-1][0]) + cost[i][2]; | ||
| } | ||
|
|
||
| return min(dp[n-1][0], dp[n-1][1], dp[n-1][2]); | ||
| } | ||
|
|
||
| int main(){ | ||
| int n; | ||
| cin >> n; | ||
|
|
||
| vector<vector<int>> cost(n, vector<int>(3, 0)); | ||
| for(int i=0; i<n; i++){ | ||
| cin >> cost[i][0] >> cost[i][1] >> cost[i][2]; | ||
| } | ||
|
|
||
| vector<vector<int>> dp(n, vector<int>(n, 0)); | ||
| cout << find(dp, cost, n); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct info{ | ||
| int d, m; | ||
| }; | ||
|
|
||
| int find(vector<info> counsel, int n){ | ||
| vector<int> dp(n+1); | ||
|
|
||
| //마지막 날 하루 걸리는 상담일때만 상담함. | ||
| if(counsel[n].d == 1) | ||
| dp[n] = counsel[n].m; | ||
| else | ||
| dp[n] = 0; | ||
|
|
||
| for(int i=n-1; i>0; i--){ | ||
| if(i+counsel[i].d < n+2) // 상담을 할 수 있다면 | ||
| if(i+counsel[i].d < n+1) // 상담 마지막 날 다음 날 dp가 있으면 | ||
| dp[i] = max(dp[i+counsel[i].d]+counsel[i].m, dp[i+1]); | ||
| else // 상담 마지막 날 다음 날 dp가 없으면 | ||
| dp[i] = max(counsel[i].m, dp[i+1]); | ||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. dp 크기를 늘리면 한 줄로 합칠 수 있을 것 같아요! |
||
| else | ||
| dp[i] = dp[i+1]; | ||
| } | ||
|
|
||
| return dp[1]; | ||
| } | ||
|
|
||
| int main(){ | ||
| int n; | ||
| cin >> n; | ||
|
|
||
| //입력 | ||
| vector<info> counsel(n+1); | ||
| for(int i=1; i<=n; i++){ | ||
| cin >> counsel[i].d >> counsel[i].m; | ||
| } | ||
|
|
||
| cout << find(counsel, n); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int min(int a, int b, int c){ | ||
| return b > a ? c > a ? a : c : c > b ? b : c; | ||
| } | ||
|
|
||
| int find(vector<vector<int>> dp, vector<vector<int>> cost, int n){ | ||
|
|
||
| int ans [3]; | ||
| for(int i=0; i<3; i++){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 첫 번째 집을 어디로 고정할지를 main에서 정하고, 함수로 보내주면 더 간결하게 작성할 수 있을 것 같아요! |
||
| for(int j=0; j<3; j++){ | ||
| if(j==i) | ||
| dp[0][j] = cost[0][j]; | ||
| else | ||
| dp[0][j] = 100001; | ||
| } | ||
|
|
||
| for(int j=1; j<n; j++){ | ||
| dp[j][0] = min(dp[j-1][1], dp[j-1][2]) + cost[j][0]; | ||
| dp[j][1] = min(dp[j-1][0], dp[j-1][2]) + cost[j][1]; | ||
| dp[j][2] = min(dp[j-1][1], dp[j-1][0]) + cost[j][2]; | ||
| } | ||
|
|
||
| if(i==0) | ||
| ans[i] = min(dp[n-1][1], dp[n-1][2]); | ||
| else if(i==1) | ||
| ans[i] = min(dp[n-1][0], dp[n-1][2]); | ||
| else | ||
| ans[i] = min(dp[n-1][0], dp[n-1][1]); | ||
| } | ||
|
|
||
| return min(ans[0], ans[1], ans[2]); | ||
| } | ||
|
|
||
| int main(){ | ||
| int n; | ||
| cin >> n; | ||
|
|
||
| vector<vector<int>> cost(n, vector<int>(3, 0)); | ||
| for(int i=0; i<n; i++){ | ||
| cin >> cost[i][0] >> cost[i][1] >> cost[i][2]; | ||
| } | ||
|
|
||
| vector<vector<int>> dp(n, vector<int>(n, 0)); | ||
| cout << find(dp, cost, n); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #include <iostream> | ||
| #include <deque> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int main(){ | ||
| int n, m; | ||
| cin >> n >> m; | ||
|
|
||
| int cnt = 0; | ||
| deque<int> dodo; | ||
| deque<int> su; | ||
| deque<int> do_gr; | ||
| deque<int> su_gr; | ||
|
|
||
| //입력 | ||
| for(int i=0; i<n; i++){ | ||
| int a, b; | ||
| cin >> a >> b; | ||
| dodo.push_front(a); | ||
| su.push_front(b); | ||
| } | ||
|
|
||
| int win = 0; | ||
| while(true){ | ||
| //도도가 카드 옮김. | ||
| do_gr.push_front(dodo.front()); | ||
| dodo.pop_front(); | ||
| //도도 덱이 비었다면 수가 이김. | ||
| if(dodo.empty()){ | ||
| win = 1; | ||
| break; | ||
| } | ||
| //도도가 5를 내면 도도가 가져감 | ||
| if(do_gr.front() == 5){ | ||
| while(!su_gr.empty()){ | ||
| dodo.push_back(su_gr.back()); | ||
| su_gr.pop_back(); | ||
| } | ||
| while(!do_gr.empty()){ | ||
| dodo.push_back(do_gr.back()); | ||
| do_gr.pop_back(); | ||
| } | ||
|
Comment on lines
+36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 어느 덱으로 넣는지와, 어느 그라운드에서 가져오는지 보내주면 함수화 해봐도 좋을 것 같아요~! |
||
| } | ||
| //빈 그라운드가 없고 합이 5라면 수가 가져감. | ||
| else if(!do_gr.empty() && !su_gr.empty() && do_gr.front() + su_gr.front() == 5){ | ||
| while(!do_gr.empty()){ | ||
| su.push_back(do_gr.back()); | ||
| do_gr.pop_back(); | ||
| } | ||
| while(!su_gr.empty()){ | ||
| su.push_back(su_gr.back()); | ||
| su_gr.pop_back(); | ||
| } | ||
| } | ||
| cnt++; | ||
| //전체 턴이 끝났다면 종료함. | ||
| if(cnt == m) | ||
| break; | ||
|
|
||
| //수가 카드 옮김. | ||
| su_gr.push_front(su.front()); | ||
| su.pop_front(); | ||
| //수 덱이 비었다면 도도가 이김. | ||
| if(su.empty()){ | ||
| win = 2; | ||
| break; | ||
| } | ||
| //수가 5를 내면 도도가 가져감. | ||
| if(su_gr.front() == 5){ | ||
| while(!su_gr.empty()){ | ||
| dodo.push_back(su_gr.back()); | ||
| su_gr.pop_back(); | ||
| } | ||
| while(!do_gr.empty()){ | ||
| dodo.push_back(do_gr.back()); | ||
| do_gr.pop_back(); | ||
| } | ||
| } | ||
| else if(!do_gr.empty() && !su_gr.empty() && do_gr.front() + su_gr.front() == 5){ | ||
| while(!do_gr.empty()){ | ||
| su.push_back(do_gr.back()); | ||
| do_gr.pop_back(); | ||
| } | ||
| while(!su_gr.empty()){ | ||
| su.push_back(su_gr.back()); | ||
| su_gr.pop_back(); | ||
| } | ||
| } | ||
| cnt++; | ||
| if(cnt == m) | ||
| break; | ||
| } | ||
|
|
||
| if(win == 1) | ||
| cout << "su"; | ||
| else if(win == 2) | ||
| cout << "do"; | ||
| else{ | ||
| if(dodo.size() > su.size()) | ||
| cout << "do"; | ||
| else if( dodo.size() < su.size()) | ||
| cout << "su"; | ||
| else | ||
| cout << "dosu"; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // | ||
| // Created by LG on 2021-10-02. | ||
| // | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <map> | ||
| #include <deque> | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct info{ | ||
| int r, c; | ||
| }; | ||
|
|
||
| int n; | ||
| map<int, char> change; | ||
| info head = {1, 1}; | ||
|
|
||
| //동서남북 방향 전환 함수 | ||
| char changeDir(char from, char to){ | ||
| if(from == 'E'){ | ||
| if(to == 'L') | ||
| return 'N'; | ||
| else | ||
| return 'S'; | ||
| } | ||
| else if(from == 'W'){ | ||
| if(to == 'L') | ||
| return 'S'; | ||
| else | ||
| return 'N'; | ||
| } | ||
| else if(from == 'S'){ | ||
| if(to == 'L') | ||
| return 'E'; | ||
| else | ||
| return 'W'; | ||
| } | ||
| else{ | ||
| if(to == 'L') | ||
| return 'W'; | ||
| else | ||
| return 'E'; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. 방향 관련 배열을 사용하면 더 간편하게 작성할 수 있어요!! 예를 들어 좌표로 표현했을 때, 위로 가는 건 (-1,0)을 현재 좌표에 더해주는 거고, 왼쪽으로 가는 건 (0, -1)을 더해주는 거죠! 이런 식의 상,하,좌,우 변화값을 저장한 배열을 만들어서 관리하면 훨씬 좋아요! 한 번 수정해볼까요!! |
||
|
|
||
| //방향에 따라 머리 이동하고 종료 결정 | ||
| void moveHead(char &dir, bool &fin, vector<vector<bool>> &check, deque<info> &snake, int &cnt){ | ||
| //방향 바꿔야 할 초이면 방향을 바꿈 | ||
| if(change.find(cnt-1) != change.end()) | ||
| dir = changeDir(dir, change[cnt-1]); | ||
|
|
||
| if(dir == 'E') | ||
| head.c++; | ||
| else if(dir == 'W') | ||
| head.c--; | ||
| else if(dir == 'S') | ||
| head.r++; | ||
| else | ||
| head.r--; | ||
|
|
||
| //벽이나 몸통에 닿지 않았으면 머리 더함. | ||
| if(head.r > 0 && head.r <= n && head.c > 0 && head.c <=n &&!check[head.r][head.c]){ | ||
| snake.push_front({head.r, head.c}); | ||
| check[head.r][head.c] = true; | ||
| //cout << "cnt:" << cnt << ' ' << "r:" << head.r << ' ' << "c:" << head.c << '\n'; | ||
| } | ||
| else{ | ||
| fin = true; | ||
| } | ||
| } | ||
|
|
||
| int main(){ | ||
| int k, l, cnt = 1; | ||
| cin >> n >> k; | ||
| deque<info> snake; | ||
| vector<vector<bool>> check(n+1, vector<bool>(n+1, false)); | ||
| vector<vector<bool>> apple(n+1, vector<bool>(n+1, false)); | ||
|
|
||
| //사과 위치 입력 | ||
| for(int i=1; i<=k; i++){ | ||
| int r, c; | ||
| cin >> r >> c; | ||
| apple[r][c] = true; | ||
| } | ||
|
|
||
| //방향 전환 정보 입력 | ||
| cin >> l; | ||
| for(int i=0; i<l; i++){ | ||
| int sec; | ||
| char dir; | ||
| cin >> sec >> dir; | ||
| change.insert(pair<int, char>(sec, dir)); //!insert(make_pair(sec, dir)); | ||
| } | ||
|
|
||
| char dir = 'E'; | ||
| snake.push_back({1, 1}); | ||
| check[1][1] = true; | ||
| bool fin = false; | ||
| while(true){ | ||
| moveHead(dir, fin, check, snake, cnt); | ||
| if(fin) | ||
| break; | ||
| //사과 먹었는지에 따라 꼬리 없애거나 사과 없앰. | ||
| if(!apple[head.r][head.c]){ | ||
| info t = snake.back(); | ||
| snake.pop_back(); | ||
| check[t.r][t.c] = false; | ||
| } | ||
| else{ | ||
| apple[head.r][head.c] = false; | ||
| } | ||
| cnt++; | ||
| } | ||
|
|
||
| cout << cnt; | ||
| } | ||
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.
p2. 삼항연산자는 가독성이 안 좋아서 최대한 피하는 게 좋아요! min 함수 2개 쓰거나, 반복문 활용해서 작성해주시면 좋을 것 같아요!