POJ 3076 Sudoku
思路:
dfs + 剪枝
首先,如果这个位置只能填一种字母,那就直接填
其次,如果对于每一种字母,如果某一列或者某一行或者某一块只能填它,那就填它
然后,对于某个位置如果不能填字母了,或者某种字母在一行一列或一块中出向了两次以上,说明当前方案不成立
最后贪心地从可选情况少的往下搜
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = ;
int mp[N][N];
int st[N][N];
int sum = ;
char s[N][N+];
void add(int x, int y, int t) {
mp[x][y] = t;
sum++;
for (int i = ; i <= ; i++) {
st[i][y] |= <<t-;
st[x][i] |= <<t-;
}
int xx = (x+)/, yy = (y+)/;
for (int i = (xx-)* + ; i <= xx*; i++) {
for (int j = (yy-)* + ; j <= yy*; j++) {
st[i][j] |= <<t-;
}
}
}
void print() {
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
putchar(mp[i][j]-+'A');
}
puts("");
}
puts("");
}
bool dfs() {
if(sum == ) {
print();
return true;
} for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if(!mp[i][j]) {
int cnt = , t = ;
for (int k = ; k <= ; k++) {
if((st[i][j] & (<<k-)) == ) {
cnt++;
t = k;
if(cnt == ) break;
}
}
if(!cnt) return false;
if(cnt == ) add(i, j, t);
}
}
} for (int i = ; i <= ; i++) {
for (int k = ; k <= ; k++) {
int cnt1 = , cnt2 = , y;
for (int j = ; j <= ; j++) {
if(mp[i][j] == k) cnt1++;
if(cnt1 == ) return false;
if(!mp[i][j] && (st[i][j] & (<<k-)) == ) cnt2++, y = j;
}
if(!cnt1 && !cnt2) return false;
if(!cnt1 && cnt2 == ) add(i, y, k);
}
} for (int j = ; j <= ; j++) {
for (int k = ; k <= ; k++) {
int cnt1 = , cnt2 = , x;
for (int i = ; i <= ; i++) {
if(mp[i][j] == k) cnt1++;
if(cnt1 == ) return false;
if(!mp[i][j] && (st[i][j] & (<<k-)) == ) cnt2++, x = i;
}
if(!cnt1 && !cnt2) return false;
if(!cnt1 && cnt2 == ) add(x, j, k);
}
} for (int i = ; i <= ; i++) {
int x = (i+)/, y = i - (x-)*;
for (int k = ; k <= ; k++) {
int cnt1 = , cnt2 = , xx, yy;
for (int ii = (x-)*+; ii <= x*; ii++) {
for (int jj = (y-)*+; jj <= y*; jj++) {
if(mp[ii][jj] == k) cnt1++;
if(cnt1 == ) return false;
if(!mp[ii][jj] && (st[ii][jj] & (<<k-)) == ) cnt2++, xx = ii, yy = jj;
}
}
if(!cnt1 && !cnt2) return false;
if(!cnt1 && cnt2 == ) add(xx, yy, k);
}
}
if(sum == ) {
print();
return true;
} int mn = N, x, y;
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if(!mp[i][j]) {
int cnt = ;
for (int k = ; k <= ; k++) {
if((st[i][j] & (<<k-)) == ) {
cnt++;
if(cnt >= mn) break;
}
}
if(cnt < mn) {
mn = cnt;
x = i;
y = j;
}
}
}
}
int tst[N][N], tmp[N][N];
memcpy(tst, st, sizeof(st));
memcpy(tmp, mp, sizeof(mp));
int tsum = sum; for (int k = ; k <= ; k++) {
if((st[x][y] & (<<k-)) == ) {
add(x, y, k);
bool f = dfs();
if(!f) {
memcpy(st, tst, sizeof(tst));
memcpy(mp, tmp, sizeof(tmp));
sum = tsum;
}
else return true;
}
}
return false;
}
int main() {
while(scanf("%s", s[]+) != EOF){
for (int i = ; i <= ; i++) {
scanf("%s", s[i]+);
}
sum = ;
mem(mp, );
mem(st, );
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if(isalpha(s[i][j])) add(i, j, s[i][j] - 'A' + );
}
}
dfs();
}
return ;
}
/*
--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-
*/
POJ 3076 Sudoku的更多相关文章
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- POJ 3076 Sudoku (dancing links)
题目大意: 16*16的数独. 思路分析: 多说无益. 想说的就是dancing links 的行是依照 第一行第一列填 1 第一行第二列填 2 -- 第一行第十五列填15 第一行第二列填 1 -- ...
- 搜索(DLX): POJ 3074 3076 Sudoku
POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- 【POJ】3076 Sudoku
DLX第一题,模板留念. /* 3076 */ #include <iostream> #include <string> #include <map> #incl ...
- Sudoku POJ - 3076
Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 5769 Accepted: 2684 Descripti ...
随机推荐
- Golang字符串格式化
Go对字符串格式化提供了良好的支持.下面我们看些常用的字符串格式化的例子. package main import ( "fmt" "os" ) type po ...
- Docker 微服务教程(搭建真正的网站)
Docker 是一个容器工具,提供虚拟环境.很多人认为,它改变了我们对软件的认识. 站在 Docker 的角度,软件就是容器的组合:业务逻辑容器.数据库容器.储存容器.队列容器......Docker ...
- sql xml 入门 (二)
DECLARE @myDoc xml --http://www.paymob.cn --话费充值api,充值api,话费充值接口,手机话费充值,车贝手机,贝萌手机,移动话费充值,联通话费充值,电信话费 ...
- k8s device plugin
基本概念入门: Device Manager Proposal Device plugin offical Doc(中文) device-plugins offical Doc(En) Go thro ...
- 利用shell脚本通过ssh绕过输入密码直接登录主机
shell #!/usr/bin/expect spawn ssh root@192.168.137.141 expect "*password:" send "lizh ...
- 安装搭建Python2.* 和3.* 环境详细步骤
Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上. 安装Python 首先进入Python官方网站,将Python下载下来. win7安装python 在官网 ...
- centos7之安装wordpress
wordpress安装教程如下: mysql安装可以参考我的博客园Centos构建Java环境:https://www.cnblogs.com/youcong/p/9118753.html 1.安装a ...
- docker-compose安装与部署项目
安装: 1.curl安装慢的问题 解决:改用pip安装,需要先安装pip相关,参照: https://www.cnblogs.com/YatHo/p/7815400.html 2.pip安装依赖库re ...
- nginx 配置 https 请求
1,先去这个网站申请一下证书 https://certmall.trustauth.cn/Home/Member/index/id/1521167511.html 上面会教你怎么去做. 2,就是配置自 ...
- 论文笔记:ReNet: A Recurrent Neural Network Based Alternative to Convolutional Networks
ReNet: A Recurrent Neural Network Based Alternative to Convolutional Networks2018-03-05 11:13:05 ...