求助
  • 板块P1784 数独
  • 楼主juruojjl_
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/7/22 15:37
  • 上次更新2023/11/6 22:35:41
查看原帖
求助
164836
juruojjl_楼主2020/7/22 15:37
#include<bits/stdc++.h>
using namespace std;
int g[10][10]={
{0,1,1,1,2,2,2,3,3,3},
{0,1,1,1,2,2,2,3,3,3},
{0,1,1,1,2,2,2,3,3,3},
{0,4,4,4,5,5,5,6,6,6},
{0,4,4,4,5,5,5,6,6,6},
{0,4,4,4,5,5,5,6,6,6},
{0,7,7,7,8,8,8,9,9,9},
{0,7,7,7,8,8,8,9,9,9},
{0,7,7,7,8,8,8,9,9,9}};
int row[10][10],col[10][10],small[10][10],maze[10][10];
bool flag=0;
void dfs(int x,int y)
{
	if(flag) return;
	if(y==10) x+=1,y=1;
	if(x==10)
	{
		flag=true;
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=9;j++) cout<<maze[i][j]<<" ";
			cout<<endl;
		}
		return;
	}
	if(maze[x][y]) dfs(x,y+1);
	else
	{
		for(int k=1;k<=9;k++)
		{
			if(row[x][k]==0&&col[y][k]==0&&small[g[x][y]][k]==0)
			{
				maze[x][y]=k;
				row[x][k]=1;
				col[y][k]=1;
				small[g[x][y]][k]=1;
				dfs(x,y+1);
				maze[x][y]=0;
				row[x][k]=0;
				col[y][k]=0;
				small[g[x][y]][k]=0;
			}
		}
	}
	return;
}
int main()
{
	for(int i=1;i<=9;i++)
		for(int j=1;j<=9;j++)
		{
			int t;
			cin>>t;
			maze[i][j]=t;
			if(t)
			{
				row[i][t]=1;
				col[j][t]=1;
				small[g[i][j]][t]=1;
			}
		}
	dfs(1,1);
	return 0;
}

为什么样例过不了QAQ

2020/7/22 15:37
加载中...