【题目链接】

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. hibernate 级联删除报更新失败的问题(org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)

    首先hibernate级联删除的前提是,首先需要在映射文件中配置,配置多表之间的关联关系: 下面以部门表(Dept)和员工表(Emp)为例: 1.在Emp.hbm.xml映射文件中配置many-to- ...

  2. JS——冒泡案例

    模态框 1.因为a链接和和顶级document都注册了单击事件,所以要阻止a链接向父级盒子冒泡,不然又会从document的单击事件走一遍 2.在document的单击事件中,只需要判断触发事件的目标 ...

  3. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...

  4. Python语言之数据结构1(序列--列表,元组,字符串)

    0.序列 列表,元组,字符串都是序列. 序列有两个特点:索引操作符和切片操作符.索引操作符让我们可以从序列中抓取一个特定项目.切片操作符让我们能够获取序列的一个切片,即一部分序列. 以字符串为例: 1 ...

  5. Nginx 重新加载日志配置

    最近在写一个nginx日志的切割脚本,切割完后,发现可以不重启服务,而直接重新加载日志配置文件的命令 [   kill -USR1 $nginx.pid   ],但是不知道 -USR1这个参数是什么意 ...

  6. HDU_1068_Girls and Boys_二分图匹配

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. js 滚动条

    <script type="text/javascript"> //文档高度1016 包含隐藏的margin和padding 实际1000 //文档1000 //窗口高 ...

  8. 如何从源码启动和编译IoTSharp

    IoTSharp 项目是一个开源物联网平台,数据库使用PostgreSQL , 后端使用 Asp.Net Core 2.2 ,前端使用  vue-element-admin , 下面我们介绍如何启动项 ...

  9. 最基础的rpm命令

    rpm -ivh package.rpm 安装一个rpm包 rpm -ivh --nodeeps package.rpm 安装一个rpm包而忽略依赖关系警告 rpm -U package.rpm 更新 ...

  10. c/c++排坑(2) -- c语言中的符号重载

    所谓的符号重载就是在不同的上下文环境里有不同的意义.甚至有些关键字也被重载而具有好几种意义,这也是C语言的作用域规则对程序员不那么清晰的主要原因. 本章内容摘自<c专家编程>P37. 大家 ...