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. 论文笔记之:Learning Cross-Modal Deep Representations for Robust Pedestrian Detection

    Learning Cross-Modal Deep Representations for Robust Pedestrian Detection 2017-04-11  19:40:22  Moti ...

  2. keySet,entrySet用法 以及遍历map的用法

    Set<K> keySet() //返回值是个只存放key值的Set集合(集合中无序存放的)Set<Map.Entry<K,V>> entrySet() //返回映 ...

  3. sap hana 数据库 EBS

    SAP实时数据平台详解 ************************************************************ EBS是Oracle 公司对原有应用产品整合后的一个产 ...

  4. Python 基础 Python是什么

    1.Python 是一门高级的.面向对象的,解释性,脚本语言.

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

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

  6. C#:CsvReader读取.CSV文件(转换成DataTable)

    原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...

  7. idea输出目录详解

    引言:在项目中遇到了一个问题,在使用idea时,项目中Tomcat的虚拟目录映射总是失败,而当我采用myeclipse时却能映射过去. 自己花费了很长时间,终于找出了问题所在,原来是由于idea自己采 ...

  8. Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...

  9. 完全二叉树的节点个数 Count Complete Tree Nodes

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  10. 网格视图GridView的使用

    网格视图GridView的排列方式与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要按矩阵格式进行显示时,就可以使用GridView控件来实现. 本文将以一个具体的实例来说明如何使用GridV ...