【POJ】DFS算法的应用

一、1E:Red and Black

http://algorithm.openjudge.cn/2020hw1/1E/
题目:根据图(邻接矩阵表示)进行深度优先遍历。

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
using namespace std;

const int maxn = 25;
char g[maxn][maxn];
bool vis[maxn][maxn];

int W, H, x, y, nx, ny, ans;
int ori[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

void init(){
	ans = 0;
	memset(vis, false, sizeof(vis));
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			cin >> g[i][j];
			if (g[i][j] == '@'){
				x = i, y = j;
			}
		}
	}
}

void dfs(int x, int y){
	ans++;
	vis[x][y] = true;
	for (int i = 0; i < 4; i++){
		nx = x + ori[i][0];
		ny = y + ori[i][1];
		if (nx >= 0 && nx < H&&ny >= 0 && ny < W&&!vis[nx][ny] && g[nx][ny] != '#')
			dfs(nx, ny);
	}
}

int main(){
	while ((cin >> W >> H) && W != 0 && H != 0){
		init();
		dfs(x, y);
		cout << ans << endl;
	}
	return 0;
}

二、打印1-n全排列数字

题目:https://www.acwing.com/problem/content/844
在这里插入图片描述

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

const int maxn = 110;
int n;
bool vis[maxn];
int p[maxn];

void dfs(int step){
	if (step == n){
		for (int i = 0; i < n; i++)
			cout << p[i] << " ";
		cout << endl;
		return;
	}
	for (int i = 1; i <= n; i++){
		if (!vis[i]){
			p[step] = i;
			vis[i] = true;
			dfs(step + 1);
			vis[i] = false;
		}
	}
}

int main(){
	memset(vis, false, sizeof(vis));
	cin >> n;
	dfs(0);
	return 0;
}

三、A:八皇后问题

http://algorithm.openjudge.cn/2020prefinaltest/A/
dfs:循环{
检测是否访问过:没有{
标记——dfs下一层——恢复标记
}
}

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

const int maxn = 20;
const int n=8;
int g[maxn][maxn];
bool col[maxn], l[maxn], fl[maxn];
int num = 1;
void print(){
	cout << "No. " << num++ << endl;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++)
			cout << g[j][i] << " ";
		cout << endl;
	}
}

void dfs(int x){
	if (x == n){
		print();
		return;
	}
	for (int y = 0; y < n; y++){
		if (!col[y]&&!l[x+y]&&!fl[y-x+n]){
			g[x][y] = 1;
			col[y] = true, l[x + y] = true, fl[y - x + n] = true;
			dfs(x + 1);
			g[x][y] = 0;
			col[y] = false, l[x + y] = false, fl[y - x + n] = false;
		}
	}
}

int main(){
	memset(g, 0, sizeof(g));
	dfs(0);
	return 0;
}

四、Butterfly

http://algorithm.openjudge.cn/2020hw1/2A/
思路:dfs遍历&判断每个点的类型,最后通过异或与relastion比较,看是否一致。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

const int maxn = 1010;
int n, m;
int visnum;
bool type[maxn];
bool vis[maxn];
bool ans;
int relation[maxn][maxn];
queue<int> q;

void init();
void judge();

int main(){
	while (scanf("%d%d", &n, &m) != EOF){
		init();
		int i;
		while (visnum!=n){
			//找第一只没访问过的蝴蝶
			for (i = 0; i < n; i++) if (!vis[i]) break;
			if (i == n) break;
			q.push(i); vis[i] = true; visnum++;
			while (!q.empty()){
				int now = q.front(); q.pop();
				//找所有与i有关系&没有访问过的蝴蝶
				for (int j = 0; j < n; j++){
					if (relation[now][j] == -1)
						continue;
					else if (relation[now][j] == 0&&!vis[j]){
						type[j] = type[now];
						q.push(j); vis[j] = true; visnum++;
					}
					else if (relation[now][j] == 1&&!vis[j]){
						type[j] = !type[now];
						q.push(j); vis[j] = true; visnum++;
					}
				}//for
			}//while
		}	
		judge();
	}
	return 0;
}
	

//初始化&输入
void init(){
	visnum = 0;
	memset(relation, -1, sizeof(relation));
	memset(type, true, sizeof(type));
	memset(vis, false, sizeof(vis));
	while (!q.empty())
		q.pop();
	for (int i = 0; i < m; i++){
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		relation[x][y] = relation[y][x] = z;
	}
}

//判断类型和关系是否一致
void judge(){
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (relation[i][j] != -1 && relation[i][j] != type[i] ^ type[j]){
				printf("NO\n");
				return;
			}
	printf("YES\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值