NYOJ722

18 篇文章 0 订阅


数独

时间限制: 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 
来源

OJ平台最优代码

 
#include <stdio.h>
#include <string.h>

int mp[10][10];
int hashx[10][10];
int hashy[10][10];
int hashxy[25][10];
typedef struct node
{
	int x,y;
}node;

node np[100];
int cn,xx;

void dfs(int n)
{
	int i;
	if(n == cn)
	{
		xx = 1;
		return ;
	}

	for(i = 1; xx == 0 && i <= 9; i++)
	{
		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)
		{
			mp[np[n].x][np[n].y] = i;
			hashx[np[n].x][i] = 1; hashy[np[n].y][i] = 1 ; hashxy[np[n].x / 3 * 10 + np[n].y / 3][i] = 1;
			dfs(n + 1);
			hashx[np[n].x][i] = 0; hashy[np[n].y][i] = 0 ; hashxy[np[n].x / 3 * 10 + np[n].y / 3][i] = 0;
		}
	}
}
int main()
{
	int loop;

	scanf("%d",&loop);
	while(loop --)
	{
		int i,j,y;

		cn = xx = 0;
		memset(hashx,0,sizeof(hashx));
		memset(hashy,0,sizeof(hashy));
		memset(hashxy,0,sizeof(hashxy));

		for(i = 0; i < 9; i++)
		{
			for(j = 0; j < 9; j++)
			{
				scanf("%d",&y);
				mp[i][j] = y;
				if(y == 0)
				{
					np[cn].x = i;
					np[cn].y = j;
					cn ++;
				}
				else
				{
					hashx[i][y] = 1;
					hashy[j][y] = 1;
					hashxy[i / 3 * 10 + j / 3][y] = 1;
				}
			}

		}
		dfs(0);
		for(i = 0; i < 9; i ++)
		{
			for(j = 0; j < 9; j ++)
				printf("%d ",mp[i][j]);
			printf("\n");
		}
	}
	return 0;
}        

另一种:

#include <iostream>  
#include <cstring>  
   
using namespace std;  
   
int sudoku[9][9];  
   
//判断填在空白位置的数字在行、列上是否符合要求  
bool Judge1(int x, int y, int n)  
{  
    int i;  
   
    for(i=0;i<9;i++)  
    {  
        //判断 列  
        if((sudoku[i][y]==n) && (i!=x))  
            return false;  
        //判断 行  
        if((sudoku[x][i]==n) && (i!=y))  
            return false;  
    }  
   
    return true;  
}  
   
//判断填在空白位置的数字在九宫格之内是否符合要求  
bool Judge2(int x, int y, int n)  
{  
    int xx,yy,i,j;  
    xx=x/3;  
    yy=y/3;  
    for(i=xx*3;i<xx*3+3;i++)  
        for(j=yy*3;j<yy*3+3;j++)  
            if(sudoku[i][j]==n)  
                if(i==x && j==y)  
                    continue;  
                else  
                    return false;  
    return true;  
}  
   
//填充空白数组  
bool Fill(int m)  
{  
    int n,x,y;  
    x=m/9;  
    y=m%9;  
    if (m>=81)  
        return true;  
    if (sudoku[x][y]==0)  
    {  
        for(n=1;n<=9;n++)  
        {  
            sudoku[x][y]=n;  
            if(Judge1(x,y,n)&&Judge2(x,y,n))  
                if(Fill(m+1))  
                    return true;  
            sudoku[x][y]=0;  
   
        }  
    }  
    else  
        return Fill(m+1);  
   
    return false;  
}  
   
   
int main()  
{  
    int n,i,j,k;  
    cin>>n;  
    while(n--)  
    {  
        //memset(sudoku,0,sizeof(sudoku));  
        for(i=0;i<9;i++)  
            for(j=0;j<9;j++)  
                cin>>sudoku[i][j];  
        if(Fill(0))  
        {  
            for(i=0;i<9;i++)  
            {  
                for(j=0;j<9;j++)  
                    cout << sudoku[i][j] << " ";  
                cout << endl;  
            }  
        }  
    }  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值