Skip to content

Commit a5f698b

Browse files
committed
2024-02-28 토마토
1 parent 9785ac4 commit a5f698b

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

rivkms/BFS/7576.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
int SIZE;
7+
8+
int main(){
9+
ios_base::sync_with_stdio(false);
10+
cin.tie(NULL);
11+
int w, h, s = 0;
12+
cin >> w >> h;
13+
SIZE = w*h;
14+
15+
vector<int> box(SIZE);
16+
queue<int> q;
17+
18+
for(int i = 0; i<SIZE; i++){
19+
cin >> box[i];
20+
if(box[i]==1){
21+
q.push(i);
22+
}
23+
}
24+
q.push(-1);
25+
while(!q.empty()){
26+
int tmp = q.front();
27+
q.pop();
28+
if(tmp == -1){
29+
if(q.empty()){
30+
break;
31+
}
32+
s++;
33+
q.push(-1);
34+
continue;
35+
}
36+
int x = tmp%w;
37+
int y = tmp/w;
38+
for(int i = 0; i<4; i++){
39+
if(x+1<w && box[y*w+x+1]==0){
40+
box[y*w+x+1] = 1;
41+
q.push(y*w+x+1);
42+
}
43+
else if(y+1<h&&box[(y+1)*w+x]==0){
44+
box[(y+1)*w+x] = 1;
45+
q.push((y+1)*w+x);
46+
}
47+
else if(x>0 && box[y*w+x-1]==0){
48+
box[y*w+x-1] = 1;
49+
q.push(y*w+x-1);
50+
}
51+
else if(y>0 && box[(y-1)*w+x]==0){
52+
box[(y-1)*w+x] = 1;
53+
q.push((y-1)*w+x);
54+
}
55+
}
56+
}
57+
for(int y = 0; y<h; y++){
58+
for(int x = 0; x<w; x++){
59+
if(box[y*w+x]==0){
60+
cout << -1;
61+
return 0;
62+
}
63+
}
64+
}
65+
cout << s;
66+
67+
return 0;
68+
}

rivkms/BFS/7576_2.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
int SIZE;
6+
bool filled(const vector<int>& box){
7+
for(int i = 0; i<SIZE; i++){
8+
if(box[i]==0){
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
14+
15+
bool changed(const vector<int>& box1, const vector<int>& box2){
16+
for(int i = 0; i<SIZE; i++){
17+
if(box1[i]!=box2[i]){
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
int main(){
25+
ios_base::sync_with_stdio(false);
26+
cin.tie(NULL);
27+
int w, h, s = 0;
28+
cin >> w >> h;
29+
SIZE = w*h;
30+
vector<int> box(SIZE);
31+
vector<int> box2(SIZE, 0);
32+
for(int i = 0; i<SIZE; i++){
33+
cin >> box[i];
34+
}
35+
while(true){
36+
if(filled(box)){
37+
cout << s << endl;
38+
break;
39+
}
40+
41+
s++;
42+
for(int y = 0; y<h; y++){
43+
for(int x = 0 ; x<w; x++){
44+
if(box[y*w+x]<0) box2[y*w+x] = -1;
45+
else if(box[y*w+x]>0) box2[y*w+x] = 1;
46+
else if(x>0 && box[y*w+x-1]>0) box2[y*w+x] = 1;
47+
else if(y>0 && box[(y-1)*w+x]>0) box2[y*w+x] = 1;
48+
else if(x<w-1 && box[y*w+x+1]>0) box2[y*w+x] = 1;
49+
else if(y<h-1 && box[(y+1)*w+x]>0) box2[y*w+x] = 1;
50+
}
51+
}
52+
53+
if(!changed(box, box2)){
54+
cout << -1 << endl;
55+
break;
56+
}
57+
box = box2;
58+
}
59+
}

rivkms/Backtracking/31413.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// <미완>
2+
#include <iostream>
3+
#include <vector>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
int N, M, A, D;
8+
vector<int> vec;
9+
pair<int, int> compare(const pair<int, int>& p1, const pair<int, int>& p2) {
10+
if(p1.first < M){
11+
if(p2.first < M){
12+
return p1.second < p2.second ? p1 : p2;
13+
}
14+
else{
15+
return p2;
16+
}
17+
}
18+
else{
19+
if(p2.first < M){
20+
return p1;
21+
}
22+
else{
23+
return p1.second < p2.second ? p1 : p2;
24+
}
25+
}
26+
}
27+
28+
pair<int, int> dfs(int point, int num, int n, int canblood) {
29+
if (n == N) {
30+
return pair<int, int>(point, num);
31+
}
32+
if(!canblood){
33+
pair<int, int> p1 = dfs(point+vec[n], num, n + 1, canblood);
34+
pair<int, int> p2 = dfs(point+A, num+1, n + 1, D-1);
35+
return compare(p1, p2);
36+
}
37+
else{
38+
return dfs(point, num, n + 1, canblood-1);
39+
}
40+
}
41+
42+
int main() {
43+
int tmp;
44+
cin >> N >> M;
45+
for (int i = 0; i < N; i++) {
46+
cin >> tmp;
47+
vec.push_back(tmp);
48+
}
49+
50+
cin >> A >> D;
51+
52+
pair<int, int> p = dfs(0, 0, 0, 0);
53+
cout<< p.first << " " << p.second;
54+
return 0;
55+
}

rivkms/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
| 2차시 | 2024.02.15 | Recursion | [하노이 탑 이동 순서](https://www.acmicpc.net/problem/11729) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/8) |
77
| 3차시 | 2024.02.18 | DP | [하노이 탑 이동 순서](https://www.acmicpc.net/problem/10844) | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/11) |
88
| 4차시 | 2024.02.18 | Backtracking | [N-queen](https://www.acmicpc.net/problem/9663) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/13) |
9+
| 4차시 | 2024.02.18 | Backtracking | [N-queen](https://www.acmicpc.net/problem/9663) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/13) |
10+
| 5차시 | 2024.02.24 | Backtracking | [입대](https://www.acmicpc.net/problem/31413) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/18) |
11+
| 6차시 | 2024.02.27 | BFS | [토마토](https://www.acmicpc.net/problem/7576) | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/20) |
912
---

0 commit comments

Comments
 (0)