数独求解问题(DFS+位运算优化)
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,
| . | 2 | 7 | 3 | 8 | . | . | 1 | . |
| . | 1 | . | . | . | 6 | 7 | 3 | 5 |
| . | . | . | . | . | . | . | 2 | 9 |
| 3 | . | 5 | 6 | 9 | 2 | . | 8 | . |
| . | . | . | . | . | . | . | . | . |
| . | 6 | . | 1 | 7 | 4 | 5 | . | 3 |
| 6 | 4 | . | . | . | . | . | . | . |
| 9 | 5 | 1 | 8 | . | . | . | 7 | . |
| . | 8 | . | . | 6 | 5 | 3 | 4 | . |
Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.
Input
The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.
Output
For each test case, print a line representing the completed Sudoku puzzle.
Sample Input
.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.
......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.
end
Sample Output
527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
思路:DFS解这道题有点极限,需要优化的东西比较复杂,参考了网上的位运算优化,终于以700ms的解决了,如果不位运算优化很难去不超时的去解这道问题
附上原超时代码,此代码修改一下应该可以过F题,单纯的求数独的题
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005
typedef long long ll;
using namespace std;
int cnt,flag;
int Map[10][10],vistrow[10][10],vistcol[10][10],vistkaui[10][10][10];
struct node
{
int x,y;
}a[100];
void DFS(int x)
{
if(x == cnt)
{
flag = 1;
return;
}
int I = a[x].x;
int J = a[x].y;
for(int i = 1; i < 10; i++)
{
if(!vistrow[I][i] && !vistcol[J][i] && !vistkaui[I/3][J/3][i])
{
Map[I][J] = i;
vistrow[I][i] = 1;
vistcol[J][i] = 1;
vistkaui[I/3][J/3][i] = 1;
DFS(x+1);
if(!flag)
{
Map[I][J] = '.';
vistrow[I][i] = 0;
vistcol[J][i] = 0;
vistkaui[I/3][J/3][i] = 0;
}
}
}
}
int main()
{
int T;
while(1)
{
memset(vistrow,0,sizeof(vistrow));
memset(vistcol,0,sizeof(vistcol));
memset(vistkaui,0,sizeof(vistkaui));
cnt = 0;
flag = 0;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
scanf("%c",Map[i][j]);
if(Map[i][j] == '.')
{
a[cnt].x = i;
a[cnt++].y = j;
}
else
{
vistrow[i][Map[i][j]-'] = 1;
vistcol[j][Map[i][j]] = 1;
vistkaui[i/3][j/3][Map[i][j]] = 1;
}
}
}
DFS(0);
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
{
if(j == 8)
printf("%c\n",Map[i][j]);
else
printf("%c",Map[i][j]);
}
}
return 0;
}
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005
using namespace std;
char Map[10][10];
int vistrow[10],vistcol[10];
int grid[10],rec[512],num[512];
inline int g(int x,int y)
{
return (x/3)*3+y/3;
}
inline void flip(int x,int y,int to)
{
vistrow[x]^=1<<to;
vistcol[y]^=1<<to;
grid[g(x,y)]^=1<<to;
}
bool DFS(int x)
{
if(x==0) return 1;
int minn=10,xx,yy;
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++)
{
if(Map[i][j]=='.')
{
int val=vistrow[i]&vistcol[j]&grid[g(i,j)];
if(!val) return 0;
if(rec[val]<minn) {
minn=rec[val],xx=i,yy=j;
}
}
}
}
int val=vistrow[xx]&vistcol[yy]&grid[g(xx,yy)];
for(;val;val-=val&-val) {
int to=num[val&-val];
Map[xx][yy]=to+'1';
flip(xx,yy,to);
if(DFS(x-1)) return 1;
flip(xx,yy,to);
Map[xx][yy]='.';
}
return 0;
}
int main() {
for(int i=0;i<1<<9;i++) {
for(int j=i;j;j-=j&-j)
rec[i]++;
}
for(int i=0;i<9;i++) {
num[1<<i]=i;
}
char s[100];
while(~scanf("%s",s)&&s[0]!='e') {
for(int i=0;i<9;i++)
vistrow[i]=vistcol[i]=grid[i]=(1<<9)-1;
int tot=0;
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
Map[i][j]=s[i*9+j];
if(Map[i][j]!='.') flip(i,j,Map[i][j]-'1');
else ++tot;
}
}
DFS(tot);
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
printf("%c",Map[i][j]);
printf("\n");
}
return 0;
}
数独求解问题(DFS+位运算优化)的更多相关文章
- N皇后解法以及位运算优化
N皇后解法以及位运算优化 观察棋盘,要求皇后之间不能处在同行同列同一条斜线,求使得每行都有一个皇后的放置方法共有多少种. 每尝试放置一个皇后,都可以把该位置所在的行.列标号用一个数组标记,含义表示该行 ...
- N皇后-位运算优化
N皇后问题 时间限制: 5 Sec 内存限制: 128 MB 题目描述 魔法世界历史上曾经出现过一个伟大的罗马共和时期,出于权力平衡的目的,当时的政治理论家波利比奥斯指出:“事涉每个人的权利,绝不应 ...
- POJ - 3074 Sudoku (搜索)剪枝+位运算优化
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- UVa 818Cutting Chains (暴力dfs+位运算+二进制法)
题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...
- 模拟赛T5 : domino ——深搜+剪枝+位运算优化
这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- UVA-818 dfs + 位运算
暴力枚举一些圆环,将这些圆环解开,看能否成为单链.判断单链的三个条件: 除了这些删除的圆环之外,其他圆环还连接着的圆环不能超过两个. 剩下的环没有连成圈. 剩下的圆环共分成m堆,每堆之间无连接,m必须 ...
- [noip模拟题]科技节 - 搜索 - 位运算优化
[问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那么多活动,还要不要认真学习了?!这样不行!……于是,校长 ...
- POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】
1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | ...
随机推荐
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- 30-懒省事的小明(priority_queue)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=55 懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 ...
- IP协议、ARP协议等之温故知新
今天才知道: 1.IP协议的固定部分长度为20字节.(貌似有一家运维工程师面试我的时候,问过我这个问题呢.) 2.IP数据包首部中的协议?? 答:协议:占8位,指出此数据报携带的数据使用何种协议以便目 ...
- Part7-时钟初始化_lesson1
1.概念解析 1.1时钟脉冲信号 1.2时钟脉冲频率 1.3时钟源(提供时钟脉冲信号) a.晶振 b.锁相环PLL 2.时钟体系 2440: 晶振的频率.时钟体系有多少个PLL.这些PLL分别产生了哪 ...
- Python基础入门-数据类型
一.变量 1)变量定义 在python中,我们可以把变量理解为一个值,变量是一个标签名,变量是有对应的一个值. name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 ...
- MySQL性能调优与架构设计——第8章 MySQL数据库Query的优化
第8章 MySQL数据库Query的优化 前言: 在之前“影响 MySQL 应用系统性能的相关因素”一章中我们就已经分析过了Query语句对数据库性能的影响非常大,所以本章将专门针对 MySQL 的 ...
- LSI Storcli 工具使用
查看RAID卡ID 命令功能 查看LSI SAS3108RAID卡的ID. 命令格式 storcli64 show 使用实例 # 查看LSI SAS3108RAID卡的ID. [root@localh ...
- angular 事件绑定
<button (click)="onClick($event)">点我</button> import { Component, OnInit } fro ...
- WinForm中TabControl的使用
TabControl和TabPage之间有一个默认颜色的边框,很难去除,需要重写TabControl控件重绘区域 public class FullTabControl : TabControl { ...
- windows下go编码转换问题
github上有两个package做编码转换,都是基于iconv,用到了cgo,在linux下没有问题,在windows下用,非常麻烦.采用mingw安装libiconv也不行,一直提示找不到libi ...