Sudoku Killer

Problem Description

自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。

据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。

所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。



数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。



例题:





答案:


Input

本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。

Output

对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。

对于每组测试数据保证它有且只有一个解。

Sample Input

7 1 2 ? 6 ? 3 5 8
? 6 5 2 ? 7 1 ? 4
? ? 8 5 1 3 6 7 2
9 2 4 ? 5 6 ? 3 7
5 ? 6 ? ? ? 2 4 1
1 ? 3 7 2 ? 9 ? 5
? ? 1 9 7 5 4 8 6
6 ? 7 8 3 ? 5 1 9
8 5 9 ? 4 ? ? 2 3

Sample Output

7 1 2 4 6 9 3 5 8
3 6 5 2 8 7 1 9 4
4 9 8 5 1 3 6 7 2
9 2 4 1 5 6 8 3 7
5 7 6 3 9 8 2 4 1
1 8 3 7 2 4 9 6 5
2 3 1 9 7 5 4 8 6
6 4 7 8 3 2 5 1 9
8 5 9 6 4 1 7 2 3

——————————————————————————————————————————————————————————

题目是在?位置填上空格完成成数独,将?位置保存在一个结构体里处理。
注意9宫判重如下
int xx = (x / 3) * 3;
int yy = (y / 3) * 3;
for (int i = xx; i<xx + 3; i++)
for (int j = yy; j<yy + 3; j++)
{
if (mp[i][j] == a)
return 0;
}




#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector> using namespace std; int mp[12][12];
int tot, cnt,flag,q;
struct node
{
int x, y;
}pl[100]; bool cheak(int x, int y, int a)
{
for (int i = 0; i<10; i++)
{
if (mp[i][y] == a)
return 0;
}
for (int i = 0; i<10; i++)
{
if (mp[x][i] == a)
return 0;
}
int xx = (x / 3) * 3;
int yy = (y / 3) * 3;
for (int i = xx; i<xx + 3; i++)
for (int j = yy; j<yy + 3; j++)
{
if (mp[i][j] == a)
return 0;
}
return 1;
} void print()
{
if (q++)
printf("\n");
for (int i = 0; i<9; i++)
{
for (int j = 0; j<8; j++)
{
printf("%d ", mp[i][j]);
}
printf("%d\n",mp[i][8]);
}
} void dfs(int cnt)
{
int xx, yy;
if (flag == 1)
return;
if (cnt == tot)
{
flag = 1;
print();
return;
}
for (int i = 1; i < 10; i++)
{
if (cheak(pl[cnt].x, pl[cnt].y, i))
{
mp[pl[cnt].x][pl[cnt].y] = i;
dfs(cnt+1);
mp[pl[cnt].x][pl[cnt].y] =0;
}
}
} int main()
{
char ch;
q = 0;
while (scanf("%s",&ch)!=EOF)
{
tot = 0;
if (ch == '?')
{
mp[0][0] = 0;
pl[tot].x = 0;
pl[tot++].y = 0; }
else
mp[0][0] = ch - '0';
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
if (i == 0 && j == 0)
continue;
scanf("%s",&ch);
if (ch == '?')
{
mp[i][j] = 0;
pl[tot].x = i;
pl[tot++].y = j;
}
else
mp[i][j] = ch - '0'; }
cnt = 0;
flag = 0;
dfs(0);
}
return 0;
}


HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏的更多相关文章

  1. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  2. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  3. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏

    Zipper Problem Description Given three strings, you are to determine whether the third string can be ...

  5. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  6. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  7. Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏

    速算24点 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  8. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  9. Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. IOS6新特性之下拉刷新<UIRefreshControl>

    在IOS6未发布之前,几乎都是使用那个UIRefresh在实现下拉刷新,甚至有人还是先了上拉的功能,不得不说牛人很多啊.可能是Apple意识到了这个功能的实用性,在IOS6中增加了下拉刷新,但是上啦还 ...

  2. 自然对数e(转)

    e表示增长的极限 e=limx→+∞ (1+1/x)^x≍2.71828 假设,一根竹子,第一天是1米,第二天长了1米,然后这根柱子的长度变成了2米.相当于 (1+1/1)^1.上面这个假设,如果仔细 ...

  3. java jpa 报错

    表的字段名不能是数据库的关键字 INSERT INTO `todo`.`todo_item` (description, completed) VALUES ('test 111', false); ...

  4. Win32 Debug & Release

    今天帮汤老师调试程序,他生成的程序不能运行,怀疑子程序之间编译顺序的问题:我试了之后,也出现同样的问题,但是把Win32 Debug 换成Win32 Release却可以运行了. 网上搜索了下,在CV ...

  5. Python3 pow() 函数

    Python3 pow() 函数  Python3 数字 描述 pow() 方法返回 xy(x的y次方) 的值. 语法 以下是 math 模块 pow() 方法的语法: import math mat ...

  6. Python打杂之路

    1.任务要落到纸上好记性不如烂笔头,再好的记性也不如写到纸上明确无误,写到纸上就不用担心会漏掉哪项工作.平时,我们总是在忙着一项工作的同时还惦记着下一项工作,把工作都记下后,我们就可以专注于一项工作, ...

  7. Golang之定时器,recover

    滴答滴答……定时器的使用 package main import ( "fmt" "time" ) //定时器的使用 func main() { t := ti ...

  8. Linux 配置文件管理

    一.简介 参考:https://robots.thoughtbot.com/rcm-for-rc-files-in-dotfiles-repos http://dotfiles.github.io/ ...

  9. APP自动化测试怎么测?

    一般来说工具还有技术都是千篇一律 测试测到最后还是业务能力...

  10. centos 安装或更新最新版本软件包(git python etc)的方法 SCL IUS

    使用centos 经常发现官方提供的软件包版本过低,很多时候大家会选择下载源码自行编译,带来了很多麻烦. centos安装最新版本软件包,例如git,python等,可以通过红帽官方提供的softwa ...