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 ...
随机推荐
- HDU 2665.Kth number 区间第K小
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 脚本路径问题_dirname
pwd可获取命令当前的路径 可是若我们想在脚本中获取脚本所在文件夹的路径,这种方法是不够用的. 例如,我们的脚本放在/home/user/script/下,名字叫做getpath.sh getpath ...
- csdn博客
https://blog.csdn.net/lydstory123?t=1 以前还有几篇文章 忘记了
- Photoshop零基础教程集锦,助你快速进阶为大佬,轻松、任性!!!
现今,对于Web或App UI设计师而言,除了不断学习专业知识,提升设计技能.掌握一款得心应手的设计工具(例如设计师们常用的图像处理工具PhotoShop,矢量图绘制工具AI, 图形视频处理工具AE, ...
- sqlserver 数据分发复制 发布订阅
转载地址:https://www.cnblogs.com/lizejia/p/6062674.html
- 连接db2数据库出现No buffer space available (maximum connections reached?)
Caused by: javax.naming.NamingException: [jcc][t4][2043][11550][3.57.82] 异常 java.net.SocketException ...
- 【Tomcat】Tomcat + Memcached 实现session共享
概述 web项目中,Tomcat的访问量总是有限的,这时候就需要用到Tomcat集群,多个Tomcat的时候就要考虑Session共享的问题,这里介绍一种使用Memcached做Session共享的解 ...
- BP神经网络的参数改进参考?
参考文献:黄巧巧. 基于BP神经网络的手写数字识别系统研究[D].华中师范大学,2009. 47-52 BP神经网络的缺陷:收敛速度慢和局部极小点的问题 使用的改进方案有 1. 学习速率(learn ...
- 58.UIScrollView XIB拖拽约束
第一步: 拖拽UIScrollView 到控制器上 ,给scrollView 添加约束 ,这时是正常的 第二步:scrollview上添加UIview ,(注意:这个 ScrollView就是根据这个 ...
- 2019.02.09 codeforces451 E. Devu and Flowers(容斥原理)
传送门 题意简述:给出n堆花,对于第j堆,有f[j]朵花,每堆花的颜色不同,现在要从中选出s朵,求方案数. 思路: 假设所有花没有上限直接插板法,现在有了上限我们用容斥扣掉多算的 状压一下再容斥:fi ...