Sudoku

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 8
Special Judge
Problem Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
 
Input
The input data will start with the number of 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
For each test case your program should print 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
 
Source
PKU
 
 
一道DFS题,虽然过了但耗时都在几百ms,我写了3次,前两次耗时都在7、8百ms,最后一次500ms
 
 
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int Sudoku[9][9];
int Count,c,vacant[81][2];
bool ro[9][10],co[9][10],sq[3][3][10]; //分别标记每行、每列、每个3*3方格各个数字是否出现过 void DFS(int i)
{
if(c==Count)
return; int k;
for(k=1;k<10;k++)
{
if(!ro[vacant[i][0]][k]&&!co[vacant[i][1]][k]&&!sq[vacant[i][0]/3][vacant[i][1]/3][k])//如果没出现过
{
Sudoku[vacant[i][0]][vacant[i][1]]=k;
c++;
ro[vacant[i][0]][k]=true;
co[vacant[i][1]][k]=true;
sq[vacant[i][0]/3][vacant[i][1]/3][k]=true;
DFS(i+1);
if(c==Count)
return;
Sudoku[vacant[i][0]][vacant[i][1]]=0;
c--;
ro[vacant[i][0]][k]=false;
co[vacant[i][1]][k]=false;
sq[vacant[i][0]/3][vacant[i][1]/3][k]=false;
}
}
return;
} int main()
{
int T;
cin>>T;
while(T--)
{
int i,j;
Count=0;
c=0;
string temp[9];
memset(ro,false,sizeof(ro));
memset(co,false,sizeof(co));
memset(sq,false,sizeof(sq));
memset(vacant,0,sizeof(vacant));
for(i=0;i<9;i++)
cin>>temp[i];
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
Sudoku[i][j]=temp[i][j]-'0';
ro[i][Sudoku[i][j]]=true;
co[j][Sudoku[i][j]]=true;
sq[i/3][j/3][Sudoku[i][j]]=true;
if(Sudoku[i][j]==0)
{
vacant[Count][0]=i; //将需要填的空格位置保存
vacant[Count][1]=j;
Count++; //计算空格个数
}
}
}
DFS(0);
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<<Sudoku[i][j];
cout<<endl;
}
}
}

HDOJ-三部曲一(搜索、数学)-1013-Sudoku的更多相关文章

  1. 三部曲一(搜索、数学)-1016-Code

    Code Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submissi ...

  2. HDU 4294 Multiple(搜索+数学)

    题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n ...

  3. HDOJ三部曲-DP-1017-pearls

    Pearls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submis ...

  4. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  5. Leetcode初级算法(排序和搜索+数学篇)

    合并两个有序数组 开始的时候将这道题理解错了,发现几个奇怪的测试案例后才明白这道题什么意思.本来的想法就是把nums2全部放到num1里面,然后删除重复元素.排序一下,就有了下面的代码: class ...

  6. luogu 1731 搜索剪枝好题

    搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...

  7. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  8. LeetCode Top 100 Liked 点赞最高的 100 道算法题

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...

  9. SQL查询——同一张表的横向与纵向同时比较

    表名:student 表结构及数据: +----+--------+---------+------+------------+--------------+---------+ | id | nam ...

  10. vijosP1359 Superprime

    vijosP1359 Superprime 链接:https://vijos.org/p/1359 [思路] 搜索+数学. 很明显的搜索,依次确定每一个数,用参数sum记录dfs即可. 本题的关键在于 ...

随机推荐

  1. IP的正则表达式

    首先分析IP地址0-255: 0-9:       [0-9]或 \d表示数字 10-99:   [1-9]\d 100-199: 1/d{2} 200-249:    2[0-4]\d 250-25 ...

  2. 看懂UML图

    看懂UML类图和时序图 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关系: 能看懂类图中各个类之间的线条.箭头代表什么意思后,也就足够应对 日常的工作和交流: 同时,我们应该能将类图 ...

  3. springMvc配置编码过滤器

    在web.xml中配置 <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filte ...

  4. BOM初始状态配置

    一个很简单的东西:有些公司在建BOM的时候,可能不是一次性建好,或者是想需要审核或者什么的,先不让使用. 其实这是SPRO里面配置的...路径:生产->基本物料->物料清单->物料单 ...

  5. Objective-C:Foundation框架-常用类-NSNull

    集合中是不能存储nil值的,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么可以使用NSNull,它也是NSObject的一个子类. #import <Found ...

  6. Skrollr.js -- 使用Skrollr创建视差滚动效果页面

    使用方法:  http://www.helloweba.com/view-blog-262.html http://www.uedsc.com/skrollr.htmlhttp://www.hello ...

  7. 如何对HTML进行编码和解码?

    答案:这个比较简单//HTML进行编码和解码Console.WriteLine(System.Web.HttpUtility.HtmlEncode("<h1>我是中文字符!< ...

  8. 关于职位的解释---转CSDN的文章

    摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不胜! 通用法则 法则1:忍耐是 ...

  9. BroadcastReceiver接收系统广播消息

    Android常用的广播Action常量: ACTION_TIME_CHANGED:系统时间被改变. ACTION_DATE_CHANGED:系统日期被改变. ACTION_TIMEZONE_CHAN ...

  10. BroadcastReceiver的简介

    BroadcastReceiver本质上属于一个监听器,因此实现BroadcastReceiver的方法只要重写BroadcastReceiver的onReceive(Context  context ...