#include<iostream>#include<vector>#include<cstring>#include<algorithm>#include<queue>usingnamespace std;constint maxn =30;int m, n, matrix[maxn][maxn];bool iqu[maxn][maxn]={false};//iqu存储是否入过队,false没入过,true代表入过int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};struct node{int x, y;};//BFS 不是用递归实现的,所以不需要显式写出结束条件,但是需要将当前节点入队,作为第一个元素;voidBFS(int x,int y){
queue<node> q;
q.push({x, y});
iqu[x][y]=true;//当队列为空后,迭代结束while(!q.empty()){
node temp = q.front();
q.pop();for(int i =0; i <4; i ++){int x0 = temp.x + dx[i], y0 = temp.y + dy[i];if(x0 <0|| x0 >= m || y0 <0|| y0 >= n)continue;//对于涉及到下标的问题,先看下标是否合法//在下标合法的情况下,如果位置是1,并且没有入过队,才继续进行if(matrix[x0][y0]==1&& iqu[x0][y0]==false){
q.push({x0, y0});
iqu[x0][y0]=true;}}}return;}intmain(){int ans =0;//读入矩阵scanf("%d%d",&m,&n);for(int i =0; i < m; i ++){for(int j =0; j < n; j ++){scanf("%d",&matrix[i][j]);}}//遍历矩阵for(int i =0; i < m; i ++){for(int j =0; j < n; j ++){if(matrix[i][j]==1&& iqu[i][j]==false){
ans++;BFS(i, j);}}}
cout << ans;system("pause");return0;}
求连通块问题(DFS)
#include<iostream>#include<vector>#include<cstring>#include<algorithm>#include<queue>usingnamespace std;constint maxn =30;int m, n, matrix[maxn][maxn];bool check[maxn][maxn]={false};//check代表是否检查过int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};//DFS进行后剪枝,如果下标不合法,或者不是1,或者已经检查过,则返回voidDFS(int x0,int y0){//下标不合法,则返回if(x0 <0|| x0 >= m || y0 <0|| y0 >= n)return;//这个数据不是1,或者已经处理过,则返回if(matrix[x0][y0]!=1|| check[x0][y0]==true)return;
check[x0][y0]=true;//将当前元素设置为已检查for(int i =0; i <4; i ++){int x = x0 + dx[i], y = y0 + dy[i];DFS(x, y);}return;}intmain(){int ans =0;//读入矩阵scanf("%d%d",&m,&n);for(int i =0; i < m; i ++){for(int j =0; j < n; j ++){scanf("%d",&matrix[i][j]);}}//遍历矩阵for(int i =0; i < m; i ++){for(int j =0; j < n; j ++){if(matrix[i][j]==1&& check[i][j]==false){
ans++;DFS(i, j);}}}
cout << ans;system("pause");return0;}
求最少步数(BFS)
#include<iostream>#include<vector>#include<cstring>#include<algorithm>#include<queue>usingnamespace std;constint maxn =30;int m, n, ans =-1;char matrix[maxn][maxn];bool check[maxn][maxn]={false};//check代表是否入过队int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};struct node{int x, y, step;//step表示第几步到达它}S, T;voidBFS(){
queue<node> q;
q.push(S);
check[S.x][S.y]=true;//开始点入队后要设为已入队while(!q.empty()){
node top = q.front();
q.pop();//判断队首元素是不是终点if(top.x == T.x && top.y == T.y){
ans = top.step;break;}for(int i =0; i <4; i ++){int x = top.x + dx[i], y =top.y + dy[i];if(x <0|| x >= m || y <0|| y >= n)continue;//如果下标不满足,直接continue//如果不是墙且没有入过队则入队if(matrix[x][y]!='*'&& check[x][y]==false){
node temp ={x, y, top.step +1};
q.push(temp);
check[x][y]=true;}}}return;}intmain(){//读入矩阵scanf("%d%d",&m,&n);for(int i =0; i < m; i ++){getchar();//过滤回车for(int j =0; j < n; j ++){scanf("%c",&matrix[i][j]);}}scanf("%d%d%d%d",&S.x,&S.y,&T.x,&T.y);
S.step =0;//进行BFSBFS();
cout << ans;system("pause");return0;}
求最少步数(DFS)
#include<iostream>#include<vector>#include<cstring>#include<algorithm>#include<queue>usingnamespace std;constint maxn =30;int m, n, matrix[maxn][maxn];bool check[maxn][maxn]={false};//check代表是否检查过int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};int ans =-1;struct node{int x, y;}S, T;//DFS进行后剪枝,如果下标不合法,或者不是1,或者已经检查过,则返回voidDFS(int x0,int y0,int step){//下标不合法,则返回if(x0 <0|| x0 >= m || y0 <0|| y0 >= n)return;if(matrix[x0][y0]=='*')return;//如果这个点恰好是出口,跟当前全局步数比较大小if(x0 == T.x && y0 == T.y){
ans =max(step, ans);return;}for(int i =0; i <4; i ++){int x = x0 + dx[i], y = y0 + dy[i];DFS(x, y, step +1);}return;}intmain(){//读入矩阵scanf("%d%d",&m,&n);for(int i =0; i < m; i ++){getchar();//过滤回车for(int j =0; j < n; j ++){scanf("%c",&matrix[i][j]);}}scanf("%d%d%d%d",&S.x,&S.y,&T.x,&T.y);//进行DFSDFS(S.x, S.y,0);
cout << ans;system("pause");return0;}