-
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
[트리]11월 22일 #17
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uD2B8\uB9AC"
[트리]11월 22일 #17
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,66 @@ | ||
// | ||
// Created by LG on 2021-11-17. | ||
// | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int r, c, dir, cnt; | ||
|
||
bool check(vector<vector<int>> board, int n, int m){ | ||
int dx[4] = {-1, 0, 1,0}; | ||
int dy[4] = {0, 1, 0, -1}; //왼쪽, 아래, 오른쪽, 위 | ||
|
||
for(int i=3; i>=0; i--){ | ||
r += dx[(dir+i)%4]; | ||
c += dy[(dir+i)%4]; | ||
if(r >= 0 && r < n && c >=0 && c < m && board[r][c]==0){ | ||
dir = (dir+i)%4; | ||
return true; | ||
} | ||
r -= dx[(dir+i)%4]; | ||
c -= dy[(dir+i)%4]; | ||
} | ||
|
||
r += dx[(dir+2)%4]; | ||
c += dy[(dir+2)%4]; | ||
if(r < 0 || r >=n || c < 0 || c >=m || board[r][c] == 1){ | ||
r -= dx[(dir+2)%4]; | ||
c -= dy[(dir+2)%4]; | ||
return false; | ||
Comment on lines
+29
to
+31
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. false를 보내면 clean함수에서 실행되는 명령문을 보니, 불필요한 코드가 보이네요! |
||
} | ||
else | ||
check(board, n, m); | ||
|
||
return true; | ||
|
||
} | ||
|
||
int clean(vector<vector<int>> &board, int n, int m){ | ||
if(board[r][c] == 0){ | ||
board[r][c] = 2; | ||
cnt++; | ||
} | ||
|
||
bool flag = check(board, n, m); | ||
if(flag) | ||
clean(board, n, m); | ||
else | ||
return cnt; | ||
|
||
return cnt; | ||
Comment on lines
+49
to
+52
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. p3. 중복되는 코드가 있네요! |
||
} | ||
|
||
int main(){ | ||
int n, m; | ||
cin >> n >> m >> r >> c >> dir; | ||
vector<vector<int>> board(n, vector<int>(m, 0)); | ||
for(int i=0; i<n; i++){ | ||
for(int j=0; j<m; j++) | ||
cin >> board[i][j]; | ||
} | ||
|
||
cout << clean(board, n, m); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Created by LG on 2021-11-20. | ||
// | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main(){ | ||
ios_base :: sync_with_stdio(false); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
int n, a, b, q, t, k; | ||
cin >> n; | ||
vector<int> tree(n+1); | ||
for(int i=0; i<n-1; i++){ | ||
cin >> a >> b; | ||
tree[a]++; | ||
tree[b]++; | ||
} | ||
|
||
cin >> q; | ||
while(q--){ | ||
cin >> t >> k; | ||
if(t==1 && tree[k] == 1) | ||
cout << "no\n"; | ||
else | ||
cout << "yes\n"; | ||
Comment on lines
+25
to
+28
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. 코드가 짧긴 하지만, 이 부분은 함수화를 해도 괜찮겠네요! |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Created by LG on 2021-11-20. | ||
// | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
vector<vector<int>> graph; | ||
vector<vector<int>> tree; | ||
vector<int> sizee; | ||
|
||
void makeTree(int currentNode, int parent){ | ||
for(int i=0; i<graph[currentNode].size(); i++){ | ||
if(graph[currentNode][i] != parent) { | ||
tree[currentNode].push_back(graph[currentNode][i]); | ||
makeTree(graph[currentNode][i], currentNode); | ||
} | ||
} | ||
} | ||
|
||
void countSubtreeNodes(int currentNode){ | ||
sizee[currentNode] = 1; | ||
for(int i=0; i<tree[currentNode].size(); i++){ | ||
countSubtreeNodes(tree[currentNode][i]); | ||
sizee[currentNode] += sizee[tree[currentNode][i]]; | ||
} | ||
} | ||
Comment on lines
+22
to
+28
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. void형 함수는 피해주시는게 좋아요! 이 문제는 특히 리턴형 함수로 바로 답을 반환해줄 수 있어요. 조금 어렵다면 트리 PPT를 참고해보셔도 좋아요! 답이 나와있어요. :) |
||
|
||
int main(){ | ||
int n, r, q, u, v; | ||
cin >> n >> r >> q; | ||
graph.assign(n+1, vector<int>(0)); | ||
tree.assign(n+1, vector<int>(0)); | ||
sizee.assign(n+1, 0); | ||
|
||
n--; | ||
while(n--){ | ||
cin >> u >> v; | ||
graph[u].push_back(v); | ||
graph[v].push_back(u); | ||
} | ||
|
||
makeTree(r, -1); | ||
countSubtreeNodes(r); | ||
|
||
while(q--){ | ||
cin >> u; | ||
cout << sizee[u] << '\n'; | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// Created by LG on 2021-11-17. | ||
// | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
vector<int> pw; | ||
long long dp[5001]; | ||
int len; | ||
|
||
int find(){ | ||
|
||
if(pw[0] == 0) | ||
return 0; | ||
dp[0] = 1; | ||
|
||
if(pw[1] == 0) { | ||
if (pw[0] < 3) | ||
dp[1] = 1; | ||
} | ||
else{ | ||
if(pw[0] == 1 || (pw[0] == 2 && pw[1] < 7)) | ||
dp[1] = 2; | ||
else | ||
dp[1] = 1; | ||
} | ||
Comment on lines
+19
to
+28
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. 1번째 인덱스에 대한 연산을 따로 처리해주느라 중복 코드가 조금 있네요! 이런 경우엔 dp배열의 인덱스를 인덱스 에러가 나지 않도록 관리해주면 해당 연산도 for문 안에서 처리하도록 할 수 있어요! 이때 초기화해주시는 걸 주의해야 해요. |
||
|
||
for(int i=2; i<len; i++){ | ||
if(pw[i] == 0){ | ||
if(pw[i-1] < 3 && pw[i-1] != 0) | ||
dp[i] = dp[i-2]; | ||
else | ||
return 0; | ||
} | ||
else{ | ||
if(pw[i-1] == 0) | ||
dp[i] = dp[i - 1]; | ||
else if(pw[i-1] == 1 || (pw[i-1] ==2 && pw[i] < 7)) | ||
dp[i] = dp[i-1] + dp[i-2]; | ||
else | ||
dp[i] = dp[i-1]; | ||
} | ||
} | ||
|
||
return dp[len-1]%1000000; | ||
} | ||
|
||
int main(){ | ||
string s; | ||
cin >> s; | ||
len = s.length(); | ||
for(int i=0; i<len; i++){ | ||
pw.push_back(s[i]-'0'); | ||
} | ||
Comment on lines
+53
to
+56
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. string을 가공하고 벡터에 넣어서 사용하는 건 조금 비효율적이라, 가급적 string 자체로 사용해주시는 게 좋아요! |
||
cout << find(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Created by LG on 2021-11-20. | ||
// | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
|
||
using namespace std; | ||
|
||
map<int, pair<int, int>> tree; | ||
vector<int> preorder; | ||
|
||
void divide(int left, int right){ | ||
if(left > right) | ||
return; | ||
int root = preorder[left]; | ||
int next = left+1; | ||
while(preorder[next] < root && next < preorder.size()) | ||
next++; | ||
divide(left+1, next-1); | ||
divide(next, right); | ||
cout << root << '\n'; | ||
} | ||
Comment on lines
+13
to
+23
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. 분할정복으로 풀어주셨네요! 좋아요~~!! 트리를 만든 후, 후위순회하는 풀이는 샘플코드로 올라갈 예정이니 확인해보시면 좋을 것 같아요! |
||
int main(){ | ||
int root, node; | ||
cin >> root; | ||
preorder.push_back(root); | ||
while(cin >> node){ | ||
preorder.push_back(node); | ||
} | ||
divide(0, preorder.size()-1); | ||
return 0; | ||
} |
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. clean함수는 주로 board[r][c] == 0일 경우 cnt를 올려주는 역할을 하고 있으니, 이 부분을 수정하면 clean함수의 사용은 꼭 없어도 괜찮을 것 같아요!
더불어 문제의 조건을 잘 읽어보시면, 범위 관련해서 더 간단하게 표현할 수 있어요.