hdu第4场j.Let Sudoku Rotate

Posted 2014slx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu第4场j.Let Sudoku Rotate相关的知识,希望对你有一定的参考价值。

Problem J. Let Sudoku Rotate

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


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.

 

技术分享图片

搜索。 转3次。跟行列又重复就返回;

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long
int ans;
int a[22][22];
int tmp[22][22]={0};
void rotat(int x,int y)
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            tmp[j][4-i+1]=a[(x-1)*4+i][(y-1)*4+j];
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            a[(x-1)*4+i][(y-1)*4+j]=tmp[i][j];
}
bool check(int x,int y)
{
    int book[26];
    for(int i=x*4-3;i<=x*4;i++)
    {
        memset(book,0,sizeof book);
        for(int j=1;j<=y*4;j++)
        {
            if(!book[a[i][j]])
                book[a[i][j]]=1;
            else
                return 0;
        }
    }
    for(int j=y*4-3;j<=y*4;j++)
    {
        memset(book,0,sizeof book);
        for(int i=1;i<=x*4;i++)
        {
            if(!book[a[i][j]])
                book[a[i][j]]=1;
            else
                return 0;
        }
    }
    return 1;
}

void bfs(int x,int y,int sum)
{
    if(x==5)
    {
        if(ans>sum)
          ans=sum;
        return;
    }
    if(sum>ans)
    {
        return;
    }
    for(int i=0;i<4;i++)
    {
        if(i)  rotat(x,y);
        if(check(x,y))
        {
            if(y+1<=4)
            bfs(x,y+1,sum+i);
            else
            bfs(x+1,1,sum+i);
        }
    }
    rotat(x,y);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=16*4;
        char str[22];
        for(int i=1;i<=16;i++)
        {
            scanf("%s",str+1);
            for(int j=1;j<=16;j++)
            {
                if(str[j]>9)
                a[i][j]=str[j]-A+10;
                else a[i][j]=str[j]-0;
            }

        }
        bfs(1,1,0);
        cout<<ans<<endl;

    }
   
    return 0;
}

 

以上是关于hdu第4场j.Let Sudoku Rotate的主要内容,如果未能解决你的问题,请参考以下文章

hdu6341 Problem J. Let Sudoku Rotate (dfs)

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

Problem J. Let Sudoku Rotate

HDU 4069 Squiggly Sudoku舞蹈链样例坑

2016多校第4场 HDU 6076 Security Check DP,思维

HDU 1426 Sudoku Killer(dfs 解数独)