【题目链接】

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

【算法】

深度优先搜索

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std; struct info
{
int x,y;
} pos[]; int i,j,len,T;
char mp[][];
bool row[][],col[][],grid[][];
bool solved; inline int getpos(int x,int y)
{
return (x - ) / * + (y - ) / + ;
}
inline void dfs(int dep)
{
int i;
if (dep > len)
{
solved = true;
return;
}
for (i = ; i <= ; i++)
{
if (!row[pos[dep].x][i] && !col[pos[dep].y][i] && !grid[getpos(pos[dep].x,pos[dep].y)][i])
{
mp[pos[dep].x][pos[dep].y] = i + '';
row[pos[dep].x][i] = true;
col[pos[dep].y][i] = true;
grid[getpos(pos[dep].x,pos[dep].y)][i] = true;
dfs(dep+);
if (solved) return;
row[pos[dep].x][i] = false;
col[pos[dep].y][i] = false;
grid[getpos(pos[dep].x,pos[dep].y)][i] = false;
}
}
} int main()
{ scanf("%d",&T);
getchar();
while (T--)
{
solved = false;
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
len = ;
for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
mp[i][j] = getchar();
if (mp[i][j] == '') pos[++len] = (info){i,j};
else
{
row[i][mp[i][j]-''] = true;
col[j][mp[i][j]-''] = true;
grid[getpos(i,j)][mp[i][j]-''] = true;
}
}
getchar();
}
dfs();
for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
printf("%c",mp[i][j]);
}
printf("\n");
}
} return ; }

【POJ 2676】 Sudoku的更多相关文章

  1. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  2. 【POJ 3076】 Sudoku

    [题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...

  3. 【POJ 3074】 Sudoku

    [题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...

  4. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  5. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  6. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  7. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  8. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  9. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

随机推荐

  1. java_File对象

    package File; import java.io.File; import java.io.IOException; public class file { public static voi ...

  2. centos 设置 ip地址

    linux设置ip,主要是修改/etc/sysconfig/network-scripts/ifcfg-** 里面的网卡配置文件,然后命令 service network restart 生效 自动获 ...

  3. Django - 路由对应关系

    1.对url路由关系命名,以后可以根据此名称,生成自己想要的url urls.py中: url('^fdsaafdf(\d+)/',views.index,name='indexx') url('^f ...

  4. OprenCV学习之路一:将彩色图片转换成灰度图

    //将一张彩色图片转成灰度图: //////////////////////////// #include<cv.h> #include<cvaux.h> #include&l ...

  5. JDK5-8特性归纳

    jdk5新特性1.自动装箱和拆箱2.枚举3.静态导入4.可变参数5.內省   是Java语言对Bean类属性.事件的一种缺省处理方法.例如类A中有属性那么,那我们可以通过getName,setName ...

  6. Java基础学习总结(36)——Java注释模板

    代码注释是对代码设计者.代码阅读者以及系统间调用提供了有效的帮助,最大限度的提高团队开发合作效率增强系统的可维护性.我们追求简化,不是为了写注释而写注释. (快速使用请直接看六.七.八) 一.原则: ...

  7. 清北学堂模拟赛d6t4 数组异或

    分析:直接O(n^3)做是只有50分的,可以加一点小小的优化,就是c[k]可以从c[k-1]得到,但是还是只有60分,从宏观意义上是不能继续优化了.对于这类涉及到位运算的性质的题目,将每个数转化成二进 ...

  8. github在README中展示demo

    2017年更新,现在github再master分支直接就可以操作,所以我改了这篇文章   问题所在? 解决办法? 博主建议? 一:问题的所在 相信很多小胖友们在把自己的网页上传到github仓库中,都 ...

  9. 洛谷 P3496 [POI2010]GIL-Guilds

    P3496 [POI2010]GIL-Guilds 题目描述 King Byteasar faces a serious matter. Two competing trade organisatio ...

  10. iOS:如何让xib同时兼容支持iOS6和iOS7

    做法如下: 1. 取消xib的Use Autolayout属性的勾选: 2. 将xib中每一个子控件(没错,每一个控件,包括UIButton.UILabel等等),将它们的Y Frame的值全部增加2 ...