Sudoku

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

Special Judge

[Submit] [Status] [Discuss]

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<iostream>
#include <string.h>
#include <stdio.h> using namespace std; int map[][]; //九宫格 bool row[][]; //row[i][x] 标记在第i行中数字x是否出现了
bool col[][]; //col[j][y] 标记在第j列中数字y是否出现了
bool grid[][]; //grid[k][x] 标记在第k个3*3子格中数字z是否出现了 //(这里说明的字母不代表下面程序中的变量) bool DFS(int x,int y) ///从左到右 上到下
{
if(x==)
return true; bool flag=false; if(map[x][y])
{
if(y==) ///右边界的列
flag=DFS(x+,);///~~~~~~~~~~~~~~~~~~
else
flag=DFS(x,y+); if(flag) //回溯
return true;
else
return false;
}
else
{ int k=*((x-)/)+(y-)/+; for(int i=; i<=; i++) //枚举数字1~9填空
if(!row[x][i] && !col[y][i] && !grid[k][i])
{
map[x][y]=i; row[x][i]=true;
col[y][i]=true;
grid[k][i]=true; if(y==)
flag=DFS(x+,); ///~~~~~~~~~~~~~~~~~~~~~~
else
flag=DFS(x,y+); if(!flag) //回溯,继续枚举
{
map[x][y]=; row[x][i]=false;
col[y][i]=false;
grid[k][i]=false;
}
else
return true;
}
}
return false;
} int main()
{
int t;
int i,j;
cin>>t;
while(t--)
{ char MAP[][];
for(i=; i<=; i++)
{
for(j=; j<=; j++)
{
//scanf("%c",&map[i][j]);
cin>>MAP[i][j];
map[i][j]=MAP[i][j]-'';
} }
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(map[i][j])
{
int k=*((i-)/)+(j-)/+;
row[i][ map[i][j] ]=true;
col[j][ map[i][j] ]=true;
grid[k][ map[i][j] ]=true;
}
}
} DFS(,); for(i=; i<=; i++)
{
for(j=; j<=; j++)
printf("%d",map[i][j]);
printf("\n");
}
}
return ;
}

poj 2676 如何填满九宫格的更多相关文章

  1. poj 2676 Sudoku ( dfs )

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

  2. 随手练——POJ - 2676 数独 (回溯法)

    POJ - 2676 : http://poj.org/problem?id=2676: 解题思想 (大力出奇迹): 1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行.列,以及3 ...

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

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  4. ACM : POJ 2676 SudoKu DFS - 数独

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

  5. css实现div的高度填满剩余空间

    css实现div的高度填满剩余空间 .top{ width: 100%; height: 70px;} .bottom{background-color: #cc85d9;width: 100%;po ...

  6. datagridview随窗体的大小而变,表格填满控件

    在C#winform布局的时候,我们拖一个datagridview到窗体上面,将datagridview调整为适合窗体的大小,但是我们运行之后,点击最大化按钮的时候,却发现datagridview的大 ...

  7. [WP8] ListBox的Item宽度自动填满

    [WP8] ListBox的Item宽度自动填满 范例下载 范例程序代码:点此下载 问题情景 开发WP8应用程序的时候,常常会需要使用ListBox作为容器来呈现各种数据集合.但是在ListBox呈现 ...

  8. 深搜+回溯 POJ 2676 Sudoku

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

  9. 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的

    while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...

随机推荐

  1. 0 or 1,1 and 0

    最近小编遇到很头疼的的一件事 就是以下这几道运算题 ,以下结果是小编经过大量的运算得出的 一.或运算 1.0 or 1 结果为:1 2.1 or 0 结果为:1 3.1 or 2 结果为:1 4.2 ...

  2. linux以16进制方式查看文件

    vim打开文件 :%!xxd  以16进制查看 :%!xxd -r  转回来

  3. [JAVA]多线程下如何确定执行顺序性

    最近在讨论一个下载任务:要求文件下载后进行打包,再提供给用户下载: 如何确保打包的线程在所有下载文件的线程执行完成后进行呢? 看看下面三个兄弟的本事: CountDownLatch.CyclicBar ...

  4. base64编码是什么

    首先明确一点base64 是一种编码格式.就想utf-8一样,能在电脑上表示所有字符,或者换句话说通过编码能让电脑理解你想要标识的字符(因为电脑只知道0和1 ) 就像ascII 中 0100 0001

  5. ajax的基础

    去年也是这个时候,开始学了ajax,也是这个技术领我走上了网页制作的道路,于是这样感觉到手写html比之前的dw拖拖拽拽要有意思得多. 话不多说,下面是一个例子: 这个是ajax显示页面:index. ...

  6. [转载]How To Install Nginx And PHP-FPM On CentOS 6 Via Yum

    http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/ http://blog.csdn.net/ ...

  7. 设置默认Browser

    电信A库要求android系统中有多个Browser时,开机自动设置一个默认浏览器,而不用弹出选择框让用户手动选择. 监听开机广播Intent.ACTION_BOOT_COMPLETED, 用Pack ...

  8. ubuntu配置ftp server

    ubuntu配置ftp server 1. 安装vsftpd   sudo apt-get install vsftpd 安装后会自动新建一个用户ftp,密码ftp,作为匿名用户登录的默认用户 sud ...

  9. Python开课复习7

    操作系统 操作系统把复杂的硬件操作封装成简单的接口给用户/应用程序使用,其中文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件,可以将自己的数据永久保存下来. #1. 打开文 ...

  10. 使用Hadoop API 压缩HDFS文件

    下篇解压缩:使用Hadoop API 解压缩 HDFS文件 起因: 集群磁盘剩余空间不足. 删除了存储在HDFS上的,一定时间之前的中间结果,发现并不能释放太多空间,查看计算业务,发现,每天的日志存在 ...