做了很久还是参考了别人的答案orz,其实也不难啊。我要开始学一下怎么写搜索了。。。

题目链接:poj2676 Sudoku

题解:暴力搜索,DFS每个空白格子所放数字。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
bool row_f[][];//row_f[i][j]=1表示第i行已经放了数字j
bool col_f[][];
bool block_f[][];
int g[][];
struct blank{
int i, j;
blank(int i, int j):i(i),j(j) {}
};
vector<blank> bk;
int get_blockid(int i, int j){
i /= ;
j /= ;
return i * + j;
}
bool ok(int i, int j, int x){
return !row_f[i][x] && !col_f[j][x] && !block_f[get_blockid(i, j)][x];
}
void setnum(int i, int j, int x, int f){
row_f[i][x] = f;
col_f[j][x] = f;
block_f[get_blockid(i, j)][x] = f;
}
bool dfs(int n){//处理前n个空白格
if(n < ) return true;
int r = bk[n].i;
int c = bk[n].j;
for(int x = ; x <= ; ++x){
if(ok(r, c, x)){
setnum(r, c, x, );
g[r][c] = x;
if(dfs(n - )) return true;
setnum(r , c, x, );
}
}
return false;
}
int main(){
int t, i, j;
char c;
scanf("%d", &t);
while(t--){
bk.clear();
memset(row_f, , sizeof(row_f));
memset(col_f, , sizeof(col_f));
memset(block_f, , sizeof(block_f));
for(i = ; i < ; ++i){
for(j = ;j < ; ++j){
cin >> c;
g[i][j] = c - '';
if(!g[i][j])
bk.push_back(blank(i, j));
else
setnum(i, j, g[i][j], );
}
}
if(dfs(bk.size()-)){
for(i = ; i < ; ++i){
for(j = ; j < ; ++j)
printf("%c", g[i][j] + '');
printf("\n");
}
}
}
return ;
}

poj2676 Sudoku(DFS)的更多相关文章

  1. POJ2676 – Sudoku(数独)—DFS

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24081   Accepted: 11242   Specia ...

  2. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

  3. poj2676 Sudoku(搜索)

    题目链接:http://poj.org/problem?id=2676 题意:9*9的方格,0代表没数字,其他代表数字,请在格子中填入1~9的数字,使得在每行,每列和每个3*3的方块中,1~9的数字每 ...

  4. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. 12.Generics

    benifit: 1.make developers extremely productive is code reuse, which is the ability to derive a clas ...

  2. 9.Methods(二)

    4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...

  3. 虚拟机guest为windows7的环境下安装破解版simplify3d_3.0.2

    情形: 1.主机(host):ubuntu 2.虚拟机里安装的操作系统版本(guest):windows 7专业版 3.simplify3d破解版版本:3.0.2(破解需要的工具均在下文的百度云地址里 ...

  4. SQL collate

    摘自:http://www.cnblogs.com/window5549-accp/archive/2009/10/03/1577682.html 我们在create table时经常会碰到这样的语句 ...

  5. uploads 上传图片

    public static function upFile($r,$p='../images/link/',$type='gif,jpg,png',$named=0){ $newnames = nul ...

  6. MVC加载下拉列表

    public ActionResult GetList() { string strSql = "select staff_id,nick_name from tbl_ac_info whe ...

  7. sgu-508 Black-white balls 概率-贝叶斯公式

    题意:有n个球,其中有0.1.2...n个黑球的概率是相等的,现在从中取出L个球,p个黑球q个白球.现在问猜一个黑球的区间,使得落在这个区间的概率大于给定的一个数值. 详见代码: #include & ...

  8. hanio 塔和递规的理解。

    //递规很好理解,但是初看hanoi的时候,总没有理所当然的感觉.//那应该是对递规根本还没理解吧.仔细想了下.有点总结. 后来翻到 <<数据结构>> 112页,原来hanio ...

  9. [转载] DevOps年中盘点:国外最受欢迎的10篇技术文章

    本文根据高效运维系列微信群的群友投稿整理而成.“高效运维”公众号作为本系列群的官方唯一公众号,原创并独家首发. 欢迎关注“高效运维”公众号,以免费参加「运维讲坛」每月一次的线下交流活动:并抢先赏阅干货 ...

  10. dateTimePicker的使用,时间控件

    <li> <label>促销时间<span class="imprt">*</span></label> <inp ...