hdu1246 Sudoku Killer 数独

 

#include<iostream>
#include<string>
using namespace std;

struct node
{
	int x,y;
};
int map[10][10];
node *Q;
int n;
bool isok;
void output()
{
	int i,j;
	for(i=1;i<10;i++)
	{
		for(j=1;j<10;j++)
		{
			if(j>1)printf(" ");
			printf("%d",map[i][j]);
		}
		printf("\n");
	}
}
bool unfind(int x,int y,int k)
{
	int i,j;
	for(i=1;i<10;i++)
	{
		if(map[x][i]==k||map[i][y]==k)return false;
	}
	int tx=(x-1)/3*3+1;
	int ty=(y-1)/3*3+1;
	for(i=tx;i<tx+3;i++)
		for(j=ty;j<ty+3;j++)
			if(map[i][j]==k)return false;
			
			return true;
}
void dfs(int cnt)
{
	if(isok)return;
	int i;
	if(cnt==n)
	{
		isok=true;
		output();
	}
	else
	{
		int tx=Q[cnt].x;
		int ty=Q[cnt].y;
		for(i=1;i<10;i++)
		{
			if(unfind(tx,ty,i))
			{
				map[tx][ty]=i;
				dfs(cnt+1);
				map[tx][ty]=0;
			}
		}
	}
}


int main()
{
	char ch[2];
	int a=0;
	while(scanf("%s",ch)!=EOF)//cin>>ch
	{
		
		Q=new node[81];
		n=0;
		if(ch[0]!='?')
			map[1][1]=ch[0]-'0';
		else
		{
			map[1][1]=0;
			Q[n].x=1;
			Q[n++].y=1;
		}
		int i,j;
		for(i=1;i<10;i++)
		{
			for(j=1;j<10;j++)
			{
				if(i!=1||j!=1)
				{scanf("%s",ch);
				//cin>>ch;
				if(ch[0]!='?')
				{
					int t=ch[0]-'0';
					map[i][j]=t;
				}
				else
				{
					map[i][j]=0;
					Q[n].x=i;
					Q[n++].y=j;
				}
				} 
			}
		}
		if(a)printf("\n");a++;
		isok=false;
		dfs(0);
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值