当前位置: 首页 > news >正文

2025年大一ACM训练-搜索

2025年大一ACM训练-搜索

前期知识:DFS,本文搜索题解法以深度优先搜索为主
1.1 DFS 的定义
深度优先搜索(Depth-First Search)是一种用于遍历树或图的算法。核心思想是尽可能“深入”访问图的每个节点,直到无法继续前进为止,然后再回溯到之前的节点继续遍历。

1.2 DFS 的工作原理
DFS 的基本步骤如下:

  1. 访问当前节点:从起点节点开始访问。
  2. 递归遍历子节点:依次访问当前节点的所有未被访问过的子节点。
  3. 回溯:当所有子节点都被访问完毕后,回溯到父节点继续遍历。

1.3 DFS 的典型应用场景
迷宫求解:寻找从起点到终点的路径。
连通性问题:判断图中的两个节点是否连通。
路径查找:在图中查找从一个节点到另一个节点的所有可能路径。
DFS主要以栈结构或递归来实现,递归本质上也是栈结构的运用

读者可通过以下视频进行初步学习

图的算法-DFS深度优先遍历搜索算法 数据结构与算法

Problem A 迷宫寻路-搜索:这里是引用

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char a[1001][1001];
int posx[2],posy[2],n,m;
void dfs(int x,int y)
{a[x][y]='#';int x1,y1,i;for(i=0;i<4;i++){x1=x+dir[i][0];y1=y+dir[i][1];if(x1>=0 && x1<n && y1>=0 && y1<m && a[x1][y1]=='*'){dfs(x1,y1);}}
}
int main()
{int i,j;while(cin>>n>>m){int p=-1;for(i=0;i<n;i++){for(j=0;j<m;j++){cin>>a[i][j];if((i==0 || i==n-1 || j==0 || j==m-1) &&a[i][j]=='*'){posx[++p]=i;posy[p]=j;}}}dfs(posx[0],posy[0]);if(a[posx[1]][posy[1]]=='#') cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;
}

从代码里可以看出几个关键部分:
①对于迷宫问题,对开始点和结束点的确定是必要部分
②在dfs函数内(x,y)点要朝上下左右四个方向移动,用dir数组来实现简化
③判断边界
④对于已经访问过的节点要标记,防止陷入死循环

Problem B 白与黑-搜索:
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;int graph[21][21];
int w, h, startX, startY, cnt = 0;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
void dfs(int x, int y)
{graph[x][y] = 0;cnt++;for (int i = 0; i < 4; i++){int nx = x + dx[i];int ny = y + dy[i];if (nx >= 1 && nx <= w && ny >= 1 && ny <= h && graph[nx][ny] == 1){dfs(nx, ny);}}
}int main() {while (cin >> w >> h && w && h){memset(graph, 0, sizeof(graph));cnt = 0;for (int i = 1; i <= h; i++) {for (int j = 1; j <= w; j++) {char ch;cin >> ch;if (ch == '#') graph[j][i] = 0;else if (ch == '.') graph[j][i] = 1;else if (ch == '@') {startX = j;startY = i;graph[j][i] = 1;}}}dfs(startX, startY);cout << cnt << endl;}return 0;
}

Problem C 搜索入门-搜索:
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char a[5][5];
void dfs(int x,int y)
{a[x][y]='*';int x1,y1,i;for(i=0;i<4;i++){x1=x+dir[i][0];y1=y+dir[i][1];if(x1>=0 && x1<4 && y1>=0 && y1<4 && a[x1][y1]=='#'){dfs(x1,y1);}}
}
int main()
{int n,i,j;cin>>n;while(n--){for(i=0;i<4;i++)for(j=0;j<4;j++) cin>>a[i][j];dfs(0,0);if(a[3][3]=='*') cout<<"YES"<<endl;else cout<<"NO"<<endl;}
}

Problem D 逃出迷宫-搜索:
在这里插入图片描述

这题和其他几个有所不同,用的是BFS,具体不在本文阐述

#include<bits/stdc++.h>
#define MAX_R 1000
#define MAX_C 1000
#define INF INT_MAX
typedef struct {int x, y;int time;
} Position;
typedef struct {int x, y;
} Fire;
char maze[MAX_R][MAX_C];
int dist[MAX_R][MAX_C];
int fire_dist[MAX_R][MAX_C];
Position queue[MAX_R * MAX_C];
Fire fire_queue[MAX_R * MAX_C];
int front,rear,fire_front,fire_rear,R,C;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
void bfs_fire()
{fire_front=fire_rear=0;for (int i=0;i<R;i++){for (int j=0;j<C;j++){if (maze[i][j]=='F'){fire_queue[fire_rear].x=i;fire_queue[fire_rear].y=j;fire_rear++;fire_dist[i][j] = 0;}else fire_dist[i][j] = INF;}}while (fire_front<fire_rear){Fire current=fire_queue[fire_front];fire_front++;for (int i=0;i<4;i++){int nx = current.x + dx[i];int ny = current.y + dy[i];if (nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nx][ny] != '#' && fire_dist[nx][ny] == INF) {fire_dist[nx][ny] = fire_dist[current.x][current.y] + 1;fire_queue[fire_rear].x = nx;fire_queue[fire_rear].y = ny;fire_rear++;}}}
}
int bfs_joe() {int start_x = -1, start_y = -1;for (int i = 0; i < R; i++){for (int j = 0; j < C; j++){if (maze[i][j] == 'J'){start_x = i;start_y = j;break;}}if (start_x != -1) break;}front = rear = 0;queue[rear].x = start_x;queue[rear].y = start_y;queue[rear].time = 0;rear++;dist[start_x][start_y] = 0;while (front < rear){Position current = queue[front];front++;for (int i = 0; i < 4; i++){int nx = current.x + dx[i];int ny = current.y + dy[i];if (nx < 0 || nx >= R || ny < 0 || ny >= C)return current.time + 1;if (nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nx][ny] != '#' && dist[nx][ny] == -1 && (current.time + 1 < fire_dist[nx][ny] || fire_dist[nx][ny] == INF)){dist[nx][ny] = current.time + 1;queue[rear].x = nx;queue[rear].y = ny;queue[rear].time = current.time + 1;rear++;}}}return -1;
}
int main()
{int T;cin>>T:while (T--){cin>>R>>C;for (int i=0;i<R;i++) cin>>maze[i];bfs_fire();memset(dist, -1, sizeof(dist));int result = bfs_joe();if (result == -1) cout<<"IMPOSSIBLE"<<endl;else cout<<result<<endl;}return 0;
}

Problem E 林大超市买水果-搜索:
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int M, N, K;
int prices[31];
bool found = false;
void dfs(int start,int count,int sum)
{if (found) return;if (count == K){if (sum == M){found = true;}return;}for (int i=start;i<N;i++){if (sum+prices[i]>M) continue;dfs(i+1,count+1,sum+prices[i]);}
}
int main()
{cin>>M>>N>>K;for (int i = 0; i < N; i++)cin>>prices[i];dfs(0,0,0);if (found) printf("Yes\n");else printf("No\n");return 0;
}

Problem F 瓷砖-搜索:
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;int graph[51][51];
int w, h, startX, startY, cnt = 0;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
void dfs(int x, int y)
{graph[x][y] = 0;cnt++;for (int i = 0; i < 4; i++){int nx = x + dx[i];int ny = y + dy[i];if (nx >= 1 && nx <= w && ny >= 1 && ny <= h && graph[nx][ny] == 1){dfs(nx, ny);}}
}int main()
{while (cin >> w >> h && w && h){memset(graph, 0, sizeof(graph));cnt = 0;for (int i = 1; i <= h; i++) {for (int j = 1; j <= w; j++) {char ch;cin >> ch;if (ch == '#') graph[j][i] = 0;else if (ch == '.') graph[j][i] = 1;else if (ch == '@'){startX = j;startY = i;graph[j][i] = 1;}}}dfs(startX,startY);cout<<cnt<<endl;}return 0;
}

Problem G 最大黑色区域-搜索:
在这里插入图片描述

#include<bits/stdc++.h> 
using namespace std;
int n,m,ans=1,mmax;
char mapc[105][105];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x0, int y0)
{for (int i=0;i<4;i++){int x =x0+dir[i][0];int y =y0+dir[i][1];if (mapc[x][y] == '1'){mapc[x][y] = '0';ans++;dfs(x, y);}}
}int main()
{cin>>n>>m;for (int i=1;i<=n;i++){for (int j=1;j<=m;j++)cin>>mapc[i][j];}for (int i=1;i<=n;i++){for (int j=1;j<=m;j++){if (mapc[i][j] == '1'){mapc[i][j]='0';dfs(i,j);mmax=max(mmax,ans);ans = 1;}}}cout << mmax << endl;return 0;
}

Problem H 猴群-搜索:
在这里插入图片描述

#include<bits/stdc++.h> 
using namespace std;
int n, m, ans = 0;
char mapc[105][105];
int dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};void dfs(int x, int y)
{for (int i = 0; i < 4; i++){int nx = x + dir[i][0];int ny = y + dir[i][1];if (nx>=1 && nx<=n && ny>=1 && ny<=m && mapc[nx][ny]!='0'){mapc[nx][ny] = '0';dfs(nx,ny);}}
}int main()
{cin >> n >> m;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++)cin >> mapc[i][j];}for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (mapc[i][j] != '0'){ans++;dfs(i,j);}}}cout << ans<<endl;return 0;
}

总体来说除D题是BFS算法,E题不是迷宫问题,其余均是基本的DFS迷宫题型,代码容易且相似。

相关文章:

  • VScode在 Markdown 编辑器中预览
  • 聊一聊接口测试的核心优势及价值
  • echarts自定义图表
  • AI与智能农业:如何通过精准农业提升作物产量与资源使用效率?
  • Linux进程学习【环境变量】进程优先级
  • AUTOSAR_Feature_Model_Analysis
  • c++流对象
  • 智慧水库与AI深度融合的实现方案及典型应用场景
  • MySQL快速入门篇---增删改查(下)
  • LeetCode 24 两两交换链表中的节点
  • 【深度好文】4、Milvus 存储设计深度解析
  • 【Nginx】负载均衡配置详解
  • 【2025最新Java面试八股】如何在Spring启动过程中做缓存预热?
  • kafka 中消费者 groupId 是什么
  • [python] 基于WatchDog库实现文件系统监控
  • Seaborn模块练习题
  • GCC 内建函数汇编展开详解
  • 【数据挖掘】时间序列预测-时间序列预测策略
  • 脏读、幻读、可重复读
  • 反序列化漏洞2
  • 国家发改委:我国能源进口来源多元,企业减少甚至停止自美能源进口对国内能源供应没有影响
  • 党旗下的青春|83岁仍在“下生活”,他说生活是创作的源泉
  • 三杀皇马剑指四冠,硬扛到底的巴萨,赢球又赢人
  • 巴黎奥运后红土首秀落败,郑钦文止步马德里站次轮
  • 乐聚创始人:人形机器人当前要考虑泡沫问题,年底或将进入冷静期
  • “2025上海西九文化周”启动,香港顶尖文艺6月齐聚申城