所谓深搜就是不撞南墙不回头的搜法他借助递归来实现,就是着一条路,直至不满足条件返回上一个在着一条路继续,知道找完为止,举个例题点击打开链接
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers:
N and
K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
这个就可以用深搜,但是会超时,因为它的时间复杂度为n方,我们就根据这个题来详细说明一下这个让我头疼的dfs
#include<iostream>
#include<cstdio>
using namespace std;
int vis[100005];
int minx=0x3f3f3f3f;
int m,n;//前面都是必写的,vis是标记走过的点的,mixn是记录最短步数
void dfs(int pos,int step){
if(pos==n){//当位置找到时
if(step<minx){//找最小步数
minx=step;
}
return ;//记得要再返回上一步继续搜索,因为没搜完,可以调试看下
}
for(int i=0;i<3;i++){//dfs搜索的条件用循环来解决
int t;
//对应的条件
if(i==0) t=pos-1;
else if(i==1) t=pos+1;
else if(i==2) t=pos*2;
//确定范围
if(t>4*n||t<=0||t>100000) return;//如果超出范围 ,换下一种继续搜索
if(!vis[t]){
vis[t]=1;//找到一步满足条件,标记并用dfs从这个点开始再搜
dfs(t,step+1);//步数要加1的
vis[t]=0;//因为要返回,所以取消标记
}
}
}
int main()
{
scanf("%d%d",&m,&n);
vis[m]=1;
dfs(m,0);
printf("%d",minx);
}
后面会说到广搜
在看一个
由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的矩形来表示。农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘。八连通的积水被认为是一块积水。
给你约翰农场的航拍图,确定有多少池塘?
Input
给你约翰农场的航拍图,确定有多少池塘?
* 第1行:N 和 M
*第2行:N+1: M个字符一行,每个字符代表约翰的农场的土地情况。每个字符中间不包含空格。
Output
*第2行:N+1: M个字符一行,每个字符代表约翰的农场的土地情况。每个字符中间不包含空格。
* 第1行:池塘的数量
Sample Input
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
Sample Output
3
同样是按dfs
#include <stdio.h>
char a[105][105];
int ans;
int n,m;
void dfs(int x,int y){
for(int dx=-1;dx<=1;dx++){
for(int dy=-1;dy<=1;dy++){
int tx=x+dx;
int ty=y+dy;
//以上移动条件
if(tx<0||ty<0||tx>n||ty>m) continue;//是否越界
if(a[tx][ty]=='W'){//标记再搜
a[tx][ty]='.';
dfs(tx,ty);
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",a[i]);
}
ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]=='W'){
dfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
}
//下面是四个方向的,上面是八个方向的,体会区别
/*#include <stdio.h>
char a[105][105];
int ans;
int n,m;
void dfs(int x,int y){
for(int dx=-1;dx<=1;dx++){
int tx=x+dx;
if(tx<0||tx>n) continue;
if(a[tx][y]=='W'){
a[tx][y]='.';
dfs(tx,y);
}
}
for(int dy=-1;dy<=1;dy++){
int ty=y+dy;
if(ty<0||ty>m) continue;
if(a[x][ty]=='W'){
a[x][ty]='.';
dfs(x,ty);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",a[i]);
}
ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]=='W'){
dfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
} */
在看一个,代码如出一辙
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1Sample Output
2 1
#include <stdio.h>
#include <string.h>
#define maxn 105
char a[maxn][maxn];
int flag[maxn];
int sum;
int n,m;
int ans;
void dfs(int i){
if(sum==m){
ans++;
return;
}
if(i>=n) return;//循环可以continue,其他用return;
for(int j=0;j<n;j++){
if(a[i][j]=='#'&&flag[j]==0){
flag[j]=1;
sum++;
dfs(i+1);
sum--;
flag[j]=0;
}
}
dfs(i+1);
}
int main(){
while(scanf("%d%d",&n,&m),n+m!=-2){
sum=0;
ans=0;
memset(flag,0,sizeof flag);
for(int i=0;i<n;i++){
scanf("%s",a[i]);
}
dfs(0);
printf("%d\n",ans);
}
}