题目地址: 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 搜索- 状态压缩的更多相关文章

  1. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  2. POJ 3691 (AC自动机+状态压缩DP)

    题目链接:  http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...

  3. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  4. poj 2046 Gap

    题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap. You have 28 c ...

  5. POJ 2923 Relocation (状态压缩,01背包)

    题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...

  6. POJ 1321 棋盘问题(状态压缩DP)

    不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...

  7. POJ 3254 Corn Fields 状态压缩

    这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...

  8. POJ 3254 Corn Fields 状态压缩DP (C++/Java)

    id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...

  9. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

随机推荐

  1. PouchDB:JavaScript同步数据库

    简介 PouchDB是受 Apache CouchDB启发为Web设计的一款占用空间少的数据库.PouchDB项目的目标是帮助开发者构建线上和线下都能很好地使用的Web应用程序,在应用程序离线的时候, ...

  2. Spring中@Transactional事务回滚

    转载: Spring中@Transactional事务回滚 一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用.下面举个栗子:比如一个部 ...

  3. checkbox 更改样式

    html: <div class="wrap"> <p>1. 勾选</p> <input type="checkbox" ...

  4. cpp笔记(长期更新)

    c++0x:c++11标准成为正式标准之前的草案临时名字 多态:接口的多种不同的实现方式即为多态,即子类类型的指针赋值给父类类型的指针 动态内存(堆)的管理是通过一对运算符来完成的: new:在动态内 ...

  5. GNU Wget 1.19.4 for Windows

    资源地址:https://eternallybored.org/misc/wget/ 然后将工具目录加入环境变量

  6. CodeForces 785B Anton and Classes

    简单判断. 找第一类区间中$R$最大的,以及第二类区间中$L$最小的,判断距离. 找第二类区间中$R$最大的,以及第一类区间中$L$最小的,判断距离. 两种情况取个最大值即可. #include &l ...

  7. 洛谷P2151 [SDOI2009] HH去散步 [矩阵加速]

    题目传送门 HH去散步 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走 ...

  8. Xcode代码提示里的字母含义

    P -- 协议 M -- 成员方法 C -- 类 K -- 枚举 .常量 V -- 成员变量 T -- typedef类型 G -- 全局变量 f -- 函数 # -- #define指令

  9. C和指针之学习笔记(2)

    第6章 指针 1.在一组字符串中查找字符: #include<stdio.h> #include<assert.h> #include<stdlib.h> #def ...

  10. 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)

    3997: [TJOI2015]组合数学 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 919  Solved: 664 Description 给出 ...