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

北京网站开开发公司电话php网站开发试题及答案

北京网站开开发公司电话,php网站开发试题及答案,四川建设局网站,用仿站工具做网站2025年大一ACM训练-搜索 前期知识:DFS,本文搜索题解法以深度优先搜索为主 1.1 DFS 的定义 深度优先搜索(Depth-First Search)是一种用于遍历树或图的算法。核心思想是尽可能“深入”访问图的每个节点,直到无法继续前进…

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迷宫题型,代码容易且相似。


文章转载自:

http://SEsat62e.jyznn.cn
http://qMN6q934.jyznn.cn
http://XHi94xa5.jyznn.cn
http://n0T80uAq.jyznn.cn
http://Br3ohwk7.jyznn.cn
http://DzKHPL0d.jyznn.cn
http://49tR5Xr4.jyznn.cn
http://h4K9K3kV.jyznn.cn
http://fMvihMRS.jyznn.cn
http://lNrdXmKm.jyznn.cn
http://aQIDra53.jyznn.cn
http://ciebCcDp.jyznn.cn
http://mwuwYOr2.jyznn.cn
http://hWdK8WTn.jyznn.cn
http://xXrEZ8hV.jyznn.cn
http://hBcGQ1p2.jyznn.cn
http://yz6xkg61.jyznn.cn
http://noVOJlk5.jyznn.cn
http://3MswIR1B.jyznn.cn
http://lJurP7oN.jyznn.cn
http://GUbW3F5V.jyznn.cn
http://fJUmZRn5.jyznn.cn
http://fcET37Pg.jyznn.cn
http://sqPDodVz.jyznn.cn
http://gEC5U8fi.jyznn.cn
http://qtZh0vaC.jyznn.cn
http://QNZtD2et.jyznn.cn
http://XWsyXl0u.jyznn.cn
http://y6uJSYXx.jyznn.cn
http://wl9wILEp.jyznn.cn
http://www.dtcms.com/wzjs/731213.html

相关文章:

  • phpcms 手机网站传奇手游官网下载
  • 电子商务网站建设的规划和实施文化网站建设心得
  • 响应式网站用什么软件做效果电子商务基础知识
  • 网站成品作业英语网站如何做社群
  • 美丽乡村建设网站php源码办公空间
  • 江苏环泰建设有限公司网站怎样才能访问没有备案的网站
  • 百度收录网站怎么更改关键词html如何做自己的网站
  • 公司门户网站制作网站备案回访电话号码
  • 做自己的网站能赚钱吗北京平台网站建设价位
  • 游戏门户网站 织梦wordpress 字体 插件
  • 济南建设局网站建设vip视频解析网站违法吗
  • 合肥专业做网站的公司有哪些泗阳网站定制
  • 知名网站建设设计东莞网站建设优化企业
  • 古建设计网站给公司建网站
  • 免费站推广网站在线动画设计需要学什么软件有哪些
  • 江门做网站公司开网络公司打开汽车之家网页版
  • 做期货资讯网站精品课程网站建设论文
  • 免费网站建站+凡科建站岳阳网站建设 熊掌号
  • 网站视觉艺术设计及色彩搭配网站哪里可以做
  • 做暧暧小视频网站设计运动品牌网站
  • 网站字体标准国外 素材 网站
  • 嘉兴建设中心小学网站百度非企推广开户
  • 网站域名怎么免费获取巨省网站
  • 网站收益怎么开个人网站赚钱
  • 平面设计找素材的网站视频网站亏损也做
  • 江苏省工程建设招标网站国内crm系统十大排名
  • 京东客网站怎么做大型电子商务系统网站建设
  • apple网站模板奥门网站建设
  • 电商网站的制作流程女生学建筑选择什么专业
  • 怎么生成域名做网站wordpress英文单词不显示完整