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 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 数独游戏。感觉这道题的代码还是蛮实用的~思路是dfs从头到尾依次枚举每个0点,尝试填入1->9,不合适再返回上层,直到填满0为止。
还有就是反搜(0ms),虽然我不会证明,但是和正搜(891ms)对比,是不是很神奇?
#include<stdio.h>
#include<string.h> char aa[][];
int a[][];
int row[][],col[][],squ[][];
int c1,c2,f; int jud(int x,int y)
{
if(x<=&&y<=) return ;
if(x<=&&<=y&&y<=) return ;
if(x<=&&<=y) return ;
if(<=x&&x<=&&y<=) return ;
if(<=x&&x<=&&<=y&&y<=) return ;
if(<=x&&x<=&&<=y) return ;
if(<=x&&y<=) return ;
if(<=x&&<=y&&y<=) return ;
if(<=x&&<=y) return ;
} void dfs()
{
int i,j,k;
if(f==) return;
if(c1==c2){
f=;
for(i=;i<=;i++){
for(j=;j<=;j++){ //反搜枚举9->1
printf("%d",a[i][j]);
}
printf("\n");
}
return;
}
for(i=;i<=;i++){
for(j=;j<=;j++){
if(a[i][j]==){
for(k=;k<=;k++){
if(row[i][k]==&&col[j][k]==&&squ[jud(i,j)][k]==){
row[i][k]=;
col[j][k]=;
squ[jud(i,j)][k]=;
a[i][j]=k;
c2++;
dfs();
row[i][k]=;
col[j][k]=;
squ[jud(i,j)][k]=;
a[i][j]=;
c2--;
}
}
return;
}
}
}
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--){
memset(a,,sizeof(a));
memset(row,,sizeof(row));
memset(col,,sizeof(col));
memset(squ,,sizeof(squ));
c1=;
for(i=;i<;i++){
getchar();
scanf("%s",aa[i]);
for(j=;j<;j++){
a[i+][j+]=aa[i][j]-'';
if(a[i+][j+]==) c1++;
else{
row[i+][a[i+][j+]]=;
col[j+][a[i+][j+]]=;
squ[jud(i+,j+)][a[i+][j+]]=;
}
}
}
c2=;f=;
dfs();
}
return ;
}

POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  2. POJ 2676/2918 数独(dfs)

    思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...

  3. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  4. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  5. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  6. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  7. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  8. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

  9. DFS POJ 2676 Sudoku

    题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...

随机推荐

  1. docker&k8s填坑记

    本篇主要用于记录在实施docker和kubenetes过程中遇到的一个问题和解决办法. 本节部分内容摘自互联网,有些部分为自己在测试环境中遇到到实际问题,后面还会根据实际情况不断分享关于docker/ ...

  2. Dash 使用

    花了 160 买了这个软件,至少看一遍它的 user guide,钱不能白花. https://kapeli.com/guide/guide.html 设置全局快捷键 Preference -> ...

  3. 使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...

  4. Jquery放大镜插件---imgzoom.js(原创)

    Jquery放大镜插件imgzoom能够实现图片放大的功能,便于与原图进行比较. 使用方法: 1.引入jQuery与imgzoom,imgzoom.css <link rel="sty ...

  5. MVC入门——删除页

    添加Action DeleteUserInfo using System; using System.Collections.Generic; using System.Linq; using Sys ...

  6. 数据库的update、delete、insert和select用法

    String sql=null; 1.sql="update 表名 set <列名>=<表达式> [where=<表达式>]" 2.sql=&q ...

  7. SpringBoot-(2)-Web的json接口,静态网页,动态页面

    一, 了解注解@Controller和@RestController @Controller:处理Http请求 @RestController:Spring4以后新增注解,相当于@Controller ...

  8. xcode环境变量设置(转载)

    一般我们在xcode里面配置包含工程目录下头文件的时候,都要关联着相对路径和绝对路径,如果只是自己用这个项目,用绝对路径的问题不大,但是如果你把工程发给别人,别人就要在改这个绝对路径,这时候绝对路径的 ...

  9. 理解vue ssr原理,自己搭建简单的ssr框架

    前言 大多数Vue项目要支持SSR应该是为了SEO考虑,毕竟对于WEB应用来说,搜索引擎是一个很大的流量入口.Vue SSR现在已经比较成熟了,但是如果是把一个SPA应用改造成SSR应用,成本还是有些 ...

  10. python库学习笔记——分组计算利器:pandas中的groupby技术

    最近处理数据需要分组计算,又用到了groupby函数,温故而知新. 分组运算的第一阶段,pandas 对象(无论是 Series.DataFrame 还是其他的)中的数据会根据你所提供的一个或多个键被 ...