Skip to content
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

6-rivkms #20

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions rivkms/BFS/7576.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
int SIZE;

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int w, h, s = 0;
cin >> w >> h;
SIZE = w*h;

vector<int> box(SIZE);
queue<int> q;

for(int i = 0; i<SIZE; i++){
cin >> box[i];
if(box[i]==1){
q.push(i);
}
}
q.push(-1);
while(!q.empty()){
int tmp = q.front();
q.pop();
if(tmp == -1){
if(q.empty()){
break;
Comment on lines +28 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queueλ₯Ό μ‚¬μš©ν•˜μ—¬ map을 일일히 νƒμƒ‰ν•˜μ§€ μ•Šκ³ λ„ ν† λ§ˆν† κ°€ λ‹΄κΈ΄ μœ„μΉ˜ λ§Œμ„ λ°°μΆœν•˜λŠ” 것이 μΈμƒμ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

}
s++;
q.push(-1);
continue;
}
int x = tmp%w;
int y = tmp/w;
for(int i = 0; i<4; i++){
if(x+1<w && box[y*w+x+1]==0){
box[y*w+x+1] = 1;
q.push(y*w+x+1);
}
else if(y+1<h&&box[(y+1)*w+x]==0){
box[(y+1)*w+x] = 1;
q.push((y+1)*w+x);
}
else if(x>0 && box[y*w+x-1]==0){
box[y*w+x-1] = 1;
q.push(y*w+x-1);
}
else if(y>0 && box[(y-1)*w+x]==0){
box[(y-1)*w+x] = 1;
q.push((y-1)*w+x);
}
}
}
for(int y = 0; y<h; y++){
for(int x = 0; x<w; x++){
if(box[y*w+x]==0){
cout << -1;
return 0;
}
}
}
cout << s;

return 0;
}
59 changes: 59 additions & 0 deletions rivkms/BFS/7576_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>

using namespace std;
int SIZE;
bool filled(const vector<int>& box){
for(int i = 0; i<SIZE; i++){
if(box[i]==0){
return false;
}
}
return true;
}

bool changed(const vector<int>& box1, const vector<int>& box2){
for(int i = 0; i<SIZE; i++){
if(box1[i]!=box2[i]){
return true;
}
}
return false;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int w, h, s = 0;
cin >> w >> h;
SIZE = w*h;
vector<int> box(SIZE);
vector<int> box2(SIZE, 0);
for(int i = 0; i<SIZE; i++){
cin >> box[i];
}
while(true){
if(filled(box)){
cout << s << endl;
break;
}

s++;
for(int y = 0; y<h; y++){
for(int x = 0 ; x<w; x++){
if(box[y*w+x]<0) box2[y*w+x] = -1;
else if(box[y*w+x]>0) box2[y*w+x] = 1;
else if(x>0 && box[y*w+x-1]>0) box2[y*w+x] = 1;
else if(y>0 && box[(y-1)*w+x]>0) box2[y*w+x] = 1;
else if(x<w-1 && box[y*w+x+1]>0) box2[y*w+x] = 1;
else if(y<h-1 && box[(y+1)*w+x]>0) box2[y*w+x] = 1;
}
}

if(!changed(box, box2)){
cout << -1 << endl;
break;
}
box = box2;
}
}
43 changes: 20 additions & 23 deletions rivkms/Backtracking/31413.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ using namespace std;
int N, M, A, D;
vector<int> vec;
pair<int, int> compare(const pair<int, int>& p1, const pair<int, int>& p2) {
if (p1.second > p2.second) {
if (p2.first >= M) {
if(p1.first < M){
if(p2.first < M){
return p1.second < p2.second ? p1 : p2;
}
else{
return p2;
}
return p1;
}
if (p1.second < p2.second) {
if (p1.first >= M) {
else{
if(p2.first < M){
return p1;
}
return p2;
else{
return p1.second < p2.second ? p1 : p2;
}
}
return p1.first > p2.first ? p1 : p2;
}

pair<int, int> dfs(bool blood, int point, int num, int n) {
pair<int, int> dfs(int point, int num, int n, int canblood) {
if (n == N) {
return pair<int, int>(point, num);
}
if (!blood) {
point += vec[n];
pair<int, int> p1 = dfs(false, point, num, n + 1);
pair<int, int> p2 = dfs(true, point, num, n + 1);
if(!canblood){
pair<int, int> p1 = dfs(point+vec[n], num, n + 1, canblood);
pair<int, int> p2 = dfs(point+A, num+1, n + 1, D-1);
return compare(p1, p2);
}
else {
point += A;
num += D;
return dfs(false, point, num, n + 1);
else{
return dfs(point, num, n + 1, canblood-1);
}
}

Expand All @@ -46,14 +46,11 @@ int main() {
cin >> tmp;
vec.push_back(tmp);
}

cin >> A >> D;

pair<int, int> p = dfs(false, 0, 0, 0);
if (p.first >= M) {
cout << -1;
}
else {
cout << p.second;
}
pair<int, int> p = dfs(0, 0, 0, 0);
cout<< p.first << " " << p.second;

return 0;
}
3 changes: 2 additions & 1 deletion rivkms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
| 2μ°¨μ‹œ | 2024.02.15 | Recursion | [ν•˜λ…Έμ΄ 탑 이동 μˆœμ„œ](https://www.acmicpc.net/problem/11729) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/8) |
| 3μ°¨μ‹œ | 2024.02.18 | DP | [ν•˜λ…Έμ΄ 탑 이동 μˆœμ„œ](https://www.acmicpc.net/problem/10844) | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/11) |
| 4μ°¨μ‹œ | 2024.02.18 | Backtracking | [N-queen](https://www.acmicpc.net/problem/9663) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/13) |
| 5μ°¨μ‹œ | 2024.02.24 | Backtracking | [μž…λŒ€](https://www.acmicpc.net/problem/31413) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/18) |
| 5μ°¨μ‹œ | 2024.02.24 | Backtracking | [μž…λŒ€](https://www.acmicpc.net/problem/31413) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/18) |
| 6μ°¨μ‹œ | 2024.02.27 | BFS | [ν† λ§ˆν† ](https://www.acmicpc.net/problem/7576) | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/20) |
---