Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

代码仅供参考·2025年7月14日8时30分(Day2)·搜索算法·C++
最后更新于:2025-07-14 19:58:26
别被 “我学不会” 的念头困住。编程就像拼图,从最小的模块开始,拼着拼着,你就会突然看清整体的轮廓。
Copyright © since 2025 FeatherBlaze (CX2521) All rights reserved.

#A. P44  特殊的四位数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> 
using namespace std;

int n, nums[100]={1};
bool a[100];

void prt(int step) {
for (int i=1; i<=4; i++) cout << nums[i];
cout << endl;
}

void dfs(int step, int has) {
if (step > 4) return ;
for(int i=nums[step-1]; i<=has&&i<n&&i<=9; i++){
if(!a[i]) {
a[i] = true;
nums[step] = i;
has -= i;
if(has==0 && step==4)prt(step);
else dfs(step+1, has);
has += i;
a[i] = false;
}
}
}

int main() {
cin >> n;
dfs(1, n);
return 0;
}

#B. P45  最大黑区域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int n, m, a[110][110], cnt, ans;

int max(int a, int b) {
return a>b?a:b;
}

void dfs(int x, int y) {
if (x<1 || x>n || y<1 || y>m || !a[x][y]) return;
a[x][y] = 0;
cnt++;
dfs(x-1, y);
dfs(x+1, y);
dfs(x, y-1);
dfs(x, y+1);
}

int main () {
cin >> n >> m;
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
cin >> a[i][j];
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (a[i][j]) {
cnt = 0;
dfs(i, j);
ans = max(ans, cnt);
}
cout << ans;
}

#C. P46  奇怪的电梯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <queue>
using namespace std;

struct node {
int x, y;
};

int n, a, b, k[205];
bool vis[205];
queue<node> q;

int bfs() {
q.push({a, 0});
vis[a] = true;
while (!q.empty()) {
int x=q.front().x, y=q.front().y;
q.pop();
if (x == b) return y;
int x_new, y_new;
x_new = x+k[x];
y_new = y+1;
if (x_new>=1 && x_new<=n && !vis[x_new]) {
q.push({x_new, y_new});
vis[x_new] = true;
}
x_new = x-k[x];
y_new = y+1;
if (x_new>=1 && x_new<=n && !vis[x_new]) {
q.push({x_new, y_new});
vis[x_new] = true;
}
}
return -1;
}

int main() {
cin >> n >> a >> b;
for (int i=1; i <= n; i++) cin >> k[i];
cout << bfs();
return 0;
}

#D. P55  细胞

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

struct node {
int x, y;
}q[10000];

char ma[101][101];
int n, m, ans, dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};

void bfs(int i, int j) {
int front=0, rear=1;
q[1].x = i;
q[1].y = j;
ma[i][j] = '0';
while (front < rear) {
front++;
for (int i=0; i<=3; i++) {
int x=q[front].x+dx[i];
int y=q[front].y+dy[i];
if (x>=1 && x<=n && y>=1 && y<=m && ma[x][y]!='0') {
rear++;
q[rear].x = x;
q[rear].y = y;
ma[x][y] = '0';
}
}
}
}

int main() {
cin >> n >> m;
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
cin >> ma[i][j];
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (ma[i][j]!='0') {
ans++;
bfs(i, j);
}
cout << ans;
return 0;
}

#E. P56  抓住那头牛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int dx[3]={-1,1,0}, map[100010], n, k;
struct node {
int x, t;
}q[100010];

void bfs() {
int head=0,tail=1;
q[1].x = n;
q[1].t = 0;
map[n] = 1;
while (head < tail) {
head++;
dx[2] = q[head].x;
for (int i=0; i<3; i++){
int x = q[head].x+dx[i];
if (x>=0 && x<=100005 && map[x]==0) {
tail++;
q[tail].x = x;
q[tail].t = q[head].t+1;
map[x] = 1;
if (x == k) {
cout << q[tail].t;
return;
}
}
}
}
}

int main() {
cin >> n >> k;
if (n == k) {
cout<<0;
return 0;
}
bfs();
return 0;
}

评论