数独问题

数独

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

         数独是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个3*3宫内的数字均含1-9,不重复。 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的。

       有一天hrdv碰到了一道号称是世界上最难的数独的题目,作为一名合格的程序员,哪能随随便便向困难低头,于是他决定编个程序来解决它。。


输入
第一行有一个数n(0< n <100),表示有n组测试数据,每组测试数据是由一个9*9的九宫格构成,0表示对应的格子为空
输出
输出一个9*9的九宫格,为这个数独的答案
样例输入
1
0 0 5 3 0 0 0 0 0
8 0 0 0 0 0 0 2 0
0 7 0 0 1 0 5 0 0
4 0 0 0 0 5 3 0 0
0 1 0 0 7 0 0 0 6
0 0 3 2 0 0 0 8 0
0 6 0 5 0 0 0 0 9
0 0 4 0 0 0 0 3 0
0 0 0 0 0 9 7 0 0
样例输出
1 4 5 3 2 7 6 9 8 
8 3 9 6 5 4 1 2 7 
6 7 2 9 1 8 5 4 3 
4 9 6 1 8 5 3 7 2 
2 1 8 4 7 3 9 5 6 
7 5 3 2 9 6 4 8 1 
3 6 7 5 4 2 8 1 9 
9 8 4 7 6 1 2 3 5 
5 2 1 8 3 9 7 6 4 

代码:

#include<stdio.h>
#include<string.h>
int flag;
int arr[9][9];
int check(int number,int x,int y)
{
	int i,j;
	for(i = 0;i < 9;i++)//判断列 
	{		
		if(number == arr[i][y])
		return 0;
	}
	for(j = 0;j < 9;j++)//判断行 
	{
		if(number == arr[x][j])
		return 0;
	}
	int a=x / 3 * 3,b=y / 3 * 3;
	for(i = a; i < a + 3; i++)//判断3*3的格子 
	{
		for(j = b; j < b + 3; j++)
		{
			if(arr[i][j] == number)
			return 0;
		}
	}
	return 1;
}
void dfs(int x,int y)
{
	if(flag == 1)//退出递归出口
	return ;
	if(x == 9 && y == 0)
	{
		int i,j;
		for(i =0 ;i < 9; i++)
		{
			for(j = 0;j < 9; j++)
			{
				printf("%d ",arr[i][j]);
			}
			printf("\n");
		}
		flag = 1;
		return ;
	}
	if(y == 9)
	dfs(x + 1,0);
	if(arr[x][y]!=0)
	dfs(x, y + 1);
	if(arr[x][y]==0) 
	{
		int i;
		for(i = 1; i <= 9; i++)
		{
			if(check(i, x, y))
			{
				arr[x][y] = i;
				dfs(x, y + 1);
				arr[x][y]=0;
		    }
		}
	}
}
int main()
{
	int i, j, n;
	scanf("%d",&n);
	while(n--)
	{
		for(i = 0; i < 9; i++)
	 	{
	    	for(j = 0; j < 9; j ++)
	    	{
	    		scanf("%d",&arr[i][j]);
			}
		}
		dfs(0, 0);
		memset(arr, 0, sizeof(arr));
		flag = 0;
    }
    return 0;
}


先贴上自己认为十分好理解的代码 就是一行一行的暴力的尝试    还是老套路  递归+回溯  

贴下学长们的代码: 

01.
#include <stdio.h>
02.
#include <string.h>
03.
 
04.
int mp[10][10];
05.
int hashx[10][10];
06.
int hashy[10][10];
07.
int hashxy[25][10];
08.
typedef struct node
09.
{
10.
int x,y;
11.
}node;
12.
 
13.
node np[100];
14.
int cn,xx;
15.
 
16.
void dfs(int n)
17.
{
18.
int i;
19.
if(n == cn)
20.
{
21.
xx = 1;
22.
return ;
23.
}
24.
 
25.
for(i = 1; xx == 0 && i <= 9; i++)
26.
{
27.
if(hashx[np[n].x][i] == 0 && hashy[np[n].y][i] == 0 && hashxy[np[n].x / 3 * 10 + np[n].y / 3][i] == 0)
28.
{
29.
mp[np[n].x][np[n].y] = i;
30.
hashx[np[n].x][i] = 1; hashy[np[n].y][i] = 1 ; hashxy[np[n].x / 3 * 10 + np[n].y / 3][i] = 1;
31.
dfs(n + 1);
32.
hashx[np[n].x][i] = 0; hashy[np[n].y][i] = 0 ; hashxy[np[n].x / 3 * 10 + np[n].y / 3][i] = 0;
33.
}
34.
}
35.
}
36.
int main()
37.
{
38.
int loop;
39.
 
40.
scanf("%d",&loop);
41.
while(loop --)
42.
{
43.
int i,j,y;
44.
 
45.
cn = xx = 0;
46.
memset(hashx,0,sizeof(hashx));
47.
memset(hashy,0,sizeof(hashy));
48.
memset(hashxy,0,sizeof(hashxy));
49.
 
50.
for(i = 0; i < 9; i++)
51.
{
52.
for(j = 0; j < 9; j++)
53.
{
54.
scanf("%d",&y);
55.
mp[i][j] = y;
56.
if(y == 0)
57.
{
58.
np[cn].x = i;
59.
np[cn].y = j;
60.
cn ++;
61.
}
62.
else
63.
{
64.
hashx[i][y] = 1;
65.
hashy[j][y] = 1;
66.
hashxy[i / 3 * 10 + j / 3][y] = 1;
67.
}
68.
}
69.
 
70.
}
71.
dfs(0);
72.
for(i = 0; i < 9; i ++)
73.
{
74.
for(j = 0; j < 9; j ++)
75.
printf("%d ",mp[i][j]);
76.
printf("\n");
77.
}
78.
}
79.
return 0;
80.
}
还有一种学长的代码:

#include <stdio.h> 
#include <string.h> 
int map[9][9]; 
bool row[9][10]; 
bool col[9][10]; 
bool block[3][3][10]; 
bool flag;
void DFS(int x)
{
	if(flag)
		return;
	int r = x / 9;
	int c = x % 9;
	int i, j;
	if(81 == x)
	{
		for(i = 0; i < 9; i++)
		{
			for(j = 0; j < 9; j++)
				printf("%d ", map[i][j]);
			printf("\n");
		}
		flag = 1;
		return;
	}
	if(0 == map[r][c])
	{
		for(i = 1; i <= 9; i++)
		{
			if(!(row[r][i] || col[c][i] || block[r / 3][c / 3][i]))
			{
				map[r][c] = i;
				row[r][i] = col[c][i] = block[r / 3][c / 3][i] = 1;
				DFS(x + 1);
				row[r][i] = col[c][i] = block[r / 3][c / 3][i] = 0;
				map[r][c] = 0;
			}
		}
	}
	else
		DFS(x + 1);
}
int main()
{
	int i, j, n;
	scanf("%d", &n);
	while(n--)
	{
		flag = 0;
		memset(row, 0, sizeof(row));
		memset(col, 0, sizeof(col));
		memset(block, 0, sizeof(block));
		for(i = 0; i < 9; i++)
		{
			for(j = 0; j < 9; j++)
			{
				scanf("%d", &map[i][j]);
				if(map[i][j])
				{
					row[i][map[i][j]] = 1;
					col[j][map[i][j]] = 1;
					block[i/3][j/3][map[i][j]] = 1;
				}
			}
		}
		DFS(0);
	}
	return 0;
}

ps:  有种代码叫做学长的代码 或者说是别人的代码!!!!!!

需要解释下那个三维数组   他具体表现的是第几个小数独块的第几行第几列  不是那些什么  一维点 二维线 三维空间。。还四维空间呢。。。







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值