POJ 2046 Gap 搜索- 状态压缩
题目地址: http://poj.org/problem?id=2046
一道搜索状态压缩的题目,关键是怎样hash。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=2222222;
const LL II=1000000007;
const int M=1000007; struct xh
{
int maps[4][8],step;
}w,e,vv[500000]; int aim[4][8]=//目标状态
{
11,12,13,14,15,16,17,0,
21,22,23,24,25,26,27,0,
31,32,33,34,35,36,37,0,
41,42,43,44,45,46,47,0
}; int s[80];
int vis[M]; int gethash(int t[][8])
{
int i,j,k=0,hash=0;
for(i=0;i<4;i++)
for(j=0;j<8;j++)
{
s[k++]=t[i][j]/10;
s[k++]=t[i][j]%10;
//转化成数字,hash
}
for(i=0;i<k;i++)//hash
{
hash=hash*7+s[i];
}
hash=(hash&0x7fffffff)%M;
return hash;
} void chang(int num,int i,int j)
{
int a,b;
for(a=0;a<4;a++)
for(b=1;b<8;b++)
{
if(w.maps[a][b]==num)
{
swap(w.maps[a][b],w.maps[i][j]);
return ;
}
}
} bool test(xh a)
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<8;j++)
if(a.maps[i][j]!=aim[i][j])
return false;
return true;
} void bfs()
{
int i,j,hash,head=0,tail=0;
memset(vis,false,sizeof(vis));
w.maps[0][0]=11;
w.maps[1][0]=21;
w.maps[2][0]=31;
w.maps[3][0]=41;
w.step=0;
vv[tail++]=w;
hash=gethash(w.maps);
vis[hash]=true;
while(head!=tail)
{
e=vv[head++];
if(test(e))
{
printf("%d\n",e.step);
return ;
}
for(i=0;i<4;i++)
for(j=1;j<8;j++)
{
w=e;
if(w.maps[i][j]!=0) continue;
int num=w.maps[i][j-1];//前一个
if(num==0||num==17||num==27||num==37||num==47) continue;
chang(num+1,i,j);
hash=gethash(w.maps);
if(!vis[hash])
{
vis[hash]=true;
w.step++;
vv[tail++]=w;
}
}
}
printf("-1\n");
} int main()
{
int i,j,T,k;
cin>>T;
while(T--)
{
memset(w.maps,0,sizeof(w.maps));
for(i=0;i<4;i++)
for(j=1;j<8;j++)
{
scanf("%d",&k);
w.maps[i][j]=k;
if(k==11||k==21||k==31||k==41)
w.maps[i][j]=0;
}
bfs();
}
return 0;
}
POJ 2046 Gap 搜索- 状态压缩的更多相关文章
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3691 (AC自动机+状态压缩DP)
题目链接: http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- poj 2046 Gap
题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap. You have 28 c ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 1321 棋盘问题(状态压缩DP)
不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...
- POJ 3254 Corn Fields 状态压缩
这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
随机推荐
- Linux下文件特殊权限
SUIDSUID表示在所有者的位置上出现了s在一个命令的所有者的权限上如果出现了s,当其他人在执行该命令的时候将具有所有者的权限.SUID权限仅对二进制文件有效 SGID表示在组的位置上出现了s如果一 ...
- 前端canvas合并图片两种实现方式
---恢复内容开始--- 需求: 有一个固定的背景图,还有一个是随机生成的二维码,合并成一张图,让用户下载. 实现一:纯手写,这里为了省事生成图片我直接给的base64,其实使用qrcode.js生成 ...
- char *s 和char s[]的区别
char *s 和 char s[] 的区别小结 博客分类: C语言 c教育 . 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区( ...
- LoadRunner中常用的字符串操作函数
LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); strc ...
- HTML中的ul, ol,li , dl,dt, dd标签
ul: unordered lists ol: ordered lists li: Lists ol 有序列表. <ol><li>……</li><li> ...
- HDU 6194【后缀数组】
题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=6194] 题意: 给你一个长度不大于1e5的字符串,然后然你判断其子串严格出现k次的子串个数. 题解: ...
- 【BZOJ 3534】 3534: [Sdoi2014]重建 (Matrix-Tree Theorem)
3534: [Sdoi2014]重建 Time Limit: 10 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 709 Solved: 32 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- 对Java Serializable(序列化)的理解和总结(二)
遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题a,什么叫序列化和反序列化 b,作用.为啥要实现这个 Serializable 接口,也就是为啥要序列化 c,ser ...
- 使用辗转相除法求两个数的最大公因数(python实现)
数学背景: 整除的定义: 任给两个整数a,b,其中b≠0,如果存在一个整数q使得等式 a = bq 成立,我们就说是b整除 ...