Description

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.


Write a Sudoku playing program that reads data sets from a text file.

Input

Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i-th string stands for the i-th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,…,P,-}, where – (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file.

Output

The program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

--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-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK

题意:就是让每行,每列,每个4*4的十六宫格中A~P只出现一次。输出16*16的宫格信息。

思路:我总感觉这题用dancing links会好做很多

①肯定是选择可填入的字母最少的位置开始dfs,这样分支比较少

②如果一个位置只剩一个字母可以填,就填上这个字母(废话)

③如果所有的字母不能填在该行(列、十六宫格),立刻回溯

④如果某个字母只能填在该行(列、十六宫格)的某处,立刻填写

首先除了dfs的结束条件外,一次写下对位置的②剪枝,对行,列,十六宫格的③、④剪枝,因为②、④剪枝填了一个字母,需要再次判断结束条件,

然后就是对可能性最小的位置进行dfs

(代码参考网上,侵删)

#include<iostream>
#include<string.h>
#include<cstdio> using namespace std; int maps[][];
int table[][];
int num; void put_in(int x,int y,int k)
{
num++;
maps[x][y] = k;
table[x][y] |= <<k;
for(int i=; i<=; i++)
{
table[i][y] |= <<k;
table[x][i] |= <<k;
}
int r = (x-)/*+;
int c = (y-)/*+;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
table[r+i][c+j] |= <<k;
}
}
} int _count(int x)
{
for(int i=; x; i++)
{
if(x & )
{
if(x>> == )
return i;
return -;
}
x >>= ;
}
return -;
} int row(int x,int k)
{
int t = -;
for(int y=; y<=; y++)
{
if(maps[x][y] == k)
return -;
if(maps[x][y] >= )
continue;
if((table[x][y]&<<k) == )
{
if(t != -)
return -;
t = y;
}
}
if(t != -)
return t;
return -;
} int col(int y,int k)
{
int t = -;
for(int x=; x<=; x++)
{
if(maps[x][y] == k)
return -;
if(maps[x][y] >= )
continue;
if((table[x][y]&<<k) == )
{
if(t != -)
return -;
t = x;
}
}
if(t != -)
return t;
return -;
} void cuble(int r,int c,int k,int &x,int &y)
{
x = -;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(maps[i+r][j+c] == k)
{
x=-;
return;
}
if(maps[i+r][j+c] >= )
continue;
if((table[i+r][j+c]&<<k) == )
{
if(x != -)
{
x=-;
return;
}
x = i;
y = j;
}
}
}
} int cal(int x)
{
int cnt = ;
while(x)
{
if(x&)
cnt++;
x >>= ;
}
return cnt;
} bool dfs()
{
if(num == )
{
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
printf("%c",maps[i][j]+'A');
}
puts("");
}
puts("");
return ;
}
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(maps[i][j] >= )
continue;
int k = _count(table[i][j]);
if(k != -)
put_in(i,j,k);
}
}
for(int x=; x<=; x++)
{
for(int k=; k<; k++)
{
int y = row(x,k);
if(y == -)
return ;
if(y != -)
put_in(x,y,k); }
}
for(int y=; y<=; y++)
{
for(int k=; k<; k++)
{
int x = col(y,k);
if(x == -)
return ;
if(x != -)
put_in(x,y,k); }
}
for(int r=; r<=; r+=)
{
for(int c=; c<=; c+=)
{
for(int k=; k<; k++)
{
int x,y;
cuble(r,c,k,x,y);
if(x == -)
return ;
if(x != -)
put_in(r+x,c+y,k); }
}
}
if(num == )
{
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
printf("%c",maps[i][j]+'A');
}
puts("");
}
puts("");
return ;
}
int t_num;
int t_maps[][];
int t_table[][];
t_num = num;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
t_maps[i][j] = maps[i][j];
t_table[i][j] = table[i][j];
}
}
int mx,my,mn=;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(maps[i][j]>=)
continue;
int r = - cal(table[i][j]);
if(r < mn)
{
mn = r;
mx = i,my = j;
}
}
}
for(int k=; k<; k++)
{
if((table[mx][my] & <<k) == )
{
put_in(mx,my,k);
if(dfs())
return ;
num = t_num;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
maps[i][j] = t_maps[i][j];
table[i][j] = t_table[i][j];
}
}
}
}
return ;
} int main()
{
char s[];
while(~scanf("%s",s))
{
num = ;
memset(table,,sizeof(table));
for(int j=; j<=; j++)
{
if(s[j-]!='-')
put_in(,j,s[j-]-'A');
else
maps[][j] = -;
}
for(int i=; i<=; i++)
{
scanf("%s",s);
for(int j=; j<=; j++)
{
if(s[j-]!='-')
put_in(i,j,s[j-]-'A');
else
maps[i][j] = -;
}
}
dfs();
} }

Sudoku POJ - 3076 (dfs+剪枝)的更多相关文章

  1. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  2. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  3. Sudoku POJ - 3076

    Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 5769   Accepted: 2684 Descripti ...

  4. HDU-6341 Problem J. Let Sudoku Rotate(dfs 剪枝)

    题目:有一个4*4*4*4的数独,每一横每一竖每一个小方块中都无重复的字母,即都为0-9,A-F..有一个已经填好的数独,若干个4*4的方块被逆时针拧转了若干次,问拧转回来至少需要多少次. 分析:很明 ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  7. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  8. (简单) POJ 3076 Sudoku , DLX+精确覆盖。

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  9. POJ 3076 Sudoku

    3076 思路: dfs + 剪枝 首先,如果这个位置只能填一种字母,那就直接填 其次,如果对于每一种字母,如果某一列或者某一行或者某一块只能填它,那就填它 然后,对于某个位置如果不能填字母了,或者某 ...

随机推荐

  1. Confluence 6 协同编辑问题解决

    协同编辑是 Synchrony 提供的,用于在编辑的时候实时同步.在一般的情况下,这个进程是不需要 Confluence 的管理员进行手动管理的. 这个页面将会帮助你 Confluence 安装实例中 ...

  2. Confluence 6 数据库表-集群(Clustering)

    下面的表格包含了 Confluence 站点使用集群的信息. clustersafety 在通常的情况下,这个表格只有一条记录. safetynumber 的值是 Confluence 被用来如何找到 ...

  3. css之操作属性

    1.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 ...

  4. 【ftp】主动模式和被动模式

    来自:http://blog.csdn.net/liuhelong12/article/details/50218311 原博主不让转载全文,不过下面这部分是原博主转载别人的,所以我拿过来应该没问题吧 ...

  5. burpsuite使用教程和实战详解(一)

    1.最近做渗透测试,其实使用一种方式很难全面的对一个web或者app等安全服务器做安全评估,所以要尽可能的对网络安全的渗透测试有一个较全面的认知.不光要熟悉前端和 后天的编程,还有掌握基于这两种编程的 ...

  6. php模拟数据请求

    php:模拟后台接受数据的步骤<?php> 1.连接数据库 $host="localhost"; $uname="root"; $upwd=&quo ...

  7. 实现用VB.Net/(C#)开发K/3 BOS 插件的真正可行方法

    转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没有认真去找这个方法呀. https://blog.csdn.net/chzjxgd/arti ...

  8. C++11 中的function和bind、lambda用法

    std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...

  9. gradle编译命令 & 自动打包等

    ./gradlew -v 版本号,首次运行,没有gradle的要下载的哦. ./gradlew clean 删除HelloWord/app目录下的build文件夹 ./gradlew build 检查 ...

  10. Windows Phone MultiBinding :Cimbalino Toolkit

    在WPF和WIN8中是支持MultiBinding 这个有啥用呢,引用下MSDN的例子http://msdn.microsoft.com/en-us/library/system.windows.da ...