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. 关于JVM的类型和模式

    原文出处: 摆渡者 引言 曾几何时,我也敲打过无数次这样的命令: 然而之前的我都只关心过版本号,也就是第一行的内容.今天,我们就来看看第3行输出的内容:JVM的类型和工作模式. 其实说Server和C ...

  2. java 三大框架 介绍

    三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  3. hdu----(2848)Repository(trie树变形)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. nyoj------布线问题(kruscal+求最小值)

    布线问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...

  5. iOS项目的目录结构和开发流程

    转自无网不剩的博客 网上相关的资源不多,开源的且质量还不错的iOS项目也是少之又少,最近正好跟同事合作了一个iOS项目,来说说自己的一些想法.   目录结构 AppDelegate Models Ma ...

  6. groovy基础

    字符串字面值 def age=25 log.info 'My age is ${age}' log.info "my age is \${age}" log.info " ...

  7. Eclipse添加jsp页面后引入java指令报错解决方法

    新建jsp页面老提示: Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpS ...

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

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

  9. MYSQL 日期函数【转】

    MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...

  10. SSH由WAS/Tomcat/Weblogic迁移到JBOSS

    又是一个凌晨,又一次搞项目在新的中间件上的可部署性验证... 原来将项目部署到was7上,花了三个晚上到凌晨1点多的时间,总结出了只要将common-logging和wodenxx.jar两个jar包 ...