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

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

分析:很明显的一道深授暴力题 , 一开始不知道是怎么收才好 , 那时候考虑说假如同一行或者同一列都有区域要反转 , 我该怎么找 , 后来看了题解后发现 , 我只要保证每次旋转后 , 该区域与此前的区域是满足数独的就好 , 子问题的不重复不会影响到大问题的不重复 。深搜索的能力需要加强

#include<bits/stdc++.h>
using namespace std ;
int G[20][20],tmp[20][20];
bool vis[20];
int ans;
void rot(int x , int y)
{
       for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            tmp[j][4-i+1]=G[(x-1)*4+i][(y-1)*4+j];
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            G[(x-1)*4+i][(y-1)*4+j]=tmp[i][j];
}

bool check(int x , int y)
{
    for(int i=4*x-3 ; i<=x*4 ; i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=1 ; j<=4*y ; j++)
        {
            if(!vis[G[i][j]])
            vis[G[i][j]]=1;
            else return 0;
        }
    }
    for(int i=y*4-3 ; i<=y*4 ; i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=1 ; j<=4*x ; j++)
        {
            if(!vis[G[j][i]])
            vis[G[j][i]]=1;
            else return 0;
        }
    }
    return 1;
}
void dfs(int x , int y , int sum)
{
    if(ans<=sum)
    return ;
    int X=x , Y=y+1;
    if(x==5)
    {
        ans=sum;

        return ;
    }
    if(Y==5)
    {
        X++;
        Y=1;
    }
    for(int i=0 ; i<4 ; i++)
    {
        if(i) rot(x,y);
        if(check(x,y))
        {   //printf("520");
            dfs(X,Y,sum+i);
        }

    }
    rot(x,y);
}
char s[20];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=1 ; i<=16 ; i++)
        {
            scanf("%s",s+1);
            for(int j=1 ; j<=16 ; j++)
            {
                if(s[j]>=‘0‘&&s[j]<=‘9‘)
                G[i][j]=s[j]-‘0‘;
                else
                G[i][j]=s[j]-‘A‘+10;
            }
        }

        ans=0x3f3f3f3f;
        dfs(1,1,0);
        printf("%d\n",ans);
    }
  return  0;
}

原文地址:https://www.cnblogs.com/shuaihui520/p/10324549.html

时间: 2024-11-05 22:34:20

HDU-6341 Problem J. Let Sudoku Rotate(dfs 剪枝)的相关文章

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

任意门: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 i

hdu6341 Problem J. Let Sudoku Rotate (dfs)

题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行.每列就行了 那么我们在dfs中就可以用两个行列两个数组来标记每个数字出现的次数, 大于1则不行 另外此时是逆时针来的,那么你就要顺时针回去 逆一次等于顺3次 参考博客:https://www.cnblogs.com/zquzjx/p/10326048.html 代码: #include<bits/s

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, w

hdu 1010 Tempter of the Bone (DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)

ZOJ Problem Set - 1008 Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divide

HDU 5113 Black And White(DFS+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题面: Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 350 Special Judge Problem Description I

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

HDU 2553 N皇后问题(深搜DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 772   Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出

POJ 2676 Sudoku (数独 DFS)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some