一、题意

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. dos基础+环境搭建基础理论

    dos基础 市面上两大操作系统 windows.*nix(unix.linux.mac.bsd(安全性比较高)) 后三种都属于unix的衍生版本 linux是为了兼容unix开发的,最后开放了源代码 ...

  2. C++学习之【使用位操作符求素数分析】

    先放普通代码: #include <iostream> using namespace std; void getPrime_1() { const int MAXN = 100; boo ...

  3. 【[SCOI2007]蜥蜴】

    拆点 把每个点拆成入口和出口两个点 以下几种连边方式就行了 对于有蜥蜴的点,\(S\)向入口连1的边 对于能出去的点,出口向汇点连容量为\(inf\)的边 每个点的入口和出口连容量为高度的边 之后能相 ...

  4. 运用Xdebug调试和优化PHP程序

    什么是Xdebug? Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况.Xdebug现在的最新版本是xdebug 2.0.0beta ...

  5. Alert Log删除

    标题:Renaming or Deleting the Alert Log While an Oracle Instance is Up & Running (文档 ID 74966.1) Q ...

  6. C#实现打印

    C#实现导出pdf文件,打印 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  7. Beginning DirectX11 Game Programming

    DirectX11 or 10 made a big change comparing to DirectX9 The fixed-function pipeline was removed in D ...

  8. chromium之tuple

    // A Tuple is a generic templatized container, similar in concept to std::pair. // There are classes ...

  9. ATX 浅谈自动化测试工具 python-uiautomator2

    1.简介 python-uiautomator2是一个自动化测试开源工具,仅支持Android平台的原生应用测试. 2.支持平台及语言 python-uiautomator2封装了谷歌自带的uiaut ...

  10. Percona-Tookit工具包之pt-kill

      Preface       Sometimes,we are determined to kill some MySQL connections which are occupying huge ...