一、题意

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.

考虑一个完成后的数独游戏局面,将16*16的局面分割成4*4的小方块,随机旋转小方块得到一个新的局面,现在给出新的局面,问你最少旋转几次可以得到一个完成的局面。

二、解题思路。

首先考虑暴力搜索,4^16次方次旋转——来自每个小方块4次旋转,一共16个小方块。

但是考虑数独游戏的特征,每走一步都可以判断当前的选择是否合法——因此可以进行剪枝——判断下当前所在方块、所在形状的合法性——已经取到的行、列是否合法。

处于某种我证明不了的原因认为,这种方式将会把剪枝操作剪到足够小,以至于31ms就可以搞定1000组输入。

#include<bits/stdc++.h>
using namespace std; #define ll long long
const int MAXN=; char mapp[MAXN][MAXN]; int ans=; int tran(char c)
{
if(c>=''&&c<='')return c-'';
else return c-'A'+;
} bool check_left(int a,int b)
{
// int x = a*4;
int endd = (b+)*;
if(b == )return true;
for(int i=;i<;++i)
{
int x = a* +i;
int ch[];
memset(ch,,sizeof(ch));
for(int j=;j<endd;++j)
{
ch[tran(mapp[x][j])]++;
if(ch[tran(mapp[x][j])] == )return false;
}
}return true;
} bool check_up(int a,int b)
{
int endd = (a+)*;
if(a == )return true;
for(int j=;j<;++j)
{
int y = b*+j;
int ch[];
memset(ch,,sizeof(ch));
for(int i=;i<endd;i++)
{
ch[tran(mapp[i][y])]++;
if(ch[tran(mapp[i][y])] == )return false; }
}return true;
} bool check(int a,int b)
{
return check_left(a,b)&&check_up(a,b);
} bool check(int l)
{
for(int i=;i<;++i)
{
int x = l*+i;
int ch[];
memset(ch,,sizeof(ch));
for(int j=;j<;++j)
{
ch[tran(mapp[x][j])]++;
if(ch[tran(mapp[x][j])]==)return false;
}
}
return true;
} void change(int a,int b)
{
int x = a*;
int y = b*; int tmp = mapp[x][y];
mapp[x][y] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; } void change1(int a,int b)
{
int x = a*;
int y = b*; int tmp = mapp[x][y];
mapp[x][y] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; tmp = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = mapp[x+][y+];
mapp[x+][y+] = tmp; }
bool search(int a,int b,int summ)
{ // cout<<"search : "<<a<<" "<<b<<" step: "<<summ<<endl; if(b==)
{
if(check(a)){
ans += summ;
return true;
}
return false;
} if(search(a,b+,summ))return true;
for(int i=;i<;++i)
{
change1(a,b);
if(search(a,b+,summ++i))return true;
}
change1(a,b);
return false;
} bool dfs(int now,int summ)
{
int a = now/;
int b = now%;
if(now == )
{
ans = min(ans,summ);
return true;
}
if(check(a,b)&&dfs(now+,summ));
for(int i=;i<;++i)
{
change1(a,b);
if(!check(a,b))continue;
dfs(now+,summ+i+);
}
change1(a,b);
return false;
} void show()
{
for(int i=;i<;++i)
{
for(int j=;j<;++j)
{
cout<<mapp[i][j];
}cout<<endl;
}
} void init()
{
ans = INT_MAX;
for(int i=;i<;++i)gets(mapp[i]);
dfs(,);
cout<<ans<<"\n";
} int main()
{ int t;
// cin>>t;
scanf("%d\n",&t);
while(t--)init(); return ;
}

HDU暑假多校第四场J-Let Sudoku Rotate的更多相关文章

  1. hdu第4场j.Let Sudoku Rotate

    Problem J. Let Sudoku Rotate Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  2. HDU 4635 多校第四场 1004 强联通

    我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...

  3. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  4. HDU 全国多校第四场 题解

    题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...

  5. 2019牛客多校第四场J free——分层图&&最短路

    题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...

  6. 牛客多校第四场 J Free 最短路

    题意: 求最短路,但是你有k次机会可以把路径中某条边的长度变为0. 题解: 跑k+1次迪杰斯特拉,设想有k+1组dis数组和优先队列,第k组就意味着删去k条边的情况,每次松弛操作,松弛的两点i,j和距 ...

  7. 2019牛客多校第四场J free 最短路

    free 题意 给出一个带权联通无向图,你需要从s走到t,你可以选择k条变让他们的权值为0问从s到t的最小权值是多少? 分析 思考一下,如果不带k条白嫖这个条件,那么这就是一个简单的dji就搞定了,我 ...

  8. 2018牛客多校第四场 J.Hash Function

    题意: 给出一个已知的哈希表.求字典序最小的插入序列,哈希表不合法则输出-1. 题解: 对于哈希表的每一个不为-1的数,假如他的位置是t,令s = a[t]%n.则这个数可以被插入当且仅当第s ~ t ...

  9. HDU暑假多校第八场G-Card Game

    一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...

随机推荐

  1. ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目

    ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目 标签(空格分隔): Vue 首先安装全局@vue-cli工具: npm install -g @vue/cli 然后创建项 ...

  2. April 1 2017 Week 13 Saturday

    There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. Get a life, a real life, not a manic pu ...

  3. python入门17 类和对象

    类:一类事物的抽象化.概念: 类的变量(属于类的变量,定义在类的开始处)  成员变量(self.变量) 类的方法( @classmethod,cls参数)   成员方法( self参数 )  静态方法 ...

  4. 我是一只IT小小鸟读后感 Part 1

    我是一只IT小小鸟读后感 Part 1 梦断计院 作为一个工科生,真的和作者想到一块去了.在科大这个环境下,GPA成了衡量一个学生优秀与否的唯一因素,而真正对于编程和技术性的东西有兴趣的,往往被埋没在 ...

  5. React简介

    React JS:可以用react的语法,来编写网页的交互效果 React Native:可以让我们借用react的语法来编写原生的app应用 React VR:在react的基础上去开发VR,或者说 ...

  6. U3

    一个项目里面可以有多个Activity AndroidManifest.xml<intent-filter> <action android:name="android.i ...

  7. [19/03/12-星期二] 数组_遍历(for-each)&复制&java.util.Arrays类

    一.遍历 for-each即增强for循环,是JDK1.5新增加的功能,专门用于读取数组或集合中所有的元素,即对数组进行遍历. //数组遍历 for-each public class Test_03 ...

  8. Git免密码提交

    下面说一下https克隆的方式免密码提交 在我们下载链接前面加上账号:密码@即可 方式一: 使用https的方式克隆代码 git clone '地址' 查看项目中的配置文件 vim .git/conf ...

  9. HDU 1260 Tickets (普通dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1260 Tickets Time Limit: 2000/1000 MS (Java/Others)   ...

  10. js 3秒后跳转页面的实现代码

    隔多少秒后自动跳转到其它页(js脚本) 方法一: $(function(){ Load(URL); }) var secs = 3; //倒计时的秒数 var URL = "<?= u ...