大佬们可以看一下吗?
  • 板块P1784 数独
  • 楼主grass_dream
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/8/23 09:53
  • 上次更新2023/10/27 14:04:27
查看原帖
大佬们可以看一下吗?
694866
grass_dream楼主2022/8/23 09:53
#include<iostream>
using namespace std;
int a[100][100],h[100][100],l[100][100],used[100][100];
void dfs(long long x,long long y)
{
	if(a[x][y]!=0)
	{
		if(x==9&&y==9)
		{
			for(int i=1;i<=9;++i)
			{
				for(int j=1;j<=9;++j)
				{
					cout<<a[i][j]<<" ";
				}
				cout<<endl;
			}
			exit(0);
		}
		else if(y==9)
		{
			dfs(x+1,1);
		}
		else
		{
			dfs(x,y+1);
		}
		
	}
	else
	{
		int liong=0,zh[10];
		for(int i=1;i<=9;++i)
		{
			bool v=false;
			for(int t=1;t<=9;++t)
			{
				if(a[t][y]==i)
				{
					v=true;
				}
			}
			for(int t=1;t<=9;++t)
			{
				if(a[x][t]==i)
				{
					v=true;
				}
			}
			int cx=0,cy=0;
			if(x%3==0)
			{
				cx=x-2;
			}
			else
			{
				cx=x-x%3+1;
			}
			if(y%3==0)
			{
				cy=y-2;
			}
			else
			{
				cy=y-y%3+1;
			}
			for(int ii=cx;ii<=cx+2;++ii)
			{
				for(int j=cy;j<=cy+2;++j)
				{
					if(a[ii][j]==i)
					{
						v=true;
					}
				}
			}
			if(v==true)
			{
				continue;
			}
			a[x][y]=i;
			if(x==9&&y==9)
			{
				for(int ii=1;ii<=9;++ii)
				{
					for(int j=1;j<=9;++j)
					{
						cout<<a[ii][j]<<" ";
					}
					cout<<endl;
				}
				exit(0);
			}
			else if(y==9)
			{
				dfs(x+1,1);
			}
			else
			{
				dfs(x,y+1);
			}
			a[x][y]=0;
		}
	}
}
int main()
{
	for(int i=1;i<=9;++i)
	{
		for(int j=1;j<=9;++j)
		{
			cin>>a[i][j];
		}
	}
	dfs(1,1);
	return 0;
}
2022/8/23 09:53
加载中...