Skip to content

Commit 16b556f

Browse files
authored
Merge pull request #20 from AlgoLeadMe/6-rivkms
6-rivkms
2 parents 66e2c42 + 4ffd921 commit 16b556f

File tree

4 files changed

+149
-24
lines changed

4 files changed

+149
-24
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

+20-23
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ using namespace std;
77
int N, M, A, D;
88
vector<int> vec;
99
pair<int, int> compare(const pair<int, int>& p1, const pair<int, int>& p2) {
10-
if (p1.second > p2.second) {
11-
if (p2.first >= M) {
10+
if(p1.first < M){
11+
if(p2.first < M){
12+
return p1.second < p2.second ? p1 : p2;
13+
}
14+
else{
1215
return p2;
1316
}
14-
return p1;
1517
}
16-
if (p1.second < p2.second) {
17-
if (p1.first >= M) {
18+
else{
19+
if(p2.first < M){
1820
return p1;
1921
}
20-
return p2;
22+
else{
23+
return p1.second < p2.second ? p1 : p2;
24+
}
2125
}
22-
return p1.first > p2.first ? p1 : p2;
2326
}
2427

25-
pair<int, int> dfs(bool blood, int point, int num, int n) {
28+
pair<int, int> dfs(int point, int num, int n, int canblood) {
2629
if (n == N) {
2730
return pair<int, int>(point, num);
2831
}
29-
if (!blood) {
30-
point += vec[n];
31-
pair<int, int> p1 = dfs(false, point, num, n + 1);
32-
pair<int, int> p2 = dfs(true, point, num, n + 1);
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);
3335
return compare(p1, p2);
3436
}
35-
else {
36-
point += A;
37-
num += D;
38-
return dfs(false, point, num, n + 1);
37+
else{
38+
return dfs(point, num, n + 1, canblood-1);
3939
}
4040
}
4141

@@ -46,14 +46,11 @@ int main() {
4646
cin >> tmp;
4747
vec.push_back(tmp);
4848
}
49+
4950
cin >> A >> D;
5051

51-
pair<int, int> p = dfs(false, 0, 0, 0);
52-
if (p.first >= M) {
53-
cout << -1;
54-
}
55-
else {
56-
cout << p.second;
57-
}
52+
pair<int, int> p = dfs(0, 0, 0, 0);
53+
cout<< p.first << " " << p.second;
54+
5855
return 0;
5956
}

β€Žrivkms/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
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-
| 5μ°¨μ‹œ | 2024.02.24 | Backtracking | [μž…λŒ€](https://www.acmicpc.net/problem/31413) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/18) |
9+
| 5μ°¨μ‹œ | 2024.02.24 | Backtracking | [μž…λŒ€](https://www.acmicpc.net/problem/31413) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/18) |
10+
| 6μ°¨μ‹œ | 2024.02.27 | BFS | [ν† λ§ˆν† ](https://www.acmicpc.net/problem/7576) | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/20) |
1011
---

0 commit comments

Comments
Β (0)