Problem J. Let Sudoku Rotate

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

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.

  当时总感觉复杂度会炸,后来看题解才悟到其实由于数独的限制性较强,可以减去很大一部分无用解,然后就是普通的深搜了,

关键在于如何处理这个棋盘,我是一个一个区域处理的,枚举每个区域的旋转次数,如果和之前的不冲突就往下一个区域dfs,注意的是

函数返回的时候要将rotate过得区域再ratate回去。

  

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int ans,h[],c[],e[][],g[][];
bool vis[];
void rotate(int a,int b){
int i1,i2,j1,j2;
for(i1=*a-,j2=*b-;i1<=*a;++i1,j2++){
for(j1=*b-,i2=*a;j1<=*b;++j1,i2--){
g[i1][j1]=e[i2][j2];
}
}
for(int i=*a-;i<=*a;++i){
for(int j=*b-;j<=*b;++j){
e[i][j]=g[i][j];
}
}
}
bool ok(int x,int y){
for(int i=*x-;i<=*x;++i){
memset(vis,,sizeof(vis));
for(int j=;j<=y*;++j){
if(vis[e[i][j]]) return ;
vis[e[i][j]]=;
}
}
for(int j=*y-;j<=*y;++j){
memset(vis,,sizeof(vis));
for(int i=;i<=*x;++i){
if(vis[e[i][j]]) return ;
vis[e[i][j]]=;
}
}
return ;
}
void dfs(int x,int y,int tmp){
if(x>){
if(tmp<ans) ans=tmp;
return;
}
if(tmp>=ans) return;
for(int i=;i<;++i){
if(i) rotate(x,y);
if(ok(x,y)){
if(y==) dfs(x+,,tmp+i);
else dfs(x,y+,tmp+i);
}
}
rotate(x,y);//再转一次相当于归回原位
}
int main(){
int t,n,m,i,j,k;
char ch;
cin>>t;
while(t--){
for(i=;i<=;++i){
for(j=;j<=;++j){
cin>>ch;
e[i][j]=isdigit(ch)?ch-'':ch-'A'+;
}
}
ans=inf;
dfs(,,);
cout<<ans<<endl;
}
return ;
}

hdu-6341-搜索的更多相关文章

  1. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  3. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  4. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

  5. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  6. hdu 1495 (搜索) 非常可乐

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...

  7. hdu 4665 搜索

    思路:直接搜索 #include<iostream> #include<cstdio> #include<algorithm> #include<cstrin ...

  8. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  9. hdu 4620 搜索

    好苦逼,为啥数组开小了,不会runtime error,还得我WA了几个小时,我泪流满面. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4620 #i ...

  10. hdu&&poj搜索题题号

    搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...

随机推荐

  1. (转) Supercharging Style Transfer

      Supercharging Style Transfer Wednesday, October 26, 2016 Posted by Vincent Dumoulin*, Jonathon Shl ...

  2. TIM定时器的应用

    TIM定时器的应用   ①输入捕获的应用: 上一节,我已阐述TIM的输入捕获具体作用有两个(如下图):     对输入信号的测量:                                    ...

  3. SHOI 2017 相逢是问候(扩展欧拉定理+线段树)

    题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...

  4. 【Hadoop 分布式部署 六:环境问题解决和集群基准测试】

    环境问题: 出现Temporary  failure  in  name  resolutionp-senior-zuoyan.com 的原因有很多,主要就是主机没有解析到, 那就在hadoop的sl ...

  5. 《机器学习实战》之k-近邻算法(示例)

    看了这本书的第一个算法—k-近邻算法,这个算法总体构造思想是比较简单的,在ACM当中的话就对应了kd树这种结构.首先需要给定训练集,然后给出测试数据,求出训练集中与测试数据最相近的k个数据,根据这k个 ...

  6. vs编译出现 fatal error LNK1281:无法生成 SAFESEH 映像

    问题: 在vs编译中我们有时候常常会见到这样的错误,无法生成 SAFESEH 映像,镜像安全问题 解决方法: 1.打开该项目的"属性页"对话框. 2.单击"链接器&quo ...

  7. R语言通过loess去除某个变量对数据的影响--CNV分析

    当我们想研究不同sample的某个变量A之间的差异时,往往会因为其它一些变量B对该变量的固有影响,而影响不同sample变量A的比较,这个时候需要对sample变量A进行标准化之后才能进行比较.标准化 ...

  8. colgroup和col的区别

    转载自:http://blog.csdn.net/carefree31441/article/details/3291397 colgroup和col一般出现在表格当中定义表格单独列的任意属性col能 ...

  9. Spring Boot的数据访问 之Spring Boot + jpa的demo

    1. 快速地创建一个项目,pom中选择如下 <?xml version="1.0" encoding="UTF-8"?> <project x ...

  10. 深入浅析Spring的AOP实现原理

    转载来源:https://www.jb51.net/article/81788.htm AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Or ...