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

时间:2021-10-15 16:40:52

任意门: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.
2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

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