HDU暑假多校第四场J-Let Sudoku Rotate
一、题意
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的更多相关文章
- hdu第4场j.Let Sudoku Rotate
Problem J. Let Sudoku Rotate Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- HDU 4635 多校第四场 1004 强联通
我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- HDU 全国多校第四场 题解
题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...
- 2019牛客多校第四场J free——分层图&&最短路
题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...
- 牛客多校第四场 J Free 最短路
题意: 求最短路,但是你有k次机会可以把路径中某条边的长度变为0. 题解: 跑k+1次迪杰斯特拉,设想有k+1组dis数组和优先队列,第k组就意味着删去k条边的情况,每次松弛操作,松弛的两点i,j和距 ...
- 2019牛客多校第四场J free 最短路
free 题意 给出一个带权联通无向图,你需要从s走到t,你可以选择k条变让他们的权值为0问从s到t的最小权值是多少? 分析 思考一下,如果不带k条白嫖这个条件,那么这就是一个简单的dji就搞定了,我 ...
- 2018牛客多校第四场 J.Hash Function
题意: 给出一个已知的哈希表.求字典序最小的插入序列,哈希表不合法则输出-1. 题解: 对于哈希表的每一个不为-1的数,假如他的位置是t,令s = a[t]%n.则这个数可以被插入当且仅当第s ~ t ...
- HDU暑假多校第八场G-Card Game
一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...
随机推荐
- Windows远程桌面,出现身份验证错误,要求的函数不正确
升级windows10 1803后,mstsc远程桌面出现 mstsc 远程桌面要求的函数不受支持,这可能是由于 CredSSP 加密 Oracle 修正.如图所示: 运行(win+r) gpedit ...
- 二、Python安装扩展库
第一步:推荐easy_install工具 下载地址:https://pypi.python.org/pypi/setuptools 下载"ez_setup.py"文件; 通过运行c ...
- oracle数据库中创建表空间和临时表空间,以及用户和密码以及设置密码永不过期
首先进入oracle用户,命令是: su - oracle sqlplus /nolog connect system/123456@ora11g 或者 [oracle@localhost ~]$ ...
- 小故事学设计模式之Command : (一) 在永和豆浆店
IT的事就是过场多,过场多了就容易忘,所以我们不妨看一个记一个,这也是一个办法,顺便跟同行们学习交流一下)前几天出去拍照,饿到腿软, 回城附近有一家永和豆浆店, 我们决定去那边解决午餐.豆浆店里面还不 ...
- C语言 字符串的声明与使用
// 字符串的定义和初始化 void test() { // "mj" char s[] = {'m', 'j', '\0'}; // 字符串"mj" ] = ...
- PHP设计模式——适配器模式
<?php /** * 适配器模式 * 适配器模式是将某个对象的接口适配为另一个对象所期望的接口 * * 在需要转化一个对象的接口用于另一个对象时,最好实现适配器模式对象 */ class We ...
- HTML中什么时候加px
如:<img width="800" height="600" src="#"/> 不加后面的px; #center{ ...
- 【洛谷P2168】[NOI2015]荷马史诗
荷马史诗 建一个k叉哈夫曼树,用堆维护一下 // luogu-judger-enable-o2 #include<iostream> #include<cstdio> #inc ...
- EF执行SQL语句
使用上下文中的Database.SqlQuery<对应的表名>(sql语句) var data = dbcenter.Database.SqlQuery<CcBusiFormview ...
- data-ng-hide 指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...