Almost Fault-Tolerant Database(暴力dfs)

Almost Fault-Tolerant Database
题意:求一个数组与给出的所有数组里相同位置下最多2个不同 若不能 输出NO
思路:以第一个数组为模板,改第一个数组,最多改2个元素,暴力dfs ,遇到相差>=5的数组一定不行。

#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=2e18+100;
const int maxn=3e5+100;
vector<int>g[maxn];
bool fg;
int n,m;
bool dfs(int x)//x为已经修改的个数
{
	if(x>2)return 0;
	for(int i=1;i<n;i++)
	{
		vector<int>tmp;
		for(int j=0;j<m;j++)
		{
			if(g[0][j]!=g[i][j])tmp.push_back(j);
		}
		int cnt=tmp.size();
		if(cnt<=2)continue;
		else if(cnt>=5){return 0;}
		else 
		{
			for(auto it:tmp)
			{
				int tp=g[0][it];
				g[0][it]=g[i][it];
				if(dfs(x+1))return 1;
				g[0][it]=tp;
			}
			return 0;
		}
	}
	return 1;
	
}
signed main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int x;
			cin>>x;
			g[i].push_back(x);
		}
	}
	fg=dfs(0);
	if(fg)
	{
		cout<<"Yes"<<"\n";
		for(auto it:g[0])cout<<it<<" ";
		cout<<"\n";
	}
	else 
	{
		cout<<"No"<<"\n";
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thusloop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值