任意门: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. 面向对象 OOP

    [面向对象编程OOP]   1 语言的分类 面向机器 :汇编语言 面向过程 :c语言 面向对象 :c++ Java PHP等   2 面向过程与面向对象 面向过程:专注于如何去解决一个问题的过程,编程 ...

  2. 关于webApi使用session

    1.关于webApi使用session 在Global.asax中注册session添加以下代码 public override void Init() { //开启session this.Post ...

  3. 2017年11月28日 C#进程和线程

    进程 需要放using System.Diagnostics;才可以用进程 用时的方法名为Process 用两个按钮一个为选择文件夹一个为打开可以打开系统内的进程. 注意:打开时一定要用进程名 Pro ...

  4. Java 异常的处理方式--throws和try catch

    异常的第一种处理方式throws. 看以下例子: import java.io.*;public class ExceptionTest04{ public static void main(Stri ...

  5. HDU 4283 (第k个出场 区间DP)

    http://blog.csdn.net/acm_cxlove/article/details/7964594 http://www.tuicool.com/articles/jyaQ7n http: ...

  6. Redis安装、配置

    一.Redis安装 Linux安装 下载tar包,移至Linux目录下 解压:tar -zxvf redis-4.0.1.tar.gz 安装gcc:yum install gcc-c++(编译失败需安 ...

  7. json_decode($json, true) true什么意思

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  8. 通过mysql自动同步redis

    在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终的存储引擎,Redis其实也可以作为一种键值对型的数据库,但在一些实际场景中,特别是关系型结构并不适合使用Redis直接作为数据库.这俩家 ...

  9. 第8章 CSS3中的变形与动画(上)

    变形--旋转 rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中心顺时 ...

  10. 廖雪峰JavaScript练习题2

    请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart'] 肯定有更简单的方法, ...