dfs 用的还是不行啊,做题还是得看别人的博客!!!

题目:http://poj.org/problem?id=2676

题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。

0是待填位置,其他均为已填入的数字。

要求填完九宫格并输出(如果有多种结果,则只需输出其中一种)

如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格

参考博客:http://www.cnblogs.com/tanhehe/archive/2013/08/07/3243073.html

http://blog.csdn.net/lyy289065406/article/details/6647977

r、c、g数组用来剪枝

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; struct node
{
int x,y;
}q[];
int r[][],c[][],g[][][];
int G[][],cnt; int dfs(int cn) //表示dfs 递归还是不太会
{
int x,y;
if(cn<) return ;
x=q[cn].x; y=q[cn].y;
for(int k=; k<=; k++)
{
if(r[x][k] || c[y][k] || g[x/][y/][k]) continue;
r[x][k]=c[y][k]=g[x/][y/][k]=;
G[x][y]=k;
if(dfs(cn-)) return ;
r[x][k]=c[y][k]=g[x/][y/][k]=;
}
return ;
}
int main()
{
int t,k;
cin>>t;
while(t--)
{
cnt=;
memset(r,,sizeof(r));
memset(c,,sizeof(c));
memset(g,,sizeof(g));
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
scanf("%1d",&G[i][j]); //每次输入一个整数
k=G[i][j];
if(k!=)
r[i][k]=c[j][k]=g[i/][j/][k]=; //g数组,每个子网格里是一样的
else
q[cnt++]=(node){i,j}; //赋值到结构体里,学习了
}
}
dfs(cnt-);
for(int i=; i<; i++)
{
for(int j=; j<; j++)
cout<<G[i][j];
printf("\n");
}
}
return ;
}

poj 2676 Sudoku ( dfs )的更多相关文章

  1. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  2. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  3. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  4. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  5. POJ 2676 Sudoku (DFS)

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

  6. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  7. DFS POJ 2676 Sudoku

    题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...

  8. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  9. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

随机推荐

  1. html 模板 swig 预编译插件 grunt-swig-precompile

    GitHub grunt-swig-precompile NPM grunt-swig-precompile 在书写前端静态页面的时候,每个页面总在书写很多重复的标签. 为了提高效率,结合 swig. ...

  2. RHEL 7.2 安装Oracle XE-11.2.0

    轻量快捷版本,适合开发 0. /etc/hosts 添加 本机hostname # hostnamepromote.cache-dns.local # cat /etc/hosts127.0.0.1 ...

  3. 如何利用VS2010安装和部署应用程序

    转自:http://jingyan.baidu.com/article/4b52d70255d7f0fc5d774b4d.html 1.假设你当前的winform已经okay了 2.解决方案中新建&q ...

  4. php入门变量之字符串

    字符串只是一块用引号括起来的字符:字母.数字.空格.标点符号,等等. 下面列出的全都是字符串: 'Huige' "In watermelon sugar" '100' 'Augus ...

  5. cxgrid GridMode 等于 True 时的一些问题。

    When using grid mode, the data controller loads a fixed number of dataset records into memory. The n ...

  6. 1058 A+B in Hogwarts (20)

    #include <stdio.h> int main() { ]; ]; ],&ans1[],&ans1[],&ans2[],&ans2[],&a ...

  7. zxing-master core编译

    1.下载 Maven: http://maven.apache.org/download.cgi 2.下载完以后,设一下系统变量:MAVE_HOME 3.Path中加入%MAVEN_HOME%\bin ...

  8. seo 优化 仅针对 来拍呀www.laipaiya.com(一)

    加入百度统计代码:http://tongji.baidu.com/ 查看百度统计优化分析->seo建议 对每个页面的meta标签做修改 首页 title :来拍呀 - | 折扣好房你就来拍呀 k ...

  9. vim中编写python代码使用python-mode和syntastic插件时警告(Warning)的消除

    问题: 在Vim使用了syntastic后,编写代码时,可以对代码错误和警告进行相对实时的了解,对编写代码有很大的帮助.同时这个插件和python-mode一起工作时,可以对python代码的编写提供 ...

  10. swift基础--运算符

    (1)加减乘除 (2)三目运算,切记后面的空格如果不加会报错的.估计是苹果的bug. (3)聚合运算符,省却了一个判断,很人性化 (4)区间运算符 //加减乘除等等 let a = 2 let b = ...