poj2676(数独 sudoku)

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11943 Accepted: 5952 Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

Southeastern Europe 2005

#include<stdio.h>  
#include<string.h>
int sudo[10][10],row[10][10],column[10][10],co[81][2];
int judge(int x,int y,int d)//在宫中的判断一个数是否可填
{
	int i,j;
	for(i=x/3*3;i<x/3*3+3;i++)
	{
		for(j=y/3*3;j<y/3*3+3;j++)
			if(sudo[i][j]==d)
				return 0;
	}
	return 1;
}
int dfs(int num)
{
	int i,j,k;
	if(num<0)  return 1;
	j=co[num][0];k=co[num][1];
	for(i=0;i<9;i++)
	{
		if(judge(j,k,i+1)&&!row[j][i]&&!column[k][i])
		{
			row[j][i]=column[k][i]=1;
			sudo[j][k]=i+1;
			if(dfs(num-1)) return 1;//若返回失败即此数不行
			row[j][i]=column[k][i]=0;
			sudo[j][k]=0;
		}
	}
	return 0;
}
main()
{
	int i,j,n,num;
	char str[11];
	//freopen("D:\\o.txt","r",stdin);
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		num=0;
		memset(row,0,sizeof(row));
		memset(column,0,sizeof(column));
		for(i=0;i<9;i++)
		{
			gets(str);
			for(j=0;j<9;j++)
			{
				sudo[i][j]=str[j]-'0';
				if(!sudo[i][j])
				{
					co[num][0]=i;
					co[num++][1]=j;
				}
				if(sudo[i][j])  row[i][sudo[i][j]-1]=1;//行的标志
			}
		}
		for(j=0;j<9;j++)
		{
			for(i=0;i<9;i++)
			{
				if(sudo[i][j])  column[j][sudo[i][j]-1]=1;//列的标志
			}
		}
		dfs(num-1);
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
				printf("%d",sudo[i][j]);
			printf("\n");
		}
	}
	return 0;
}


7

100830002
570001000
000509064
704008590
003010400
051400306
360704000
000600079
800052003

030007004
602041000
050030967
040003006
087000350
900700020
718020040
000160809
400500030

103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

085000210
094012003
000300704
503409000
040206030
000103907
608005000
100840360
027000890

080001600
070400021
500396000
204050130
008907500
057030900
000563009
310002050
005800040

012680090
600004010
805200370
000007523
000406000
381900000
054002801
070300002
030059760

900020401
005090020
040100097
500001000
000000002
400570309
053002180
000000000
002800003


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值