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. 说不尽的MVVM(5) - 消息满天飞

    知识预备 阅读本文,我假定你具备以下知识: C#和WPF基础知识 Lambda表达式 清楚ViewModel的职责 如果我们的程序需要弹出一个MessageBox,我们应该怎么做? 我见过不少人在Vi ...

  2. Qt5.3 打印示例时出现错误

    说明:今天我在用Qt5.3写打印文档的时候,编译出错了,出错代码为: C:\Users\joe\Desktop\5-9\myPrint\mainwindow.cpp:35: error: undefi ...

  3. JS几种数组遍历方式以及性能分析对比

    前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...

  4. Linux 常用命令小结

    学习脚本几天了,总结下linux debian下脚本常用命令. Linux    1.添加删除账户 useradd / userdel    2.修改"张三"密码 passwd 张 ...

  5. Arduino单片机使用和开发问题记录

    1.将程序上传到板子时Arduino IDE提示“avrdude: stk500_getsync(): not in sync: resp=0x00” 网上查遇到这个问题的人比较多,有说驱动问题的,有 ...

  6. c++ string 和wstring 之间的互相转换函数

    #include <string> std::string ws2s(const std::wstring& ws) { std::string curLocale = setlo ...

  7. Android 使用Fragment界面向下跳转并一级级返回

    http://www.cnblogs.com/_ymw/p/4227862.html 1.首先贴上项目结构图: 2.先添加一个接口文件BackHandledInterface.java,定义一个set ...

  8. Number Range 管理之并行缓冲

    Number Range 管理之并行缓冲: 常用的事务代码SNRO,SM56还有一些业务专用的号码管理,可以在SPRO中查找: SNRO :Number Range 管理 一般的操作是维护号码范围.如 ...

  9. 【LeetCode】Power of Two

    问题描写叙述 Given an integer, write a function to determine if it is a power of two. 意:推断一个数是否是2的n次幂 算法思想 ...

  10. 每日英语:A Whiff Of 'Welcome Home'

    Buying real estate involves weighing a lot of factors: location, price . . . smell? Some condo devel ...