题意:完成数独程序,数独要求每行每列且每个3*3矩阵都必须是1~9的数字组成。

思路:dfs

  1. 用row[i][n] 记录第i行n存在  用col[j][n] 记录第j列n存在 grid[k][n] 记录第k个3*3中的n存在
  2. 遍历的时候,先把列遍历完然后在遍历行
if (map[r][c])
{
if (c == ) flag = dfs(r + , );
else flag = dfs(r, c + );
return flag;
}

现在推

第一个矩阵为  0,0 0,1 0,2              第二个矩阵为   0,3 0,4 0,5

1,0 1,1 1,2                                       1,3  1,4 1,5

2,0 2,1 2,2                                       2,3 2,4 2,5

如果直接 (r+c)/3有些不符合,说明应该把 r c拆开来看 所以推出 r/3*3+c/3

解决问题的代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
int map[maxn][maxn];
bool row[maxn][maxn];
bool col[maxn][maxn];
bool grid[maxn][maxn];
bool dfs(int r, int c)
{
if (r == ) return true;//构造完毕
bool flag = false;
if (map[r][c])
{
if (c == ) flag = dfs(r + , );
else flag = dfs(r, c + );
return flag;
}
int k = (r / ) * + c / ;
for (int i = ; i <= ; i++)if (!row[r][i] && !col[c][i] && !grid[k][i])
{
row[r][i] = col[c][i] = grid[k][i] = true;
map[r][c] = i;
if (c == ) flag = dfs(r + , );
else flag = dfs(r, c + );
if (flag) return true;
map[r][c] = ;
row[r][i] = col[c][i] = grid[k][i] = false;
}
return false;
}
int main()
{
int T; scanf("%d", &T);
while (T--)
{
memset(row, , sizeof(row));
memset(col, , sizeof(col));
memset(grid, , sizeof(grid));
for (int i = ; i<; i++)
for (int j = ; j<; j++)
{
char x;
scanf(" %c", &x);
map[i][j] = x - '';
if (map[i][j])
{
row[i][map[i][j]] = true;
col[j][map[i][j]] = true;
int k = (i / ) * + j / ;
grid[k][map[i][j]] = true;
}
}
dfs(, );
for (int i = ; i<; i++)
{
for (int j = ; j<; j++)
printf("%d", map[i][j]);
printf("\n");
}
}
return ;
}

poj 2676 数独问题 dfs的更多相关文章

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

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

  2. POJ 2676 数独(DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21612   Accepted: 10274   Specia ...

  3. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  4. POJ 2676 Sudoku (DFS)

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

  5. ACM : POJ 2676 SudoKu DFS - 数独

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

  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)

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

随机推荐

  1. Python 踩坑之旅进程篇其四一次性踩透 uid euid suid gid egid sgid的坑坑洼洼

    目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 菜 ...

  2. SPRING代理模式

    1.静态代理 主题对象:Student public interface Student { public String add(); } 目标对象:RealStudent public class ...

  3. EF5 通用数据层 增删改查操作,泛型类

    using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...

  4. vue2.0:(六)、移动端像素border的实现和整合引入less文件

    知识点一.如何在手机上看我们制作的移动端页面. 正常我们在电脑上都是按如下图来制作手机页面的: 如果要在手机上面看就不能用localhost了.所以,进入命令行,输入ipconfig查看本地ip地址: ...

  5. Git 连接远程仓库Github

    创建SSH Key. 在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步. 如果没有,打开Shell(W ...

  6. springBoot jpa 表单关联查询

    1.创建两个实体类 import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.per ...

  7. python+selenium之处理alert弹出对话框

    注:本篇文章转载 http://www.cnblogs.com/mengyu/p/6952774.html 在完成某些操作时会弹出对话框来提示,主要分为"警告消息框"," ...

  8. C# 使用解析json 嵌套方法

    C#从网页不传参数 接收json数据 public String GetHtmlFromUrl(String url) { //Response.Write(url); //Response.End( ...

  9. 对卷积(convolution)的理解

    参考文章 https://www.jianshu.com/p/daaaeb718aed https://blog.csdn.net/bitcarmanlee/article/details/54729 ...

  10. 数组Array和字符串String的indexOf方法,以及ES7(ES2016)中新增的Array.prototype.includes方法

    前言 我们在判断某一个字符是否存在于一个字符串中或者某一个值是否存在于一个数组中时,ES7之前我们需要使用indexOf,ES7引入了新的方法includes 语法 数组:Array.inexOf(s ...