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. Apple ID双重认证验证码无法输入问题

    问题:用Apple ID登录老版本ios系统时,会提示“”需要提供Apple ID验证码才能登陆.请键入您的密码,并随后键入显示在您其他设备上的验证码”. 解决:老版本系统无输入验证码的地方,那就把验 ...

  2. Python 安装与环境变量配置

    一.软件下载 Python安装包下载地址:https://www.python.org/ 二.安装过程(略) 三.环境变量配置: 方法一:使用cmd命令添加path环境变量 在cmd下输入: path ...

  3. java.lang.NoClassDefFoundError: com/gexin/rp/sdk/exceptions/RequestException解决方法

    本文为博主原创,未经允许不得转载: 最近在开发个推的时候遇到的问题,当我在maven仓库中下载个推的jar包时,下载不下来,索性在项目中Configue build Path,将jar下载到本地 手动 ...

  4. js 字符串加密解密

    Welcome to jzzy.com

  5. Centos 7下添加新用户并授权

    1.创建一个 xiaoyang 用户 [root@VM_81_181_centos ~]# adduser xiaoyang 2.为创建的用户设置密码 [root@VM_81_181_centos ~ ...

  6. 关于页面的跳转添加参数(比如id啥的)

    最近一些新手老是问我一些关于页面之间的传递跳转,怎么带参数这件事,(比如说一个列表页到详情页面数据是怎么传输的),今个没事就在这说一些! 这里拿列表页面到详情页面来阐述下哈! 首先在列表页,我们通过a ...

  7. 报名 | 蚂蚁金服ATEC科技大会 · 上海:数字金融新原力

    小蚂蚁说: 2019年1月4日,蚂蚁金服ATEC城市峰会将以“数字金融新原力(The New Force of Digital Finance)”为主题,在中国上海举办.蚂蚁金服ATEC(Ant Te ...

  8. JAVA中使用浮点数类型计算时,计算精度的问题

    标题     在Java中实现浮点数的精确计算    AYellow(原作) 修改    关键字     Java 浮点数 精确计算   问题的提出:如果我们编译运行下面这个程序会看到什么?publi ...

  9. linux df

    显示磁盘使用情况 [hadoopuser@CNSZ443239 ~]$ df   文件系统                            1K-块           已用      可用 已 ...

  10. centos/redhat 删除虚拟网桥virbr0

    kvm虚拟化环境安装好后,ifconfig会发现多了一个虚拟网卡virbr0. 这是由于安装和启用了libvirt服务后生成的,libvirt在服务器(host)上生成一个  virtual netw ...