DFS————深度优先搜索2.例题

1.组合的输出

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int m,n;

int a[22];

void input()
{
	cin>>m>>n;
}

void print()
{
	for(int i = 1;i<=n;i++)
	{
		cout<<setw(3)<<a[i];
	}
	cout<<endl;
}

void dfs(int c,int q)
{
	if(c == n+1) print();
	else
	{
		for(int i = q+1;i<=m;i++)
		{
			a[c] = i;
			dfs(c+1,i);
		}
	}
}


int main()
{
	input();
	dfs(1,0);
	return 0;
}

2.八皇后问题

#include<bits/stdc++.h>
using namespace std;
int ps[9];
bool l,r;
int num = 1;
void dfs(int b);
void print();


void dfs(int b){
	if(b == 9){
		print();
		return;
	}
	else{
		for(int i = 1;i<=8;i++){
			bool yi = false;
			for(int j = b-1;j>0;j--){
				if((i+b == j+ps[j])||(b-i == j-ps[j])||(i == ps[j])) yi = true;
			}
			if(!yi){
				ps[b] = i;
				dfs(b+1);
			}
		}
	}
}

void print(){
	cout<<"No. "<<num++<<endl;
	for(int i = 1;i<=8;i++){
		for(int j = 1;j<=8;j++){
			if(i == ps[j]){
				cout<<"1 ";
			}
			else{
				cout<<"0 ";
			}
		}
		cout<<endl;
	}
}

int main(){
	dfs(1);
	while(true);
	return 0;
}

3.求先序排列

#include<iostream>
#include<cmath>
using namespace std;

struct Tree;

struct Tree{
	char date;
	Tree* leftTree=NULL,*rightTree=NULL;
};

void input(string &zhong,string &hou)
{
	cin>>zhong>>hou;
}

void chli(Tree* &node,string zhong,string hou)
{
		if(zhong == ""||hou == "") return;
		char nodec = hou[hou.size()-1];
		node = new(Tree);
		node->date = nodec;
		string zleft="",zright="";
		bool y = false;
		for(int i = 0;i<zhong.size();i++)
		{
			if(zhong[i] == nodec) {
				y = true;
				continue;
			}
			if(y) zright+=zhong[i];
			else zleft+=zhong[i];
		}
		string hleft="",hright="";
		for(int i = 0;i<zleft.size();i++)
		{
			hleft += hou[i];
		}
		for(int i = 0;i<zright.size();i++)
		{
			hright += hou[zleft.size()+i];
		}
		chli(node->leftTree,zleft,hleft);
		chli(node->rightTree,zright,hright);
}

void print(Tree* node)
{
	if(node == NULL) return;
	else
	{
		cout<<node->date;
		print(node->leftTree);
		print(node->rightTree);
	}
}


int main(){
	string zhong,hou;
	Tree *head;
	input(zhong,hou);
	chli(head,zhong,hou);
	print(head);
	return 0;
}
4.P1827 [USACO3.4] 美国血统 American Heritage
#include<iostream>
#include<cmath>
using namespace std;

struct Tree;

struct Tree{
	char date;
	Tree* leftTree=NULL,*rightTree=NULL;
};

void input(string &zhong,string &hou)
{
	cin>>zhong>>hou;
}

void chli(Tree* &node,string zhong,string hou)
{
		if(zhong == ""||hou == "") return;
		char nodec = hou[0];
		node = new(Tree);
		node->date = nodec;
		string zleft="",zright="";
		bool y = false;
		for(int i = 0;i<zhong.size();i++)
		{
			if(zhong[i] == nodec) {
				y = true;
				continue;
			}
			if(y) zright+=zhong[i];
			else zleft+=zhong[i];
		}
		string hleft="",hright="";
		for(int i = 0;i<zleft.size();i++)
		{
			hleft += hou[i+1];
		}
		for(int i = 0;i<zright.size();i++)
		{
			hright += hou[zleft.size()+i+1];
		}
		chli(node->leftTree,zleft,hleft);
		chli(node->rightTree,zright,hright);
}

void print(Tree* node)
{
	if(node == NULL) return;
	else
	{
		print(node->leftTree);
		print(node->rightTree);
		cout<<node->date;
	}
}


int main(){
	string zhong,hou;
	Tree *head;
	input(zhong,hou);
	chli(head,zhong,hou);
	print(head);
	return 0;
}
5.马走日
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
using namespace std;

int xj[] = {1,2,2,-1,1,-1,-2,-2},
	yj[] = {2,1,-1,2,-2,-2,1,-1};
bool qipan[9][9];
int T,n,m,cx,cy;
int ans;

void input();
void print();
void dfs(int x,int y);
void run();
bool pd();

int main()
{
	run();
	return 0;
}

void input()
{
	memset(qipan,0,sizeof(qipan));
	ans = 0;
	cin>>n>>m>>cx>>cy;
}

void print()
{
	cout<<ans<<endl;
}

void run()
{
	cin>>T;
	while(T--)
	{
		input();
		dfs(cx,cy);
		print();
	}
}

bool pd(){
	for(int i = 0;i<n;i++)
	{
		for(int j = 0;j<m;j++)
		{
			if(!qipan[i][j]) return false;
		}
	}
	return true;
}

void dfs(int x,int y)
{
	if(pd())
	{
		ans++;
		return;
	}
	else
	{
		if(x<0||y<0||x>=n||y>=m||qipan[x][y]) return;
		else
		{
			qipan[x][y] = true;
			for(int i = 0;i<8;i++){
				dfs(x+xj[i],y+yj[i]);
			}
			qipan[x][y] = false;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值