loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879
思路:题目意思很简单,就是通过一些位置的交换,最后变成有序数列,对于一组序列,我们可以用康托展开然后hash判重。
然后就是普通的bfs,稍微留意一下细节即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int is_prime[]={,,,,,,,,,,,,,,,,,};
int fac[]={,,,,,,,,}; struct Point{
int num,flag;
}; struct Node{
Point state[];
int step;
}st; bool mark[]; int Get_Hash(Node &node)
{
int a[],val=,cnt;
for(int i=;i<;i++)a[i]=abs(node.state[i].num);
for(int i=;i<;i++){
cnt=;
for(int j=;j<i;j++){
if(a[j]>a[i])cnt++;
}
val+=cnt*fac[i];
}
return val;
} bool Judge(Node &node)
{
for(int i=;i<;i++){
if(abs(node.state[i].num)>abs(node.state[i+].num))
return false;
}
return true;
} Node Get_Node(Node &p,int pos1,int pos2,int dir)
{
//pos1->pos2 left
if(dir==){
if(pos1<pos2){
int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
for(int i=pos1;i<=pos2-;i++){
p.state[i].num=p.state[i+].num;
p.state[i].flag=p.state[i+].flag;
}
p.state[pos2-].num=tmp;
p.state[pos2-].flag=flag;
}else {
int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
for(int i=pos1;i>pos2;i--){
p.state[i].num=p.state[i-].num;
p.state[i].flag=p.state[i-].flag;
}
p.state[pos2].num=tmp,p.state[pos2].flag=flag;
}
}else if(dir==){ //pos1->pos2 right
if(pos1<pos2){
int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
for(int i=pos1;i<=pos2-;i++){
p.state[i].num=p.state[i+].num;
p.state[i].flag=p.state[i+].flag;
}
p.state[pos2].num=tmp,p.state[pos2].flag=flag;
}else {
int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
for(int i=pos1;i>=pos2+;i--){
p.state[i].num=p.state[i-].num;
p.state[i].flag=p.state[i-].flag;
}
p.state[pos2+].num=tmp,p.state[pos2+].flag=flag;
}
}
return p;
} void bfs()
{
memset(mark,false,sizeof(mark));
queue<Node>que;
que.push(st);
mark[Get_Hash(st)]=true;
while(!que.empty()){
Node q,pp,p=que.front();
que.pop();
if(Judge(p)){
printf("%d\n",p.step);
return ;
}
for(int i=;i<;i++){
for(int j=;j<;j++)if(i!=j){
if(p.state[i].flag!=p.state[j].flag&&is_prime[abs(p.state[i].num)+abs(p.state[j].num)]){
for(int k=;k<;k++){
pp=p;
q=Get_Node(pp,i,j,k);
int val=Get_Hash(q);
q.step=p.step+;
if(!mark[val]){
mark[val]=true;
que.push(q);
}
}
}
}
}
}
puts("-1");
} int main()
{
int _case,t=;
scanf("%d",&_case);
while(_case--){
for(int i=;i<;i++){
scanf("%d",&st.state[i].num);
st.state[i].flag=(st.state[i].num>?:-);
}
st.step=;
printf("Case %d: ",t++);
bfs();
}
return ;
}
loj 1165(bfs+康托展开)的更多相关文章
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- Aizu0121 Seven Puzzle(bfs+康托展开)
https://vjudge.net/problem/Aizu-0121 比八数码要水的多,bfs. 但是做的时候我把康托展开记错了,wa了好几次. 附上康托展开博客详解:https://blog.c ...
- 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)
Description 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- hdu 1430(BFS+康托展开+映射+输出路径)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU_1430 魔板 【BFS+康托展开+置换】
一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 5012 bfs 康托展开
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
随机推荐
- 百度或者Google---SEO优化(转载)
google 和百度的技术差别: 1.百度还认不清哪个是原创的 2.google蜘蛛不够百度快 4.google排名结果随时变化 流量.权重.权威.内容.用户体验.用户关注度等等细节的排名,已表 达了 ...
- ProcDump
https://technet.microsoft.com/en-us/sysinternals/dd996900.aspx
- “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常[WCF]
未处理System.TypeInitializationException HResult=-2146233036 Message=“System.ServiceModel.Diagnostics ...
- PHP中array_chunk的用法
转自:http://cn2.php.net/manual/zh/function.array-chunk.php (PHP 4 >= 4.2.0, PHP 5) array_chunk — 将一 ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
- 异常:The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml or the jar files deployed with this application
The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml ...
- 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹
一, 最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...
- c# 如何使用wlanapi连接电脑到wifi
http://www.codeproject.com/Articles/72105/Manage-WiFi-with-Native-API-WIFI-on-Windows-XP-SP Introduc ...
- ssh连接慢的问题的解决?
<1>群中同学遇到的问题,我之前在uuwatch也遇到了同样的问题? 问个问题师兄们 突然之间 公司服务器连接很慢 连一个shell需要10几秒钟 服务器就在公司全是内网服务器, 我也不知 ...
- 每天一个命令day1【diff 命令】(具体实例看下一节)
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...