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. struts2在pom.xml中的配置

    <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifac ...

  2. 数据类型转换的三种方式 Convert,parse和TryParse的解析

    以Int类型为例,具体说明Convert.ToInt32(object value),int.Parse(object value)和int.TryParse(string s,out int res ...

  3. java二维数组简单初步理解

    二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr[0][ ...

  4. css——子代与后代选择器

    一直都以为,子代选择器与后代选择器作用是一样的,都是选择一个元素下面的子元素,直到今天才明白: 1.子代选择器(用<连接):ul>li 子选择器仅仅选择ul包围的 子元素 中的 li元素, ...

  5. ubuntu14.10设置开机启动服务

    1.比如lampp其他的都类似: 我是这么操作:(屌丝初学者) a.把lampp启动程序放到/etc/bin下面 b.vi /etc/rc.local ,加入lampp start(有了第一步就可以这 ...

  6. 使用Web Service进行网络编程-----Web Service简介

    Android应用通常都是运行在手机平台上,手机系统的硬件资源是有限的,不管是存储能力还是计算能力都是有限的,在Android系统上开发.运行一些单用户.小型应用是可能的,但对于需要进行大量的数据处理 ...

  7. java成员变量与局部变量修饰符的区别

    成员变量: 可以被 public,static ,protected,default,final修饰. 局部变量:包括方法里的和 代码块里的(静态和非静态) 可以被default, final修饰 参 ...

  8. hdu 4638 Group

    http://acm.hdu.edu.cn/showproblem.php?pid=4638 问题其实就是求[L,R]中有多少个连续的段 若每一个人都是一个段 那么[L,R]中每一个朋友关系就会减少一 ...

  9. hdu 4616 Game

    http://acm.hdu.edu.cn/showproblem.php?pid=4616 要记录各种状态的段  a[2][4] a[0][j]表示以trap为起点一共有j个trap的最优值 a[1 ...

  10. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...