Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12005   Accepted: 5984   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
题目大意:数独填空。
解题方法:搜索。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; typedef struct
{
int x;
int y;
}Point; Point p[]; char Maze[][];
int nCount = ;
bool bfind = false; bool Judge1(int row, int n)
{
for (int i = ; i < ; i++)
{
if (Maze[row][i] == n)
{
return false;
}
}
return true;
} bool Judge2(int col, int n)
{
for (int i = ; i < ; i++)
{
if (Maze[i][col] == n)
{
return false;
}
}
return true;
} bool Judge3(int row, int col, int n)
{
row = row / ;
col = col / ;
for (int i = row * ; i < row * + ; i++)
{
for (int j = col * ; j < col * + ; j++)
{
if (Maze[i][j] == n)
{
return false;
}
}
}
return true;
} void DFS(int Step)
{
if (Step == nCount && !bfind)
{
bfind = true;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
printf("%d", Maze[i][j]);
}
printf("\n");
}
}
for (int i = ; i <= ; i++)
{
if (Judge1(p[Step].x, i) && Judge2(p[Step].y, i) && Judge3(p[Step].x, p[Step].y, i) && !bfind && Maze[p[Step].x][p[Step].y] == )
{
Maze[p[Step].x][p[Step].y] = i;
DFS(Step + );
Maze[p[Step].x][p[Step].y] = ;
}
}
} int main()
{
int nCase;
char str[];
scanf("%d", &nCase);
memset(Maze, , sizeof(Maze));
while(nCase--)
{
nCount = ;
bfind = false;
for (int i = ; i < ; i++)
{
scanf("%s", str);
for (int j = ; j < ; j++)
{
Maze[i][j] = str[j] - '';
if (Maze[i][j] == )
{
p[nCount].x = i;
p[nCount].y = j;
nCount++;
}
}
}
DFS();
}
return ;
}

POJ 2676 Sudoku的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

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

  2. ACM : POJ 2676 SudoKu DFS - 数独

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

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

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

  4. POJ 2676 Sudoku (数独 DFS)

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

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

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

  6. poj 2676 Sudoku ( dfs )

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

  7. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  8. POJ 2676 Sudoku (DFS)

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

  9. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

随机推荐

  1. JavaScript实用技巧总结

    前言 总结一下最近接触到的JavaScript语法糖,与大家共享. 每块糖都有详细的说明和示例,就不多说了. 准确的类型检查 /* * @function: * 类型检查示例 * 通过此方法,可以检查 ...

  2. jQuery的XX如何实现?——3.data与cache机制

    往期回顾: jQuery的XX如何实现?——1.框架 jQuery的XX如何实现?——2.show与链式调用 -------------------------- 源码链接:内附实例代码 jQuery ...

  3. 布局神器display:table-cell

    元素两端对齐 第一个案例是让两个元素分别向左和向右对齐,如果是过去,我一定会用float来实现,但其实用table可以这么做: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  4. 浅谈压缩感知(二十七):压缩感知重构算法之稀疏度自适应匹配追踪(SAMP)

    主要内容: SAMP的算法流程 SAMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.SAMP的算法流程 前面所述大部分OMP及其前改算法都需要已知信号的稀疏度 ...

  5. 你应该知道的RPC原理

    你应该知道的RPC原理 在学校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互 ...

  6. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  7. python类的特性

    #encoding=utf-8 class Province: #静态字段 memo = '这里是静态变量' def __init__(self,name,capital,leader,flag): ...

  8. 爬虫神器xpath的用法(三)

    xpath的多线程爬虫 #encoding=utf-8 ''' pool = Pool(4) cpu的核数为4核 results = pool.map(爬取函数,网址列表) ''' from mult ...

  9. 安卓App流量统计

    http://keepcleargas.bitbucket.org/2013/10/12/android-App-Traffic.html 安卓App流量统计 12 OCT 2013 android流 ...

  10. nginx用户认证配置( Basic HTTP authentication)

    ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容.web上的一些内容不想被其他人知道,但是又想让部分人看到.nginx的http aut ...