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 ...
随机推荐
- Expressions入门示例
学习表达式的入门例子,前提是要对委托有一定的了解,泛型明白一些.using System; using System.Linq; using System.Linq.Expressions; usin ...
- 终于搞定Fastreport2.x PDF输出,相信其他版本也差不多
这个版本有powerpdf可以支持,但有bug, 经过反复摸索,终于搞定. 基本可用. 主要是中英文混合在一起,如果按中文输出,会有英文宽度也是中文的宽度了,格式变化,不可 接受. 而按英文输出,又是 ...
- java 环境搭建
一.安装jdk 下载jdk http://www.oracle.com/technetwork/java/javase/downloads 将下载的jdk文件放到 /opt 下解压 $sudo cp ...
- BUG(1):一个关于指针的bug
是时候记录一下这个让我栽了两次的bug了. 具体情况如下: #include <stdio.h>#include <stdlib.h> struct app_info_t { ...
- 常用到的photoshop实用设计功能都在这了!
常用到的photoshop实用设计功能都在这了!赶快收藏学起来,需转不谢~ 编辑:千锋UI设计
- hbase shell 命令
HBase使用教程 时间 2014-06-01 20:02:18 IT社区推荐资讯 原文 http://itindex.net/detail/49825-hbase 主题 HBase 1 基 ...
- SharePoint 开发小结
目标:将sharepoint网站对接Office 365 最直接的API:How to: Add Office 365 APIs to a Visual Studio project http://m ...
- [C#.net]处理UTF-8文件乱码
今天帮同事处理一个2M左右的文件的格式,发现使用Encoding.default & Encoding.UTF8 & Encoding.GetEncoding("GB2312 ...
- oracle创建表空间、添加数据库文件
创建表空间: create [undo|TEMPORARY]tablespace venn datafile '/opt/oracle/db01/app/oracle/oradata/OSSORCL/ ...
- JSON.parse和JSON.stringify的区别
JSON.stringify()的作用是将 JavaScript 值转换为 JSON 字符串, 而JSON.parse()可以将JSON字符串转为一个对象. 简单点说,它们的作用是相对的,我用JSON ...