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],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...
随机推荐
- File not Found:DockForm.dcu的解决办法
安装控件时,如果引用了dsgnintf单元,那么就会提示找不到proxy.pas 或者DockForm.dcu的错误,只需在安装控件包时添加“lib\DesignIde.dcp”即可
- 怎么在overflow-y:sroll的情况下 隐藏滚动条
当我们的内容超出了我们的div,往往会出现滚动条,影响美观. 尤其是当我们在做一些导航菜单的时候.滚动条一出现就破坏了UI效果. 我们不希望出现滚动条,也不希望超出去的内容被放逐,就要保留鼠标滚动的 ...
- pymongo 常用操作函数
pymongo 是 mongodb 的 python Driver Editor. 记录下学习过程中感觉以后会常用多一些部分,以做参考. 1. 连接数据库 要使用pymongo最先应该做的事就是先连上 ...
- bzoj 3339 莫队
题意: 求任意一个区间的SG函数. 想到线段树,但是线段树合并很麻烦. 线段树——分块. 分块的一个应用就是莫队算法. 怎么暴力递推呢? 从一个区间到另一个区间,Ans 取决于 Ans 和 加入和删除 ...
- 【转】Java类加载原理解析
原链接 1 基本信息 每个java开发人员对java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载. Java的类加载机制是j ...
- idea原生ajax数据处理(增删改查)
项目名称:Bookstore UI界面 项目文件 操作: jsp代码 <%@ page import="dao.BookDAO" %> <%@ page impo ...
- Android学习笔记_21_ViewFlipper使用详解 手势识别器
一.介绍ViewFilpper类 1.1 屏幕切换 屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面:一个个性化设置 ...
- lucene&solr学习——索引维护
1.索引库的维护 索引库删除 (1) 全删除 第一步:先对文档进行分析 public IndexWriter getIndexWriter() throws Exception { // 第一步:创建 ...
- python-time、datetimme模块
time模块 1.time.time():返回当前时间的时间戳. 打印时间戳: >>> import time >>> time.time() 1530329387 ...
- 浅谈二分查找 JavaScript
算法介绍 二分查找,也称折半查找,是一种在有序数组中查找特定元素的搜索算法.查找过程经历一下步骤: (1)从有序数组的中间的元素开始搜索,如果该元素正好是目标元素,则停止搜索并返回该元素的索引值,否则 ...