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. Scrum 项目5.0

    1.团队成员完成自己认领的任务. 2.燃尽图:理解.设计并画出本次Sprint的燃尽图的理想线.参考图6. 3.每日立会更新任务板上任务完成情况.燃尽图的实际线,分析项目进度是否在正轨.    每天的 ...

  2. JavaWeb基础: 学习大纲

    JavaWeb基础: Web应用和Web服务器 JavaWeb基础: Tomcat JavaWeb基础:HTTP协议和基于Restful的架构 JavaWeb基础: Web工程配置文件 JavaWeb ...

  3. Jquery元素追加和删除

    原文链接:http://www.cnblogs.com/william-lin/archive/2012/08/12/2635402.html 介绍    DOM是Document Object Mo ...

  4. cms3.0——收获(1)

    或许是由于各个公司的情况不同,使得每次写后台管理系统就沿用之前的nodejs中的thinkjs来写后台管理系统,也是因为这样后期维护起来更加方便吧?不过最早之前的项目,却有一个使用的是nodejs 中 ...

  5. ES mlockall作用——preventing that memory from being paged to the swap area

    elasticsearch还有一个重要的参数bootstrap.mlockall,这个参数的目的是当你无法关闭系统的swap的时候,建议把这个参数设为true.防止在内存不够用的时候,elastics ...

  6. js刷新页面和跳转

    javascript返回上一页: 1.返回上一页 history.go(-1); 返回上两个页面 history.go(-2); <a href="javascript:history ...

  7. formvalidator4.1.3 使用过程中一些问题的解决

    在使用formvalidator4.1.3 插件时  发现 正常情况调用时没有问题的,但是我们的项目中需要把css  .js 之类的静态资源用单独的域名加载. 那么问题就来了在 该插件中 有一段这样的 ...

  8. C#入门篇6-11:字符串操作 查找与替换

    #region 查找与替换 public class C4 { //查找 public static void StrFind() { //目标字符串 string str1 = "~awe ...

  9. UVALive 6680 Join the Conversation

    题意:conversion的定义是下一句提到上一句的人的名字.请你输出最长的对话的长度,及组成对话的序列号. 思路:动态规划的思想很容易想到,当前句子,根据所有提到的人的名字为结尾组成的对话长度来判断 ...

  10. webApi跨域

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...