深度优先搜索

  • 数据型DFS

P2036 [COCI2008-2009#2] PERKET

#include<bits/stdc++.h>
using namespace std;
const int N = 50;
const int INF = 0x3f3f3f3f;

int n,ans = INF;
int acid[N],sweet[N];

void DFS(int i,int x,int y){
	
	if(i > n){
		if(x == 1 && y == 0)return;
		ans = min(ans,abs(x - y));
		return;
	}
	
	DFS(i + 1,x * acid[i],y + sweet[i]);
	DFS(i + 1,x,y);
	
}

int main(){
	
	cin>>n;
	for(int i = 1;i <= n;i++)cin>>acid[i]>>sweet[i];
	
	DFS(1,1,0);
	
	cout<<ans<<endl; 
} 

H1016 素数环(DFS+素数筛)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 51;

int n,cnt;
int prime[N];
bool vis[N];
int ans[N];

void prime_set(){//素数筛 
	
	for(int i = 2;i <= sqrt(N);i++)
		if(!prime[i])for(int j = 2;i * j <= N;j++)prime[i * j] = 1;
		
	prime[1] = 0,vis[1] = true;
}

void DFS(int step){
	
	//出口 
	if(prime[ans[step - 1] + ans[step - 2]])return;
	if(step == n + 1 && prime[ans[n] + 1])return;
	
	//结束输出 
	if(step == n + 1){
		for(int i = 1;i < n;i++)cout<<ans[i]<<" ";
		cout<<ans[n]<<endl;
		
	}
	
	//搜索 
	for(int i = 2;i <= n;i++){
		if(!vis[i]){
			
			vis[i] = true;
			ans[step] = i;//赋值 
			DFS(step + 1);
			vis[i] = false;//回溯 
		}
	}
}

int main(){
	
	prime_set();
	
	while(~scanf("%d",&n)){
		
		ms(vis,false);
		
		cout<<"Case "<<++cnt<<":"<<endl;
		
		ans[1] = 1;
		if(n == 1)cout<<"1"<<endl;
		else DFS(2);
		
		cout<<endl;
	}
	
	return 0;
}

H1258 Sum It Up

#include<iostream>
#include<cstring>
#include<algorithm>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 15;
//去重 难点 
int t,n;
int num[N],ans[N],flag;
bool vis[N];

void DFS(int place,int x,int sum){
	
	if(sum == t){
		
		flag = 1;
		if(x == 1)cout<<ans[1]<<endl;
		else{
			for(int i = 1;i < place - 1;i++)cout<<ans[i]<<"+";
			cout<<ans[place - 1]<<endl;
		}
		return;
	}
	
	if(x > n || sum > t)return;//写在前面的话可能正好 x == n + 1 这种情况会漏掉 
	
	ans[place] = num[x];
	DFS(place + 1,x + 1,sum + num[x]);
	
	while(x < n && num[x] == num[x + 1])x++;//??????
	DFS(place,x + 1,sum); //???? 真没看懂 
	
}

int main(){
	
	while(true){
		
		flag = 0;
		ms(vis,false);
		
		cin>>t>>n;
		if(!n)break;
		for(int i = 1;i <= n;i++)cin>>num[i];
		
		cout<<"Sums of "<<t<<":"<<endl;
		DFS(1,1,0);
		
		if(flag == 0)cout<<"NONE"<<endl;
	}
	return 0;
}

P2392 kkksc03考前临时抱佛脚

#include<bits/stdc++.h>
using namespace std;
const int N = 50;
const int INF = 0x3f3f3f3f;

int n,ans = INF;
int acid[N],sweet[N];

void DFS(int i,int x,int y){
	
	if(i > n){
		if(x == 1 && y == 0)return;
		ans = min(ans,abs(x - y));
		return;
	}
	
	DFS(i + 1,x * acid[i],y + sweet[i]);
	DFS(i + 1,x,y);
	
}

int main(){
	
	cin>>n;
	for(int i = 1;i <= n;i++)cin>>acid[i]>>sweet[i];
	
	DFS(1,1,0);
	
	cout<<ans<<endl; 
} 

P2404 自然数的拆分问题

#include<bits/stdc++.h>
using namespace std;
const int N = 10000;

int n;
int num[N] = {1};

void print(int x){
	for(int i = 1;i < x;i++)cout<<num[i]<<"+";
	cout<<num[x]<<endl;
}

int DFS(int x,int sum){
	
	for(int i = num[x - 1];i <= sum;i++)
		if(i < n){
			num[x] = i;
			sum-= i;
			if(!sum)print(x);
			else DFS(x + 1,sum);
			sum += i;
		}
}

int main(){
	cin>>n;
	DFS(1,n);
	return 0;
}

P2392 kkksc03考前临时抱佛脚

#include<bits/stdc++.h>
using namespace std;
const int N = 50;
const int INF = 0x3f3f3f3f;

int s1,s2,s3,s4;
int l,r,ans,minn;

int work[N][5];
int s[N];

void DFS(int x,int i){
	
	if(x > s[i]){
		minn = min(minn,max(l,r));
		return;
	}
	
	l += work[x][i];
	DFS(x + 1,i);
	l -= work[x][i];
	
	r += work[x][i];
	DFS(x + 1,i);
	r -= work[x][i];
}

int main(){
	
	cin>>s[1]>>s[2]>>s[3]>>s[4];
	
	for(int i = 1;i <= 4;i++){
		l = 0;
		r = 0;
		minn = INF;
		
		for(int j = 1;j <= s[i];j++)cin>>work[j][i];
		DFS(1,i);
		ans += minn;
	}
	
	cout<<ans; 
} 
  • 地图型DFS

P1605 迷宫

#include<bits/stdc++.h>
using namespace std;
const int N = 10;

int n,m,t,sx,sy,fx,fy,ans = 0;
int a,b;
int table[N][N];
int vis[N][N];

int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int check(int x,int y){
	if(x < 1 || x > n)return 0;
	if(y < 1 || y > m)return 0;
	return 1;
}

void DFS(int x,int y){
	//cout<<x<<" "<<y<<endl;
	//if(!check(x,y))return;
	
	if(x == fx && y == fy){
		ans++;
		return;
	}
	
	for(int i = 0;i <= 3;i++)
		if(table[x + dir[i][0]][y + dir[i][1]] == 1 && !vis[x + dir[i][0]][y + dir[i][1]]){
			vis[x][y] = 1;//是给走过的打上标记 
			DFS(x + dir[i][0],y + dir[i][1]);
			vis[x][y] = 0;
		}
	//如果没有障碍并且不是自己走过的,就进一步搜索,把自己走过的路打上标记,返回时,再将标记还原;
}

int main(){
	cin>>n>>m>>t;
	cin>>sx>>sy;
	cin>>fx>>fy;
	
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)table[i][j] = 1;
	//有一个zz把 table[i][j] = 1 写成table[i][j] == 1  然后查了半个小时代码  没错是我 
	for(int i = 1;i <= t;i++){
		cin>>a>>b;
		table[a][b] = 0;
	}
		
	DFS(sx,sy);
	
	cout<<ans;
	return 0;
}

P1596 [USACO10OCT]Lake Counting S

#include<bits/stdc++.h>
using namespace std;
const int N = 200;

int n,m,ans;
char table[N][N];
bool vis[N][N];

int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1}};

int check(int x,int y){
	if(x < 1 || x > n)return 0;
	if(y < 1 || y > m)return 0;
	return 1;
}

void DFS(int x,int y){
	
	table[x][y] = '.';
	
	for(int i = 0;i <= 7;i++)
		if(table[x + dir[i][0]][y + dir[i][1]] == 'W' && !vis[x + dir[i][0]][y + dir[i][1]]){
			vis[x][y] = 1;//是给走过的打上标记 
			DFS(x + dir[i][0],y + dir[i][1]);
			vis[x][y] = 0;
		}
}

int main(){
	cin>>n>>m;
			
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)cin>>table[i][j];
	
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)if(table[i][j] == 'W'){
			ans++;
			DFS(i,j);
		}
				
	cout<<ans;
	return 0;
}

P1219 八皇后问题

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 100;
//好吧这种方法不太行 必须要把地图存成四个数组 或者一个二维是4的数组才行 
//这样能记录 四条对角线的状态
 
int n,cnt;

int table[N][N];
int vis[N][N];
int ans[N],b[N],c[N],d[N];

//ans数组表示的是行;
//b数组表示的是列;
//c表示的是左下到右上的对角线;
//d表示的是左上到右下的对角线;

int check(int x,int y){
	if(x < 1 || x > n)return 0;
	if(y < 1 || y > n)return 0;
	
	return 1;
}


void paint(int x,int y){
	
	int a,b;
	
	a = min(n - x,n - y);
	b = min(x - 1,y - 1);
	
	for(int i = 1;i <= n;i++)vis[x][i] == true;
	for(int i = 1;i <= n;i++)vis[i][y] == true;
	for(int i = 1;i <= a;i++)vis[x + i][y + i] == true;
	for(int i = 1;i <= b;i++)vis[x - i][y - i] == true;
	
}

void DFS(int x){
	
	if(x > n){
		cnt++;
		
		if(cnt <= 3){
			for(int i = 1;i < n;i++)cout<<ans[i]<<" ";
			cout<<ans[n]<<endl;
		}
			
		return;
	}
	
	for(int i = 1;i <= n;i++){//尝试所有可能 
		if(!b[i] && !c[x + i] && !d[x - i + n]){
			
			ans[x] = i;
			b[i] = 1;
			c[x + i] = 1;
			d[x - i + n] = 1;
			
			DFS(x + 1);
			
			b[i] = 0;
			c[x + i] = 0;
			d[x - i + n] = 0;//三步回溯 
			
		}
	}
}

int main(){
	cin>>n; 
	
	DFS(1); 
	cout<<cnt;
	return 0;
}

H1045 Fire Net

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 15;

int n,ans;
char table[N][N];

int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int check(int x,int y){
	
	if(table[x][y] != '.')return 0;
	
	int dx,dy;
	for(int i = 0;i < 4;i++){
		
		dx = x + dir[i][0];
		dy = y + dir[i][1];
		
		while(true){
			if(dx < 1 || dx > n || dy < 1 || dy > n || table[dx][dy] == 'X')break;
			else if(table[dx][dy] == '1')return 0;
			
			dx += dir[i][0], dy += dir[i][1];
		}
	} 
	return 1;
}

void DFS(int num){
	
	ans = max(ans,num);
	
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			if(check(i,j)){
				
				table[i][j] = '1';
				DFS(num + 1);
				table[i][j] = '.';
				
			}
		}
	}	
}

int main(){
	
	while(scanf("%d",&n) && n){
	
		ans = 0;
		for(int i = 1;i <= n;i++)for(int j = 1;j <= n;j++)cin>>table[i][j];
		DFS(0);
		cout<<ans<<endl;
		
	}
	
	return 0;
}

H1010 Tempter of the Bone

#include<cstring>
#include<iostream>
#include<cmath>
#define ms(a,b) memset(a,b,sizeof(a))
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int N = 15;
//奇偶剪枝

int n,m,t,sx,sy,fx,fy,ans;
char table[N][N];
bool vis[N][N];

int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

void DFS(int x,int y,int step){
	
	//出口 
	if(x < 1 || x > n)return;
	if(y < 1 || y > m)return;
	
	if(table[x][y] == 'D' && step == t){
		ans = 1;
		return;
	}
	
	//剪枝 光剪枝还不够... 
	if(abs(x - fx) + abs(y - fy) > t - step)return;//步数不够
	if((t - step) % 2 != (abs(x - fx) + abs(y - fy)) % 2)return;//奇偶性不同 
	
	//搜索 
	vis[x][y] = true; 
	for(int i = 0;i <= 3;i++){
		
		int dx,dy;
		dx = x + dir[i][0],dy = y + dir[i][1];
		
		if(table[dx][dy] != 'X' && !vis[dx][dy] && !ans){//!ans 表示如果成功了就直接停止搜素 
			
			DFS(dx,dy,step + 1);
			vis[dx][dy] = false;
		}
	}
}
		

int main(){
	
	FAST;
	
	while(cin>>n>>m>>t && n && m && t){
	
	//初始化 
	ans = 0;
	ms(vis,false);
	 
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++){
			
			cin>>table[i][j];
			if(table[i][j] == 'S')sx = i,sy = j;
			if(table[i][j] == 'D')fx = i,fy = j;
			
		}
		
	//搜索 
	DFS(sx,sy,0);
	
	//输出 
	if(ans)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	
	}
	return 0;
}

H1035 Robot Motion

#include<iostream>
#include<cstring>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 15;

int n,m,start;
int step,extra; 
char table[N][N];	
int vis[N][N];

//int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int check(int x,int y){
	if(x < 1 || x > n)return 0;
	if(y < 1 || y > m)return 0;
	return 1;
}

void DFS(int x,int y){
	
	if(!check(x,y)){
		cout<<step<<" step(s) to exit"<<endl;
		return;
	}
	
	if(vis[x][y] == 1)extra++;
	
	if(vis[x][y] == 2){
		cout<<step - 2 * extra<<" step(s) before a loop of "<<extra<<" step(s)"<<endl;
		return;
	}
	
	step++;
	vis[x][y]++;
	
	if(table[x][y] == 'N')DFS(x - 1,y);
	if(table[x][y] == 'S')DFS(x + 1,y);
	if(table[x][y] == 'W')DFS(x,y - 1);
	if(table[x][y] == 'E')DFS(x,y + 1);
}

int main(){
	
	while(scanf("%d %d",&n,&m) && n != 0 && m != 0){
		
		scanf("%d",&start);
		step = 0,extra = 0;
		ms(vis,0);
		for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)cin>>table[i][j];
		DFS(1,start);
		
	}
	
	return 0;
}

我死会儿…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值