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 ...
随机推荐
- 多维数组sorted函数的用法
对某一个位置排列 l=[[1,5,7,9],[5,10,6,11],[4,2,1,4]] newlist=sorted(l,key=lambda iterm : iterm[0],reverse=Tr ...
- linux日志查找技巧
1.查找日志最后10行 tail -n test.log 查询日志尾部最后10行的日志; 2.关键词查询 grep '2014-12-17 16:17:20' test.log
- Spring IOC(二)beanName 别名管理
Spring IOC(二)beanName 别名管理 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.AliasReg ...
- 1.about
1)about Evarobot a.Evarobot Tech Specs 2)应用场景 Using a PC running visualisation/monitoring software a ...
- [C#]“正在终止线程”的问题
在C#中启用线程后,如果试图使用Abort方法来终止线程,那么必定会抛出“正在终止线程”的异常,一开始我也想过如何来避免这种异常出现,花了不少气力,但最后发现全是徒劳. 原因是一个正在运行的线程被终止 ...
- kbmmw 的远程桌面功能
kbmmw 内置了远程桌面控制功能好几年了,好多同学居然不知道这特性,因为kbmmw 默认没有开放这个特性, 今天我就给大家说一下如何开放这个功能,并用官方自带例子说一下使用方法. 首先要开放这个特性 ...
- socketserver实例化过程
一.创建server对象时__init__的执行 找继承中的__init__ 这是ThreadingMixIn类中的方法 这是TCPServer类中的方法(父类BaserServer中还会用到fini ...
- php代码中pre的作用??
- 2019.01.22 zoj3583 Simple Path(并查集+枚举)
传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...
- android studio友盟分享demo运行报错Gradle's dependency cache may be corrupt解决方法
gradle-wrapper.properties里修改了gradle的版本,与之前没有报错的项目gradle版本一致.