poj 2676 如何填满九宫格
Sudoku
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Special Judge
Description

Input
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
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 如何填满九宫格的更多相关文章
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- 随手练——POJ - 2676 数独 (回溯法)
POJ - 2676 : http://poj.org/problem?id=2676: 解题思想 (大力出奇迹): 1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行.列,以及3 ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- css实现div的高度填满剩余空间
css实现div的高度填满剩余空间 .top{ width: 100%; height: 70px;} .bottom{background-color: #cc85d9;width: 100%;po ...
- datagridview随窗体的大小而变,表格填满控件
在C#winform布局的时候,我们拖一个datagridview到窗体上面,将datagridview调整为适合窗体的大小,但是我们运行之后,点击最大化按钮的时候,却发现datagridview的大 ...
- [WP8] ListBox的Item宽度自动填满
[WP8] ListBox的Item宽度自动填满 范例下载 范例程序代码:点此下载 问题情景 开发WP8应用程序的时候,常常会需要使用ListBox作为容器来呈现各种数据集合.但是在ListBox呈现 ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的
while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...
随机推荐
- mysql 多表查询先排序,然后再取分组<mysql 先order by,然后再取group by分组>
select * from ( select cv.lasttime,cm.mailbox,cv.clientip from `co_user_visitlog` as cv INNER JOIN ` ...
- Spring 中的类加载机制 - ClassLoader
Spring 中的类加载机制 - ClassLoader Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ClassLoa ...
- Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用
Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019869 ...
- Python 之异常处理机制
python在程序运行出现错误时时有相应的反应机制 ,我们可以针对不同的错误做出不同的响应 list1 = ['a','b','c'] print(list1[4]) #>>>Ind ...
- 如何获取堆的dump 的信息,如何分析
获取方式: 1. jdk 自带启动参数 -XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=/x/x 产生dump日志,然后用visualVm分析 2. jmap 命 ...
- 2017/2/16:自己ajax+json习惯性写法 代码拼接的写法 +json用post提交乱码的原因
1.先导入jquery的包 2.ajax的写法跟注意点 返回一个list的写法 代码拼接写法: html层: 2.script处 4:在你前面传递参数的时候没有遇到乱码问题的情况下,你使用json并且 ...
- 验证签名机制——java示例
简单的验证公钥私钥签名认证: 公钥是对外公开的部分,私钥是不公开的部分,一般在项目开发中公钥是给用户,私钥是存于服务器上,二者中有一个加密,则需要另外一个来解密. 下面是java实现的一个比较简单的示 ...
- 2018.12.15 hdu4641 K-string(后缀自动机)
传送门 后缀自动机基础题. 题意简述:支持动态在串尾插入字符,查询在串中出现超过kkk次的子串的个数. 动态修改samsamsam,每次增量构造好了之后在parentparentparent树上从新建 ...
- hdu-1033(格式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1033 参考文章:https://blog.csdn.net/curson_/article/detai ...
- 使用PostSharp在.NET平台上实现AOP(转)
出处:https://www.cnblogs.com/leoo2sk/archive/2010/11/30/aop-postsharp.html 摘要 本文首先介绍AOP(面向方面编程)的相关概念及理 ...