Sudoku(DFS)

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

题目就是一个数独游戏,实用性很强,完成这道题目,数独游戏还不是so easy!!

代码:

#include<stdio.h>
#include<string.h>
using namespace std;
void Dfs(int x,int y);
void Getsuduku();
int suduku[9][10];  //存放数独
int H[9][10];       //标记是否在第 i行出现过 
int L[9][10];       //标记是否在第 i 列出现过
int S[9][10];       //标记是否在第 i个小方块出现过
int isdone;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(suduku,0,sizeof(suduku));
        memset(H,0,sizeof(H));
        memset(L,0,sizeof(L));
        memset(S,0,sizeof(S));
        Getsuduku();
        isdone=0;
        Dfs(0,0);
    }
return 0;
}
void Getsuduku(){
    int i,j;
    for(i=0;i<9;i++){
        char temp[10];
        scanf("%s",temp);
        for(j=0;j<9;j++){
            suduku[i][j]=temp[j]-'0'; //转化为数字
            if(suduku[i][j]){
                int k=i/3*3+j/3;  //求出此时在第几个小方块里
                H[i][suduku[i][j]]=1;
                L[j][suduku[i][j]]=1;
                S[k][suduku[i][j]]=1;
            }
        }
    }
}
void Dfs(int x,int y){  //遍历每一点
    if(x==9){
        isdone=1;
        int i,j;
        for(i=0;i<9;i++){
            for(j=0;j<9;j++)
                printf("%d",suduku[i][j]);
            printf("\n");
        }
    }
    if(isdone) return ;
    if(suduku[x][y]){
        if(y==8) Dfs(x+1,0);
        else Dfs(x,y+1);
    }
    else{
        int num;
        for(num=1;num<=9;num++){    //从 1 到 9 枚举数字
                int k=x/3*3+y/3;
            if(!L[y][num]&&!H[x][num]&&!S[k][num]){
                suduku[x][y]=num;
                L[y][num]=1;
                H[x][num]=1;
                S[k][num]=1;
                if(y==8) Dfs(x+1,0);
                else Dfs(x,y+1);
                suduku[x][y]=0;
                L[y][num]=0;
                H[x][num]=0;
                S[k][num]=0;
            }
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用MATLAB的深度优先搜索算法来实现七宫格不规则数独。具体步骤如下: ```MATLAB % 1. 定义数独的初始状态 sudoku = [0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0]; % 2. 定义数独的规则 % 七宫格不规则数独的规则可以参考引用[2]中的数字矩阵 % 将数字矩阵转换为一个包含81个元素的向量 rule = [1 1 1 2 2 2 3 3 3 ... 1 1 1 2 2 2 3 3 3 ... 1 1 1 2 2 2 3 3 3 ... 4 4 4 5 5 5 6 6 6 ... 4 4 4 5 5 5 6 6 6 ... 4 4 4 5 5 5 6 6 6 ... 7 7 7 8 8 8 9 9 9 ... 7 7 7 8 8 8 9 9 9 ... 7 7 7 8 8 8 9 9 9]; % 3. 定义深度优先搜索算法 function [sudoku, success] = dfs(sudoku, rule, index) % 如果已经填满了数独,则返回成功 if index > 81 success = true; return; end % 计算当前位置的行和列 row = ceil(index / 9); col = mod(index - 1,9) + 1; % 如果当前位置已经有数字,则跳过 if sudoku(row, col) ~= 0 [sudoku, success] = dfs(sudoku, rule, index + 1); return; end % 尝试填入数字 for num = 1:9 % 检查当前数字是否符合规则 if check(sudoku, rule, row, col, num) sudoku(row, col) = num; % 递归搜索下一个位置 [sudoku, success] = dfs(sudoku, rule, index + 1); if success return; end sudoku(row, col) = 0; end end % 如果所有数字都尝试过了,仍然无法得到解,则返回失败 success = false; end % 4. 定义检查函数 function valid = check(sudoku, rule, row, col, num) % 检查行和列是否符合规则 if any(sudoku(row, :) == num) || any(sudoku(:, col) == num) valid = false; return; end % 检查七宫格是否符合规则 region = rule((row - 1) * 9 + col); [r, c] = find(rule == region); if any(sudoku(r, c) == num) valid = false; return; end valid = true; end % 5. 调用深度优先搜索算法求解数独 [sudoku, success] = dfs(sudoku, rule, 1); % 6. 输出结果 if success disp(sudoku); else disp('No solution found.'); end ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值