数独解法c++实现

转自http://blog.csdn.net/qq_31558353/article/details/50615760

用个小样列(据说是世界上最难的数独,10s过)

//<pre code_snippet_id="1592833" snippet_file_name="blog_20160301_1_4048211" name="code" class="cpp">#include <iostream>
#include<iostream>
using namespace std;

/* 构造完成标志 */
bool sign = false;

/* 创建数独矩阵 */
int num[9][9];

/* 函数声明 */
void Input();
void Output();
bool Check(int n, int key);
int DFS(int n);

/* 主函数 */
int main()
{
    cout << "请输入一个9*9的数独矩阵,空位以0表示:" << endl;
    Input();
    DFS(0);
    Output();
    system("pause");
}

/* 读入数独矩阵 */
void Input()
{
    char temp[9][9];
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            cin >> temp[i][j];
            num[i][j] = temp[i][j] - ‘0‘;
        }
    }
}

/* 输出数独矩阵 */
void Output()
{
    cout << endl;
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            cout << num[i][j] << " ";
            if (j % 3 == 2)
            {
                cout << "   ";
            }
        }
        cout << endl;
        if (i % 3 == 2)
        {
            cout << endl;
        }
    }
}

/* 判断key填入n时是否满足条件 */
bool Check(int n, int key)
{
    /* 判断n所在横列是否合法 */
    for (int i = 0; i < 9; i++)
    {
        /* j为n竖坐标 */
        int j = n / 9;
        if (num[j][i] == key) return false;
    }

    /* 判断n所在竖列是否合法 */
    for (int i = 0; i < 9; i++)
    {
        /* j为n横坐标 */
        int j = n % 9;
        if (num[i][j] == key) return false;
    }

    /* x为n所在的小九宫格左顶点竖坐标 */
    int x = n / 9 / 3 * 3;

    /* y为n所在的小九宫格左顶点横坐标 */
    int y = n % 9 / 3 * 3;

    /* 判断n所在的小九宫格是否合法 */
    for (int i = x; i < x + 3; i++)
    {
        for (int j = y; j < y + 3; j++)
        {
            if (num[i][j] == key) return false;
        }
    }

    /* 全部合法,返回正确 */
    return true;
}

/* 深搜构造数独 */
int DFS(int n)
{
    /* 所有的都符合,退出递归 */
    if (n > 80)
    {
        sign = true;
        return 0;
    }
    /* 当前位不为空时跳过 */
    if (num[n/9][n%9] != 0)
    {
        DFS(n+1);
    }
    else
    {
        /* 否则对当前位进行枚举测试 */
        for (int i = 1; i <= 9; i++)
        {
            /* 满足条件时填入数字 */
            if (Check(n, i) == true)
            {
                num[n/9][n%9] = i;
                /* 继续搜索 */
                DFS(n+1);
                /* 返回时如果构造成功,则直接退出 */
                if (sign == true) return 0;
                /* 如果构造不成功,还原当前位 */
                num[n/9][n%9] = 0;
            }
        }
    }
}

输入

感受一下输出

计算机是个伟大的发明

百度百科上的世界最难数独秒过,垃圾~~

时间: 2024-08-09 02:19:50

数独解法c++实现的相关文章

数独解法小探

数独的游戏要求在一个9X9的格子内填入1~9的数字,使得每一行,每一列,以及九个3X3的子区域内都没有重复的数字. 稍作思索,我写出了第一种解法.从事后查询维基百科1来看,这种方法可以称之为回溯法.思路很简单,依次扫描每一个待填数字的空格: 1. 在第一个空格里面填上“1”,检查这个数字是否合法(其所在的行.列,以及3X3的子区域里不存在重复的数字).如果合法,则前进到第二个格子.否则,在这个格子里继续试2,3,… ,直到合法为止. 2. 在第二个格子里面继续填数字,从“1”开始试起,直到找到一

数独解法

<pre name="code" class="cpp"> <pre name="code" class="cpp">//25...9.4. //4.71.3..6 //8.34.759. //3.8.7..69 //.1.3.24.. //5.49.6.83 //9.6.3.7.8 //.3.6.8.1. //1.2.9.6.4 struct node { node(int r,int c):row(

数独解法(C#)

未完成,回家继续 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithems { class Sudoku_Chain : IAlgorithm { class Blank { public long Possibilies = 0x111111111; public int X;

POJ 3074&amp;&amp;2676 数独DFS

经典数独问题 用DFS模拟数独解法,找摒除解和余数解 数独解法:http://www.sudokufans.org.cn/forums/topic/8/ 2676 #include "stdio.h" #include "string.h" struct node { int x,y; int s[10]; // 对于每个空格,数字i是否可用 int sum; // 对于每个空格,一共可以填入的数字种数 }order[101]; int cnt; // 总空格数 c

数独GUI程序项目实现

数独GUI程序项目实现 导语:最近玩上了数独这个游戏,但是找到的几个PC端数独游戏都有点老了...我就想自己做一个数独小游戏,也是一个不错的选择. 前期我在网上简单地查看了一些数独游戏的界面,代码.好好地了解了一下现在数独游戏的大概的框架.当然,我自己写的小游戏,也许没那么好.但是我一定会一点点升级这个小游戏的. 目前,我做的游戏是V1.0版本的,只能说实现了这个游戏的基本功能:可以进行数独游戏.可以更换背景色以及一些其他的基本功能.接下来,在空余时间,我会进行对其中一Studying功能的实现

leetcode037:Sudoku Solver

问题描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 问题分析 数

求解数独的所有解法,java编程实现

数独是一种考验眼力和逻辑的小游戏,关键在这个"独"字上,横竖不能重复,方块不能重复.今天我给大家介绍一种利用"循环+递归+回溯"的办法来用Java程序替我们完成数独. 先给代码随后讲解: 1 import java.util.HashMap; 2 import java.util.Map; 3 4 public class T2 { 5 public static final int N=3; 6 public static void main(String[] a

数独游戏求解:解法适用于任意阶数的数独

0.数独简介 数独(すうどく,Sūdoku)是一种运用纸.笔进行演算的逻辑游戏.以九阶数独为例,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 1)4阶(可填数字范围1~4,宫格2阶) 2)9阶(可填数字范围1~9,宫格3阶) 3)16阶(可填数字范围1~16,宫格4阶) *见附录 1.数独的表示 对于N阶数独可以用一个N*N的二维数组表示 1)数独阶数GridRank=N 2)宫格阶数SubGridRank=Sqrt

数独的C++解法

grid.h 1 #ifndef _GRID_H_ 2 #define _GRID_H_ 3 4 #include <set> 5 #include <cstddef> 6 7 class Grid { 8 public: 9 Grid() { for ( int i = 0; i < 9; i++ ) { value_s.insert(i + 1); } } 10 11 bool set( int value ) { 12 if ( !contain(value) ) {