【POJ 2676】 Sudoku
【题目链接】
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的更多相关文章
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- 【POJ 3074】 Sudoku
[题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
随机推荐
- echarts交叉关系图一
想要做一个公司-人员关系图,官网echarts图graph webkit dep 稍微改了一下, 也是有点恶心自己,调了一个数据最多的去改,如果正好有人需要就不用去改了 说明:此图没有坐标,可以设置图 ...
- [Windows Server 2003] 安装网站伪静态
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS伪静 ...
- [源码阅读]RocketMQ-策略篇
一:为什么要阅读rocketmq的源码? 1 可以了解mq的底层实现逻辑. 二:打算怎么读,行动路径是哪儿些? 1: 本地启动 2 分步调试 3 fork项目,添加中文注释,提交到自己的代码库.并改 ...
- async await 同步方法调用异步方法死锁
同步方法调用异步方法.GetAwaiter().GetResult()计算函数超时,异步方法所有的回调操作都会期望返回到主线程. 所以会导致各种线程死锁.异步方法中使用ConfigureAwait(f ...
- which
功能说明:显示命令的全路径. 参数选项: -a 默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项. 参数-a把所有匹配命令路 ...
- webpack核心概念使用的综合小案例
注: 由于版本更新很快,同样的配置不同版本很可能会出错(这个就很绝望了) 解决思路 看文档 查看源码接口 网上搜索相应错误 环境 webpack4.x + yarn 文件结构 . ├── dist / ...
- Lua操作符的优先级
lua 操作符的优先级 lua操作符的优先级 如表所示(从高到低) 在二元操作符中,除了指数操作符 “^” 和连接操作符 “..” 是 ”右结合” 的,是所有其他操作符都是 “左结合” 的.因此如 ...
- SDOI2018退役记
在NOIp2017中,我意识到自己啥也不会.如今SDOI2018快来了,自己还是啥也不会.高一两次考试注定以打两次酱油告终.还是记录一下,到NOIp之后如果还没有退役的话,那这个博客可能还会继续更新吧 ...
- HDU 1465(错排公式)
不容易系列之一 题意: 一个人要寄n个信封,结果装错了.信纸的编号为1到n,信封的编号为1到n,信纸的编号不能和信封的编号一样,全都不能一样. 思路:错排公式. D(n)表示n件信封装错的所有的情况. ...
- PAT 1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...