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 ...
随机推荐
- awk练习总结
>>> >>>awk是个优秀文本处理工具,可以说是一门程序设计语言.下面是awk内置变量. 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n ...
- [实战]MVC5+EF6+MySql企业网盘实战(21)——网盘操作日志
写在前面 上篇文章介绍了一个bootstrap的分页插件,这篇将弄一个完整的例子,就以日志分页为例说明如何请求服务端然后进行分页. 系列文章 [EF]vs15+ef6+mysql code first ...
- SSH整合最简单的一个例子
1.新建mysql数据库 create database spring; 切换数据库 use spring; 新建表 create table user (id int(3) auto_incre ...
- ssm demo,用户角色权限管理
SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...
- 通过JS实现HTML的转义与反转义
function HTMLEncode(html) { var temp = document.createElement("div"); (temp.textContent != ...
- Python实现简单的API接口
get方法 代码实现 # coding:utf-8 import json from urlparse import parse_qs from wsgiref.simple_ ...
- Scrapy实战篇(六)之Scrapy配合Selenium爬取京东信息(上)
在之前的一篇实战之中,我们已经爬取过京东商城的文胸数据,但是前面的那一篇其实是有一个缺陷的,不知道你看出来没有,下面就来详细的说明和解决这个缺陷. 我们在京东搜索页面输入关键字进行搜索的时候,页面的返 ...
- [BOI2004]Sequence
题面描述 给定整数数组$a_1,a_2,a_3...a_n$,求递增数组$b_1,b_2,b_3...b_n$ 使得$|a_1 - b_1| + |a_2 - b_2| + ... + |a_n - ...
- [BZOJ4012][HNOI2015]开店(动态点分治,树链剖分)
4012: [HNOI2015]开店 Time Limit: 70 Sec Memory Limit: 512 MBSubmit: 2168 Solved: 947[Submit][Status] ...
- bzoj 1051 强连通分量
反建图,计算强连通分量,将每个分量看成一个点,缩点后的图是一个DAG,如果是一棵树,则根代表的连通分量的大小就是答案,否则答案为0. 收获: 图的东西如果不好解决,可以尝试缩点(有向图将每个强连通分量 ...