7.5搜索-路径问题
课堂练习
-P1958 上学路线-
题目描述
你所在城市的街道好像一个棋盘,有
你住在
现在有
问你上学一共有多少走法?
思路
- 第一,由于这题是路径问题,所以可以想到深度优先搜索
(DFS) ,只不过需要特判一下障碍物(可以在数组中标记为2 ) - 第二,我们需要注意这题只有两个方向
- 第三,由于这题的问题是求路径数,所以需要回溯
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,k,x,y,cnt=0;
int a[20][20];
void dfs(int xx,int yy)
{
if(xx==n && yy==m)
{
cnt++;
return ;
}
else
{
if(a[xx][yy]!=2)
{
dfs(xx,yy+1);//向下
dfs(xx+1,yy);//向右
}
}
}
int main()
{
cin>>n>>m>>k;
memset(a,0,sizeof(a));
while(k--)
{
cin>>x>>y;
a[x][y]=2;//障碍物标记
}
for(int i=1;i<=n;i++)//防止越界
{
a[i][m+1]=2;
}
for(int i=1;i<=m;i++)//防止越界
{
a[n+1][i]=2;
}
dfs(1,1);
cout<<cnt;
return 0;
}
-D3432 【基础】迷宫的第一条出路-
题目描述
已知一
思路
- 第一,由于这题需要按照左、上、右、下的顺序进行搜索,所以我们的方向数组需要改动(具体请看下面代码)
- 第二,因为这题需要输出具体路径,所以我们要新建一个
pre 数组用于储存上一步位置,再新建一个print 函数用于路径的输出(具体也请看下面代码)代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
int x,y;
}pre[30][30];//用于储存上一步位置
int dx[5]={0,0,-1,0,1};
int dy[5]={0,-1,0,1,0};
int n;
char c[30][30];
bool vis[30][30];
void print(int x,int y)//用于路径的输出
{
if(x==1 && y==1)
{
cout<<"(1,1)";
return ;
}
print(pre[x][y].x,pre[x][y].y);
printf("->(%d,%d)",x,y);
}
void dfs(int x,int y)
{
if(x==n && y==n)
{
print(x,y);
return ;
}
for(int i=0;i<=4;i++)
{
int nx,ny;
nx=x+dx[i];
ny=y+dy[i];
if(nx>=1 && nx<=n && ny>=1 && ny<=n && c[nx][ny]=='0' && vis[nx][ny]==false)
{
vis[nx][ny]=true;
pre[nx][ny]={x,y};
dfs(nx,ny);
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>c[i][j];
}
}
vis[1][1]=true;
dfs(1,1);
return 0;
}
-D3361 【基础】卒的遍历-
题目描述
在一张
思路
- 这题其实和上一题差不多,但是需要注意方向数组和输出的改动(没什么好说的)
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
int x,y;
}pre[30][30];
int dx[3]={0,1,0};
int dy[3]={0,0,1};
int n,m,cnt;//cnt:路径的数量
int a[30][30];
bool vis[30][30];
void print(int x,int y)
{
if(x==1 && y==1)
{
cout<<"1,1";
return ;
}
print(pre[x][y].x,pre[x][y].y);
printf("->%d,%d",x,y);
}
void dfs(int x,int y)
{
if(x==n && y==m)
{
cnt++;
printf("%d:",cnt);
print(x,y);
cout<<"\n";
return ;
}
for(int i=1;i<=3;i++)
{
int nx,ny;
nx=x+dx[i];
ny=y+dy[i];
if(nx>=1 && nx<=n && ny>=1 && ny<=m && vis[nx][ny]==false)
{
vis[nx][ny]=true;
pre[nx][ny]={x,y};
dfs(nx,ny);
vis[nx][ny]=false;
}
}
}
int main()
{
cin>>n>>m;
vis[1][1]=true;
dfs(1,1);
return 0;
}
-P1238 走迷宫-
crj太水了哈哈哈哈哈哈哈哈哈哈哈哈哈哈……(此处省略 10^9 个哈)
这题更没什么好说的了,几乎和上一题一模一样,只是输出改了一点
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
int x,y;
}pre[20][20];
int dx[5]={0,0,-1,0,1};
int dy[5]={0,-1,0,1,0};
int n,m,fx,fy,zx,zy;
char c[20][20];
bool vis[20][20],flag;
void print(int x,int y)
{
if(x==fx && y==fy)
{
printf("(%d,%d)",fx,fy);
return ;
}
print(pre[x][y].x,pre[x][y].y);
printf("->(%d,%d)",x,y);
}
void dfs(int x,int y)
{
if(x==zx && y==zy)
{
flag=true;
print(x,y);
cout<<"\n";
return ;
}
for(int i=1;i<=4;i++)
{
int nx,ny;
nx=x+dx[i];
ny=y+dy[i];
if(nx>=1 && nx<=n && ny>=1 && ny<=m && c[nx][ny]=='1' && vis[nx][ny]==false)
{
vis[nx][ny]=true;
pre[nx][ny]={x,y};
dfs(nx,ny);
vis[nx][ny]=false;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>c[i][j];
}
}
cin>>fx>>fy;
cin>>zx>>zy;
vis[fx][fy]=true;
dfs(fx,fy);
if(!flag)
{
cout<<"-1";
}
return 0;
}
家庭作业
@陈人杰老师 由于我的时间有一点赶,所以家庭作业的题目描述和思路就用注释代替了,见谅QaQ
-P1644 跳马问题-
代码及注释
#include<bits/stdc++.h>
using namespace std;
int m,n,cnt;
void dfs(int a,int b)//a:横坐标,b:纵坐标
{
if (a<0 || a>n || b>m)
{//马越界的情况有三种,有行数超(正负两种)和列数超(只有正)
return ;
}
if (a==n && b==m)
{
cnt++;//找到了总数++
}
else
{
dfs(a+1,b+2);
dfs(a+2,b+1);
dfs(a-2,b+1);
dfs(a-1,b+2);
}
}
int main()
{
cin>>n>>m;
dfs(0,0);
cout<<cnt;
return 0;
}
AC记录
-P1101 单词方阵-
代码及注释
#include<bits/stdc++.h>
using namespace std;
string ans=" yizhong";
char a[110][110];
bool book[110][110];
int t;
int dfs(int n,int m,int no,int f)
{
if(n<1 || m<1 || n>t || m>t)//如果越界则返回
{
return 0;
}
if(a[n][m]==ans[no])//如果是对应字母,则继续深搜
{
if(no==7)//如果搜到最后一个,则返回1,表示深搜成功
{
book[n][m]=1;
return 1;
}
else
{
//有选择性的搜索
switch(f)
{
case 1: if(dfs(n-1,m-1,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 2: if(dfs(n-1,m,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 3: if(dfs(n-1,m+1,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 4: if(dfs(n,m-1,no+1,f)){book[n][m]=1;return 1;}else return 0;
/*
case 5表示no==1,
作为第一个字母,因此是个特例,要向所有方向搜索
其余情况都只向一个特定方向搜索
*/
case 5:
if(dfs(n-1,m-1,no+1,1)) book[n][m]=1;
if(dfs(n-1,m,no+1,2)) book[n][m]=1;
if(dfs(n-1,m+1,no+1,3)) book[n][m]=1;
if(dfs(n,m-1,no+1,4)) book[n][m]=1;
if(dfs(n,m+1,no+1,6)) book[n][m]=1;
if(dfs(n+1,m-1,no+1,7)) book[n][m]=1;
if(dfs(n+1,m,no+1,8)) book[n][m]=1;
if(dfs(n+1,m+1,no+1,9)) book[n][m]=1;
break;
case 6: if(dfs(n,m+1,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 7: if(dfs(n+1,m-1,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 8: if(dfs(n+1,m,no+1,f)){book[n][m]=1;return 1;}else return 0;
case 9: if(dfs(n+1,m+1,no+1,f)){book[n][m]=1;return 1;}else return 0;
}
}
}
else//否则返回0
{
return 0;
}
return 0;//注意要返回,不然全部RE
}
int main()
{
cin>>t;
for(int i=1;i<=t;i++)
{
for(int j=1;j<=t;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=t;i++)
{
for(int j=1;j<=t;j++)
{
dfs(i,j,1,5);
}
}
for(int i=1;i<=t;i++)
{
for(int j=1;j<=t;j++)
{
if(book[i][j])
{
cout<<a[i][j];
}
else
{
cout<<"*";
}
}
cout<<"\n";
}
return 0;
}
AC记录
-P12266 [蓝桥杯 2024 国 Python B] 不同的总分值-
思路
使用搜索去枚举每道题的通过情况,当所有的题的通过情况都枚举完之后,把通过的题的分数加起来就是这组通过情况的最终分数,然后把这个分数打上标记。等最后搜索完之后再把打上标记的分数个数统计出来就是答案。
最终求出答案为
代码
#include<bits/stdc++.h>
using namespace std;
int a[20]={0,5,5,10,10,15,15,20,20,25,25};
bool b[210];
int ans=0;
void dfs(int k,int s)
{
if(k>10)
{
b[s]=1;
return;
}
dfs(k+1,s);
dfs(k+1,s+a[k]);
}
int main()
{
dfs(1,0);
for(int i=0;i<=150;i++)
{
ans+=b[i];
}
printf("%d\n",ans);
return 0;
}
AC记录