思路:记录每行每列每一个宫已经出现的数字就可以。数据比較弱

另外POJ 3074 3076 必须用剪枝策略。但实现较麻烦,还是以后学了DLX再来做吧

//Accepted	160K	0MS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N =15;
char sudo[N][N];
bool visr[N][N],visc[N][N],visg[N][N];
int pos[N][N];
bool flag;
void print()
{
for(int i=1;i<=9;i++)
printf("%s\n",sudo[i]+1);
}
void dfs(int x,int y)
{
if(y==10)
{
dfs(x+1,1);
return ;
}
if(x==10)
{
print();
flag=1;
return ;
}
if(sudo[x][y]!='0')
{
dfs(x,y+1);
return ;
}
for(int i=1;i<=9&&!flag;i++)
{
if(visr[x][i]==0&&visc[y][i]==0&&visg[pos[x][y]][i]==0)
{
sudo[x][y]='0'+i;
visc[y][i]=visr[x][i]=visg[pos[x][y]][i]=1;
dfs(x,y+1);
sudo[x][y]='0';
visc[y][i]=visr[x][i]=visg[pos[x][y]][i]=0;
}
}
}
void ini()
{
flag = false ;
memset(visr,0,sizeof(visr));
memset(visc,0,sizeof(visc));
memset(visg,0,sizeof(visg));
}
int main()
{
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
pos[i][j]=((j-1)/3+1)+3*((i-1)/3); int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
printf("Scenario #%d:\n",cas);
ini();
for(int i=1;i<=9;i++)
scanf("%s",sudo[i]+1);
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
{
int val=sudo[i][j]-'0';
visr[i][val]=true;
visc[j][val]=true;
visg[pos[i][j]][val]=true;
}
dfs(1,1);
puts("");
}
return 0;
}

POJ 2676/2918 数独(dfs)的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

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

  2. 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 ...

  3. POJ 2676 Sudoku (DFS)

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

  4. ACM : POJ 2676 SudoKu DFS - 数独

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

  5. 随手练——POJ - 2676 数独 (回溯法)

    POJ - 2676 : http://poj.org/problem?id=2676: 解题思想 (大力出奇迹): 1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行.列,以及3 ...

  6. 深搜+回溯 POJ 2676 Sudoku

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

  7. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

  8. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

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

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

随机推荐

  1. EasyUI - Tabs 选项卡标签

    基本效果: 效果图: html代码: <div id="tab"> <div title="tab1" > <p>tab1& ...

  2. delphi中左右翻转窗体(修改EXStyle)

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  3. jquery如何在加载完iframe的内容后才进行下一步操作

    为iframe添加onload事件 ie使用attachEvent("onload",function(){}) firefox.chrome使用addEventListener( ...

  4. Andorid Binder进程间通信---总结

    一.Server和Service Manager进程间通信 Service Manager进程启动时,已经创建了Service Manager实体对象,没有Service Manager本地对象. S ...

  5. ASP.NET - cookie

    下面是写cookie  HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项  DateTime dt ...

  6. Custom draw 和 Owner draw 的区别

    "Custom Draw" is a feature shared by all of Microsoft's common controls, which allows you ...

  7. VC 获取指定文件夹路径的方法小结

    VC获取指定文件夹路径 flyfish  2010-3-5 一 使用Shell函数 1 获取应用程序的安装路径 TCHAR buf[_MAX_PATH];SHGetSpecialFolderPath( ...

  8. C#多线程实现方法——Task/Task.Factary

    原文:C#多线程实现方法--Task/Task.Factary Task 使用 Task以及Task.Factory都是在.Net 4引用的.Task跟Thread很类似,通过下面例子可以看到. st ...

  9. Goffi and Squary Partition

    题意: 给你N和K,问能否将N拆分成K个互不相同的正整数,并且其中K-1个数的和为完全平方数. PS:这道题目原来是要求输出一种可行方案的,所以下面题解是按照输出方案的思想搞的. 分析: 我们尝试枚举 ...

  10. Swift - 协议(protocol)

    1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...