Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16444   Accepted: 8035   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

题意:每行每列每个小的九宫格 每个数字只出现一次;

看着很高大上的题目做起来这么好玩,搜索真是很神奇!应该是第一次写带有返回值的搜索

转载请注明出处:優YoU  http://user.qzone.qq.com/289065406/blog/1303713313

大致题意:

九宫格问题,也有人叫数独问题

把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。

0是待填位置,其他均为已填入的数字。

要求填完九宫格并输出(如果有多种结果,则只需输出其中一种)

如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格

解题思路:

DFS试探,失败则回溯

用三个数组进行标记每行、每列、每个子网格已用的数字,用于剪枝

bool row[10][10];    //row[i][x]  标记在第i行中数字x是否出现了

bool col[10][10];    //col[j][y]  标记在第j列中数字y是否出现了

bool grid[10][10];   //grid[k][x] 标记在第k个3*3子格中数字z是否出现了

row 和 col的标记比较好处理,关键是找出grid子网格的序号与 行i列j的关系

即要知道第i行j列的数字是属于哪个子网格的

首先我们假设子网格的序号如下编排:

由于1<=i、j<=9,我们有: (其中“/”是C++中对整数的除法)

a= i/3 , b= j/3  ,根据九宫格的 行列 与 子网格 的 关系,我们有:

不难发现 3a+b=k

即 3*(i/3)+j/3=k

 

又我在程序中使用的数组下标为 1~9,grid编号也为1~9

因此上面的关系式可变形为 3*((i-1)/3)+(j-1)/3+1=k

 

 

有了这个推导的关系式,问题的处理就变得非常简单了,直接DFS即可

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
int row[][],col[][],grid[][];
int g[][];
bool dfs(int x,int y)
{
if(x == )
{
return true;
}
bool flag = false;
if(g[x][y])
{
if(y == )
{
flag = dfs(x + , );
}
else
{
flag = dfs(x, y + );
}
if(flag)
return true;
else
return false;
}
else if(g[x][y] == )
{
for(int i = ; i <= ; i++)
{
int k = (x - ) / * + (y - ) / + ;
if(col[y][i] == && row[x][i] == && grid[k][i] == )
{
g[x][y] = i;
col[y][i] = ;
row[x][i] = ;
grid[k][i] = ;
if(y < )
{
flag = dfs(x,y + );
}
else
{
flag = dfs(x + , );
}
if(flag == false)
{
g[x][y] = ;
col[y][i] = ;
row[x][i] = ;
grid[k][i] = ;
}
else
return true;
}
}
}
return false;
}
int main()
{
int t;
scanf("%d", &t);
getchar();
while(t--)
{
memset(row,,sizeof(row));
memset(col,,sizeof(col));
memset(grid,,sizeof(grid));
memset(g,,sizeof(g));
char ch;
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
scanf("%c", &ch);
if(ch != '')
{
g[i][j] = ch - '';
row[i][ch - ''] = ;
col[j][ch - ''] = ;
int k = (i - ) / * + (j - ) / + ;
grid[k][ch - ''] = ;
}
}
getchar();
}
dfs(,);
for(int i = ; i <= ; i ++)
{
for(int j = ; j <= ; j++)
{
printf("%d",g[i][j]);
}
printf("\n");
}
}
return ;
}

POJ2676Sudoku(类似于八皇后)的更多相关文章

  1. POJ 1321 棋盘问题【DFS/回溯/放与不放/类似n皇后】

    棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62164 Accepted: 29754 Description 在一 ...

  2. 回溯算法——解决n皇后问题

    所谓回溯(backtracking)是通过系统地搜索求解问题的方法.这种方法适用于类似于八皇后这样的问题:求得问题的一个解比较困难,但是检查一个棋局是否构成解很容易. 不多说,放上n皇后的回溯问题代码 ...

  3. 2n皇后问题-------递归 暴力求解题与分布讨论题

    问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一列或同一 ...

  4. 二模12day1解题报告

    T1.笨笨与电影票(ticket) 有n个1和m个0,求每个数前1的个数都大于等于0的个数的排列数. 非常坑的一道题,推导过程很烦.首先求出所有排列数是 C(n+m,m),然后算不合法的个数. 假设存 ...

  5. Codevs p1004 四子连棋

                          四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...

  6. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  7. HDU 1045(炮台安置 DFS)

    题意是在 n*n 的方格中进行炮台的安置,炮台不能处于同一行或同一列(类似于八皇后问题),但若是炮台间有墙壁阻挡,则可以同时安置这对炮台.问图中可以安放的最大炮台数目. 用深搜的方法,若此处为空地,则 ...

  8. POJ1321 棋盘问题 —— DFS回溯

    题目链接:http://poj.org/problem?id=1321 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  9. USACO1.5 Checker Challenge(类n皇后问题)

    B - B Time Limit:1000MS     Memory Limit:16000KB     64bit IO Format:%lld & %llu   Description E ...

随机推荐

  1. poj 1159 Palindrome

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 59094   Accepted: 20528 Desc ...

  2. FusionCharts或其它flash的div图层总是浮在最上层? (转)

    div的图层由div的style中的z-index来决定,z-index是层垂直屏幕的坐标,0最小,越大的话位置越靠上. 由于FusionCharts的图表都放在div中,如果页面还有其他的div,将 ...

  3. 【转】【PNG压缩工具】PNG 图像的优化及压缩工具介绍

    图像格式有许多种不同类型,在互联网上最常见的有JPEG.GIF.BMP.TIFF和PNG.每一种图像格式都有它自己的用途,比如GIF是用于动画的,JPEG是用于高清图片的,这种图片在保存或者调整大小后 ...

  4. U3D协程Coroutine之WWW与Update()的并行测试

    using System.Collections; using UnityEditor; using UnityEngine; using UnityEngine.UI; /************* ...

  5. GeoServer+MySQL安装及配置过程

    GeoServer的安装配置请参考 http://simen-net.iteye.com/blog/609078 由于大部分WEBGIS不仅仅只是一个地图的显示,还需要一些业务处理,会有用到数据库地方 ...

  6. 软件开发之路、Step 1 需求分析

    百度百科 需求分析 所谓"需求分析",是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应输出什么.可以说,在软件工程当中的“需求分析” ...

  7. 蓝牙技术BlueTooth

    转载网址:http://blog.csdn.net/dxdxsmy/article/details/7790568 蓝牙核心架构概念的理解请参考上面的网址.

  8. Sqlite3 设置插入触发器

    需求: 数据库中表t_VerifyCsmDetail需要最多保存10W条记录,超出时删除最旧的那一条. 思路:设置插入触发器.插入前先判断表中记录总数,如果大于99999条,则删除最旧的一条记录. 代 ...

  9. 开源分布式实时计算引擎 Iveely Computing 之 安装部署(2)

          在Github中下载代码和二进制程序中,您都会看到一个bin\iveely computing目录,里面即是Iveely Computing的运行库.              以前总是有 ...

  10. commonjs amd cmd的区别

    一篇博客告诉你三者的区别:http://zccst.iteye.com/blog/2215317 告诉你三者同requirejs seajs的区别:http://blog.chinaunix.net/ ...