POJ 2676 Sudoku (数独 DFS)

简介: Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14368 Accepted: 7102 Special Judge DescriptionSudoku is a very simple task.
 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14368   Accepted: 7102   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

 
http://bailian.openjudge.cn/practice/2982/
 
题目大意:就是数独咯,让你求解数独游戏,9乘9的矩阵要求每行每列和9个3*3的子矩阵内都出现数字1-9
 
题目分析:9乘9的矩阵,从第一个位置一直搜到最后一个位置,若当前位置有数字搜下一位置,否则枚举1-9并判断,
判断时当前行r = n/9当前列为c = n%9当前子矩阵的第一个元素位置为r / 3 * 3,c / 3 * 3
 1 #include <cstdio>  
 2 char s[10];  
 3 int num[9][9];  
 4 bool flag;  
 5   
 6 bool ok(int n, int cur)   
 7 {  
 8     int r = n / 9;      //当前行  
 9     int c = n % 9;      //当前列  
10     for(int j = 0; j < 9; j++)  //枚举列  
11         if (num[r][j] == cur)   
12             return false;  
13     for(int i = 0; i < 9; i++)  //枚举行  
14         if (num[i][c] == cur)   
15             return false;  
16     //得到当前所在的子矩阵的第一个元素位置  
17     int x = r / 3 * 3;    
18     int y = c / 3 * 3;  
19     //枚举子矩阵中的元素  
20     for(int i = x; i < x + 3; i++)  
21         for(int j = y; j < y + 3; j++)  
22             if (num[i][j] == cur)   
23                 return false;  
24     return true;  
25 }  
26   
27 void DFS(int n)  
28 {  
29     if(n > 80 || flag)   
30     {  
31         flag = true;  
32         return;  
33     }  
34     if(num[n / 9][n % 9])//当前位置有数字直接搜索下一位  
35     {  
36         DFS(n + 1);  
37         if(flag)  
38             return;  
39     }  
40     else  
41     {  
42         for(int cur = 1; cur <= 9; cur++)  //枚举数字  
43         {  
44             if(ok(n, cur)) //若ok则插入  
45             {  
46                 num[n / 9][n % 9] = cur;   
47                 DFS(n + 1);  
48                 if(flag)   
49                     return;  
50                 num[n / 9][n % 9] = 0; //还原  
51             }  
52         }  
53     }  
54 }  
55   
56 int main()  
57 {     
58     int T;  
59     scanf("%d", &T);  
60     while(T--)  
61     {  
62         flag = false;  
63         for(int i = 0; i < 9; i++) //得到数独矩阵  
64         {  
65             scanf("%s", s);  
66             for(int j = 0; j < 9; j++)  
67                 num[i][j] = (s[j] - '0');  
68         }  
69         DFS(0);  //从第一位开始搜  
70         for(int i = 0; i < 9; i++)  
71         {  
72             for(int j = 0; j < 9; j++)  
73                 printf("%d", num[i][j]);  
74             printf("\n");        
75         }  
76     }  
77 }  
View Code

题解来源:http://blog.csdn.net/tc_to_top/article/details/43699047 

 

相关文章
|
10天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
9天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
408 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
3天前
|
存储 安全 前端开发
如何将加密和解密函数应用到实际项目中?
如何将加密和解密函数应用到实际项目中?
197 138
|
9天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
377 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
3天前
|
存储 JSON 安全
加密和解密函数的具体实现代码
加密和解密函数的具体实现代码
197 136
|
21天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1353 8
|
8天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
20天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1460 87