数独

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int count,map[10][10];
int a[100][2];


void  Input()
{
	char ch;
	count=0; 
	for(int i=1;i<=9;i++)
		for(int j=1;j<=9;j++)
		{
			cin>>ch;
			map[i][j] = ch-'0';
			if(map[i][j]==0)
			{
				a[count][0]=i;
				a[count++][1]=j;                
			}
		}
		
}


bool Isok(int x,int y,int value)
{
	for(int i=1;i<=9;i++)
	{
		if(map[x][i]==value )  return false;
		if(map[i][y]==value )  return false;
	}
	
	int xx = ((x-1)/3)*3;  //注意这里的计算
	int yy = ((y-1)/3)*3;
	
    for( i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			if(map[xx+i][yy+j]==value)
				return false; 
			return true;    
}







bool dfs(int cur)
{
	if(cur<0)
		return 1;
	int x,y;
	for(int i=1;i<=9;i++)
	{
		x = a[cur][0];
		y = a[cur][1];      
		
		if(Isok(x,y,i))
		{
            map[x][y]=i;
            if(dfs(cur-1))
				return 1;
			//dfs(cur-1);
			map[x][y]=0;                
		}
	} 
	
	return  0;
}

void Output()
{
    for(int i=1;i<=9;i++)
    {
		for(int j=1;j<=9;j++)
			cout<<map[i][j];
		cout<<endl;
    }
}

int main()
{
    int t;
    
	
	scanf("%d",&t);
	
	while(t--)
	{
	       
		Input();
		
        dfs(count-1);
        printf("*****\n");
		Output();          
	}   
    
	return 0;
}


/*

  
	1
	103000509
	002109400
	000704000
	300502006
	060000050
	700803004
	000401000
	009205800
	804000107
	
	  
		143628579
		572139468
		986754231
		391542786
		468917352
		725863914
		237481695
		619275843
		854396127
		
		  
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值