任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341

Problem J. Let Sudoku Rotate

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1363    Accepted Submission(s): 717

Problem Description
Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the world.
In this problem, let us focus on puzzles with 16×16 grids, which consist of 4×4 regions. The objective is to fill the whole grid with hexadecimal digits, i.e. 0123456789ABCDEF, so that each column, each row, and each region contains all hexadecimal digits. The figure below shows a solved sudoku.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Yesterday, Kazari solved a sudoku and left it on the desk. However, Minato played a joke with her - he performed the following operation several times.
* Choose a region and rotate it by 90 degrees counterclockwise.
She burst into tears as soon as she found the sudoku was broken because of rotations.
Could you let her know how many operations her brother performed at least?
 
Input
The first line of the input contains an integer T (1≤T≤103) denoting the number of test cases.
Each test case consists of exactly 16 lines with 16 characters each, describing a broken sudoku.
 
Output
For each test case, print a non-negative integer indicating the minimum possible number of operations.
 
Sample Input
1
681D5A0C9FDBB2F7
0A734B62E167D9E5
5C9B73EF3C208410
F24ED18948A5CA63
39FAED5616400B74
D120C4B7CA3DEF38
7EC829A085BE6D51
B56438F129F79C2A
5C7FBC4E3D08719F
AE8B1673BF42A58D
60D3AF25619C30BE
294190D8EA57264C
C7D1B35606835EAB
AF52A1E019BE4306
8B36DC78D425F7C9
E409492FC7FA18D2
 
Sample Output
5

Hint

The original sudoku is same as the example in the statement.

 
Source

题意概括:

有一个 16×16 的已经打乱的数独,4×4为一宫,每次可对宫顺时针旋转 90 度。最少要操作多少次可以还原数独。

解题思路:

递归每一宫的左上角坐标 (x, y) ,DFS枚举每一宫的旋转次数,可知这样暴力的方案数为 4^(16) =  4294967296,需要剪枝。

由于数独的特殊性,我们枚举下一个状态前可以先判断上一个状态是否满足条件,即每行每列的数都要单独存在。

对于矩阵旋转的操作,找一下下标的规律:第一行变成第一列,第二行变成第二列,第三行变成第三列.....

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = ;
char str[MAXN][MAXN];
int mmp[MAXN][MAXN];
int tmp[MAXN][MAXN];
bool vis[MAXN];
int ans; void rt(int x, int y)
{
int rx = *x, ry = *y-;
for(int i = *x-; i <= *x; i++){
rx = *x;
for(int k = *y-; k <= *y; k++){
tmp[i][k] = mmp[rx][ry];
rx--;
}
ry++;
} for(int i = *x-; i <= *x; i++){
for(int j = *y-; j <= *y; j++){
mmp[i][j] = tmp[i][j];
}
} } bool check(int x, int y)
{
for(int i = *x-; i <= *x; i++){
memset(vis, , sizeof(vis));
for(int j = ; j <= *y; j++){
if(vis[mmp[i][j]]){
return false;
}
vis[mmp[i][j]] = true;
}
}
for(int i = *y-; i <= *y; i++){
memset(vis, , sizeof(vis));
for(int j = ; j <= *x; j++){
if(vis[mmp[j][i]]){
return false;
}
vis[mmp[j][i]] = true;
}
}
return true;
} void dfs(int x, int y, int cnt)
{
if(x > ){
ans = min(ans, cnt);
return;
}
for(int i = ; i < ; i++){
if(i) rt(x, y);
if(check(x, y)){
if(y < ) dfs(x, y+, cnt+i);
else dfs(x+, , cnt+i);
}
}
rt(x, y);
} int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
for(int i = ; i < ; i++){
scanf("%s", str[i]);
}
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
if(str[i][j] >= '' && str[i][j] <= ''){
mmp[i+][j+] = str[i][j]-'';
}
else mmp[i+][j+] = str[i][j]-'A'+;
}
}
ans = INF;
dfs(, , );
printf("%d\n", ans);
}
return ;
}

2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】的更多相关文章

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

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

  2. hdu6341 Problem J. Let Sudoku Rotate (dfs)

    题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行. ...

  3. hdu第4场j.Let Sudoku Rotate

    Problem J. Let Sudoku Rotate Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  4. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  5. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  6. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  7. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  8. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  9. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

随机推荐

  1. Magento 2开发教程 - 创建新模块

    视频在youtube网站国内访问不了,可以使用FQ软件查看. 视频地址:www.youtube.com/embed/682p52tFcmY@autoplay=1 下面是视频文字介绍: Magento ...

  2. sql server存储过程中SELECT 与 SET 对变量赋值的区别 转自Theo

    SQL Server 中对已经定义的变量赋值的方式用两种,分别是 SET 和 SELECT. 对于这两种方式的区别,SQL Server 联机丛书中已经有详细的说明,但很多时候我们 并没有注意,其实这 ...

  3. CXF生成客户端遇到的问题

    一.CXF环境配置路径错误 1.错误现象 在命令行中输入 wsdl2java -v 检查CXF安装是否正确. 出现错误=> ERROR: Unable to find cxf-manifest. ...

  4. 版本控制器之SVN

    1.开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目中需要一个很复杂的功 ...

  5. javaweb之MVC设计模式

    1.MVC简介 MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它把应用程序分成三个核心模块:模型,视图,控制器,它们各自处理自己的任务. 模型( ...

  6. js实现字体和容器宽高随窗口改变

    用于字体大小和容器的宽高字体和宽高设为rem就可以了 var html = document.documentElement; function fonts(){ var hW = html.offs ...

  7. es6 class类实例、静态、私有方法属性笔记

    实例属性.方法 class Foo { valueA = 100 //第一种实例属性定义,位置:new的实例上 constructor() { this.valueB = 200 //第二种实例属性定 ...

  8. safari兼容时间格式

    前提: 使用iview的DatePicker组件,保存时间后台接收时间戳 问题: safari中不支持2018-02-13这种格式转为时间戳会显示NaN 解决: new Date('2018/02/1 ...

  9. CSS性能优化新属性:will-change

    ---恢复内容开始--- will-change属性通过告诉浏览器什么属性.什么元素将会发生变化,可以对这些操作进行可能性的优化,由此提高CSS动画的执行效率. 这个属性可以有4个值: auto: 实 ...

  10. JavaScript & jQuery Code Snippet

    1. 按照每个object的Name属性对object对象集合进行排序: //sort a collection of objects by Name property function sortBy ...