北大ACM poj2676 Sudoku

程序设计与算法(二)期末考试之06:Sudoku 描述 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 cel 阅读详情
Sudoku

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

用深度优先搜索,可能因为数据原因倒着搜索更快。

 

#include <stdio.h>
/* 构造完成标志 */
int sign;   //0代表false,1代表true 
/* 创建数独矩阵 */
int num[9][9];
/* 函数声明 */
void Input();
void Output();
int Check(int n, int key);//返回0代表false,1代表true 
int DFS(int n);
 
/* 主函数 */
main()
{	
    int test,i,j;
//	freopen("in.txt","r",stdin);
    scanf("%d",&test);
    while(test--)
    {
		sign=0;//设初始值为0 
	    Input();//输入 
	    DFS(0);  
	    Output();//输出   	
    }
}
 
/* 读入数独矩阵 */
void Input()
{
    int i,j;
    for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
        	scanf("%1d",&num[i][j]);//一个数一个数的读 
}
 
/* 输出数独矩阵 */
void Output()
{
    int i,j;
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            printf("%d",num[i][j]);
        }
        printf("\n");
    }
}
 
/* 判断key填入n时是否满足条件 */
int Check(int n, int key)
{
    /* 判断n所在横列是否合法 */
    int i,j;
    int x,y;
    for (j = 0; j < 9; j++) 
    {
        /* i为n竖坐标 */
        i = n / 9;
		//如果有相同的数,返回0(不合法) 
        if (num[i][j] == key) return 0;
    }
 
    /* 判断n所在竖列是否合法 */
    for (i = 0; i < 9; i++)
    {
        /* j为n横坐标 */
        j = n % 9;
		//如果有相同的数,返回0(不合法) 
        if (num[i][j] == key) return 0;
    }
 
    /* x为n所在的小九宫格左顶点竖坐标 */
    x = n / 9 / 3 * 3;//如:38/9/3*3=3 
 
    /* y为n所在的小九宫格左顶点横坐标 */
    y = n % 9 / 3 * 3;//如:38%9/3*3=6 
 
    /* 判断n所在的小九宫格是否合法 */
    for (i = x; i < x + 3; i++)
    {
        for (j = y; j < y + 3; j++)
        {
            if (num[i][j] == key) return 0;
        }
    }
 
    /* 全部合法,返回正确 */
    return 1;
}
 
/* 深搜构造数独 */
int DFS(int n)
{
    /* 所有的都符合,退出递归 */
    int i;
    if (n > 80)
    {
        sign = 1;
        return 0;
    }
    /* 当前位不为空时跳过 */
    if (num[n/9][n%9] != 0)
    {
        DFS(n+1);
    }
    else
    {
        /* 否则对当前位进行枚举测试 */
        for (i = 1; i <= 9; i++)
        {
            /* 满足条件时填入数字 */
            if (Check(n, i) == 1)
            {
                num[n/9][n%9] = i;
                /* 继续搜索 */
                DFS(n+1);
                /* 返回时如果构造成功,则直接退出 */
                if (sign == 1) return 0;
                /* 如果构造不成功,还原当前位 */
                num[n/9][n%9] = 0;
            }
        }
    }
}


 

POJ - 2676:Sudoku POJ-2676:Sudoku 来源:POJ 标签:图论-&amp;amp;gt;深度优先搜索 参考资料: 相似题目: 题目 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 t... 阅读详情

相关推荐

POJ2676-Sudoku

北大POJ2676-Sudoku 解题报告+AC代码

ACM篇:POJ 2676 -- Sudoku

简单粗暴的深搜。

Ace_is_my_name的博客 501

基于R语言的DICE模型实践技术应用;预测不同减排路径下的经济增长、碳排放量、气候变化程度以及社会福利变化

DICE模型是一个动态综合气候经济模型,由诺贝尔经济学奖得主William Nordhaus开发,用于评估气候变化对经济的影响以及不同减排政策的经济成本和效益。以下是一个关于DICE模型在气候变化影响评估中的实际应用案例,结合R语言代码进行讲解。

KY_chenzhao的博客 870

acm之旅之数独问题

文章目录Sudoku Sudoku 题目链接:Sudoku 参考博文:数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝) 题目大意: 给你一个数独,让你填数: 1.每行的九个数字互不相同; 2.每列的九个数字各不相同; 3.被分成的3*3的小矩阵中的九个数字互不相同; 输出完成后的数表,若不能满足上述条件,则输出原图。 思路:这道题大致思路比较好想,但是一直想不到优雅的实现方式,看了...

Accept1234的博客 549

图片边界矢量化-地理配准

无地理和投影信息的JPG图片,通过ArcGIS进行地理配准(Georeferencing)完成数字化边界。

u012727861的博客 1913

关于发那科机器人抓手及物料搬运阀配置

4:确保抓手阀岛和输入模块配网连接正常,MENU—SETUP—MH Valves—Details,详细界面进入到左上图所示,根据每组阀分配夹钳数量并应用。3:设置搬运有效,抓手是否带点焊可以自行选择,要设置应用选择,物料搬用工具数量,工具上阀岛的数量,夹钳存在数量,工具存在数量,以及真空吸盘,冷启动。​2:MENU—应用选择,将搬运置为有效,剩余其他项根据机器人实际用途进行选择。5:设置夹钳数量,检查打开夹钳,检查闭合夹钳,延迟时间要给选择配置应用。

dxs93的博客 4783

ACM Sudoku

Sudoku是一个非常简单的任务。 具有9行9列的方形表被划分为9个较小的正方形3x3,如图所示。 在一些单元格中写入从1到9的十进制数字。其他单元格为空。 目标是填充空单元格,其中十进制数字从1到9,每个单元格一个数字,使得每行中每列和每个标记的3x3子方格中的所有数字将显示为1到9。 编写程序来解决给定的数独任务。 Input 输入数据将以测试用例的数量开始。 对于每个测试用例,跟随9...

weixin_30675247的博客 168

Pytorch中CrossEntropyLoss()详解

交叉熵主要是用来判定实际的输出与期望的输出的接近程度。举个例子:在做分类训练的时候,如果一个样本属于第 K 类,那么这个类别所对应的输出节点的输出值应该为1,而其他节点的输出都为0,即 [0,0,1,0,….0,0] ,这个数组也就是样本的 Label,是神经网络最期望的输出结果。我们用交叉熵损失函数来衡量网络的实际输出与正确标签的差异,利用这种差异经过反向传播去更新网络参数。交叉熵:它主要刻画的是实际输出(概率)与期望输出(概率)的距离,也就是交叉熵的值越小,两个概率分布就越接近。

flyingluohaipeng的博客 3865

POJ 2676 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 othe...

点点滴滴 362

HFSS仿真中如何提取最大E/H场强与频率关系曲线

本文详细讲解了在HFSS仿真中提取最大E/H场强与频率关系曲线的完整流程。通过使用场计算器(Field Calculator)和创建场报告,工程师可以准确获得器件在整个频带内的电场或磁场强度峰值,并进一步推导出关键的功率容量曲线,为高功率射频器件(如滤波器、耦合器)的设计提供至关重要的安全评估依据。

ss78901的博客 1100

药物对接仿真软件:HADDOCK_(5).HADDOCK中蛋白质-配体对接流程

半柔性能量最小化阶段是在刚体能量最小化的基础上,允许配体的部分柔性,通过优化配体的构象来进一步提高对接的准确性。半柔性能量最小化阶段是在刚体能量最小化的基础上,允许配体的部分柔性,通过优化配体的构象来进一步提高对接的准确性。如果是PDB格式,确保文件包含所有必要的原子信息。对接完成后,HADDOCK会生成一系列的输出文件,包括对接后的蛋白质-配体复合物结构文件、能量评分文件和统计分析文件。对接完成后,HADDOCK会生成一系列的输出文件,包括对接后的蛋白质-配体复合物结构文件、能量评分文件和统计分析文件。

kkchenjj的博客 1091

[Java] POJ 2676

一、思路 dfs进行试探,如果失败就进行回溯,若找到一组解就进行返回最后输出。 进行剪枝: 利用3个数组进行判断在某行某列某方格中 这个数到底被标记没有,如果别标记那么就不能选这个数。 特别注意的是如何对3*3的小方格里面的1-9进行标记 首先我们将大方格分成9个3*3的小方格 i,j代表在大方格中的位置,下标从0到8, 我们让a=i/3, b = j /3 可以从图中得出 K = ...

Tizzi的博客 1134

网格质量评价

CFD中评价网格质量的方法

B_etter_Man的博客 1万+

题目一览

CSUST_搜索专题: Problem A POJ 2488 A Knight's Journey Problem B POJ 3083 Children of the Candy Corn Problem C POJ 3009 Curling 2.0 Problem D POJ 1321 棋盘问题 Problem E P

潘卓夫 672

STM32CubeMX之定时器输入捕获

上一章介绍了STM32定时器的PWM输出功能,定时器的几个通道除了有输出功能外,还可以做输入用,可捕获信号的上升沿或下降沿,可以用于测量信号的频率、周期、占空比等。本篇文章将介绍用STM3...

zhang062061的专栏 1363

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【详解】Sudoku POJ - 2676(经典问题 DFS 重复剪枝)⭐⭐⭐

Sudoku POJ - 2676 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 fr...

Suprit's blog 787

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

POJ-2676 Sudoku(简单数独-dfs深搜)

Sudoku Time Limit: 2000MS Memory Limit: 65536K 题目链接 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 s...

孤风的博客 797

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Sudoku POJ - 2676 题解

原题 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 cell

qq_45750017的博客 967

poj2676——Sudoku(深度搜索)

DescriptionSudoku 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

Beyond the Sora 588

【怒撕剑指Offer】之「数组中重复的数字」

题目描述:在一个长度为 n 的数组里的所有数字都在 0 ~ n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2,3,1,0,2,5,3} ,那么对应的输出是重复的数字 2 或 3。 解题思路: 可以先排序再找,时间复杂度 O(nlogn);也可以用哈希表,时间复杂度 O(n) ,额外...

厉兵秣码 379

【POJ - 2676】Sudoku(数独 dfs+回溯)

-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题:给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1-9的数字,其余的则为空白(数字0为空白)。请在空白表格中填入数字1-9使得9*9表格的每行、每列、每个3*3块内无重复数字。 Input 第一行输入一个整数T,表示样例数量。对于每个样例, 一共...

Sky丨Star的博客 79

POJ 2676 Sudoku 数独(dfs)

** POJ 2676 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 decima...

逝者如斯夫,不舍昼夜 284

PKU2676 Sudoku 数独

/*SudokuTime Limit: 2000MS        Memory Limit: 65536KTotal Submissions: 4283        Accepted: 1934        Special JudgeDescriptionSudoku is a very simple task. A square table with 9 r

zonelight的专栏 1169

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 othe

码农的一亩三分地 928

M - Sudoku

M - Sudoku Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2676 Description Sudoku is a very simple task. A square table wit

CSUST_ACM的专栏 803
上一篇: 北大ACM poj2538 WERTYU
下一篇: 北大ACM poj1663
gubojun
博客等级 码龄15年 91粉丝 207原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值