Problem J. Let Sudoku Rotate Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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 × grids, which consist of × 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 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 (≤T≤) denoting the number of test cases.
Each test case consists of exactly lines with 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 681D5A0C9FDBB2F7
0A734B62E167D9E5
5C9B73EF3C208410
F24ED18948A5CA63
39FAED5616400B74
D120C4B7CA3DEF38
7EC829A085BE6D51
B56438F129F79C2A
5C7FBC4E3D08719F
AE8B1673BF42A58D
60D3AF25619C30BE
294190D8EA57264C
C7D1B35606835EAB
AF52A1E019BE4306
8B36DC78D425F7C9
E409492FC7FA18D2 Sample Output Hint The original sudoku is same as the example in the statement.
搜索。 转3次。跟行列又重复就返回;
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long
int ans;
int a[][];
int tmp[][]={};
void rotat(int x,int y)
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
tmp[j][-i+]=a[(x-)*+i][(y-)*+j];
for(int i=;i<=;i++)
for(int j=;j<=;j++)
a[(x-)*+i][(y-)*+j]=tmp[i][j];
}
bool check(int x,int y)
{
int book[];
for(int i=x*-;i<=x*;i++)
{
memset(book,,sizeof book);
for(int j=;j<=y*;j++)
{
if(!book[a[i][j]])
book[a[i][j]]=;
else
return ;
}
}
for(int j=y*-;j<=y*;j++)
{
memset(book,,sizeof book);
for(int i=;i<=x*;i++)
{
if(!book[a[i][j]])
book[a[i][j]]=;
else
return ;
}
}
return ;
} void bfs(int x,int y,int sum)
{
if(x==)
{
if(ans>sum)
ans=sum;
return;
}
if(sum>ans)
{
return;
}
for(int i=;i<;i++)
{
if(i) rotat(x,y);
if(check(x,y))
{
if(y+<=)
bfs(x,y+,sum+i);
else
bfs(x+,,sum+i);
}
}
rotat(x,y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=*;
char str[];
for(int i=;i<=;i++)
{
scanf("%s",str+);
for(int j=;j<=;j++)
{
if(str[j]>'')
a[i][j]=str[j]-'A'+;
else a[i][j]=str[j]-'';
} }
bfs(,,);
cout<<ans<<endl; } return ;
}