一般地图很小,状态不多,可以装压或者hash,构造压缩或hash的函数,构造还原地图的函数,然后就无脑bfs(感觉就是SPFA)
题目:
1.玩具游戏:二进制压缩状态
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int s,e;
bool vis[1<<17];
int dis[1<<17],map[5][5];
int dy[4]={0,0,1,-1};
int dx[4]={1,-1,0,0};
inline void div(int x){
for(int i=4;i>=1;--i){
for(int j=4;j>=1;--j){
map[i][j]=x&1;
x>>=1;
}
}
}
inline int zip(){
int x=0;
for(int i=1;i<=4;++i){
for(int j=1;j<=4;++j){
x<<=1;
x|=map[i][j];
}
}
return x;
}
inline void bfs(){
queue<int>q;
memset(dis,0x3f,sizeof(dis));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=0;
div(x);
for(int i=1;i<=4;++i){
for(int j=1;j<=4;++j){
if(map[i][j]!=1)continue;
for(int k=0;k<4;++k){
int xx=dx[k]+i;
int yy=dy[k]+j;
if(xx<1||xx>4||yy<1||yy>4)continue;
if(map[xx][yy])continue;
swap(map[xx][yy],map[i][j]);
int w=zip();
swap(map[xx][yy],map[i][j]);
if(dis[w]>dis[x]+1){
dis[w]=dis[x]+1;
if(!vis[w]){
q.push(w);
vis[w]=1;
}
}
}
}
}
}
}
int main(){
for(int i=1;i<=4;++i){
char t[10];
scanf("%s",t+1);
for(int j=1;j<=4;++j){
s<<=1;
if(t[j]=='1')s|=1;
}
}
for(int i=1;i<=4;++i){
char t[10];
scanf("%s",t+1);
for(int j=1;j<=4;++j){
e<<=1;
if(t[j]=='1')e|=1;
}
}
bfs();
cout<<dis[e]<<endl;
return 0;
}
2.八数码难题:偷懒 map来hash存状态,转移很少9!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
using namespace std;
long long s,t;
int tot,dis[402880],m[4][4];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
map<long long ,int >h;
bool v[20],vis[400000];
inline long long poww(long long a,long long b){
long long ans=1;
while(b){
if(b&1)ans*=a;
a*=a;
b>>=1;
}
return ans;
}
inline void dfs(int pos,long long val){
if(pos>9){
h[val]=++tot;
return ;
}
for(int i=0;i<=8;++i){
if(v[i])continue;
v[i]=1;
dfs(pos+1,val+i*poww(10,pos-1));
v[i]=0;
}
}
inline void div(long long x){
for(int i=3;i>=1;--i){
for(int j=3;j>=1;--j){
m[i][j]=x%10;
x/=10;
}
}
}
inline long long zip(){
long long x=0;
for(int i=1;i<=3;++i){
for(int j=1;j<=3;++j){
x*=10;
x+=m[i][j];
}
}
return x+1000000000;
}
inline void bfs(){
queue<long long>q;
memset(dis,0x3f,sizeof(dis));
int ss=h[s];
vis[ss]=1;
dis[ss]=0;
q.push(s);
while(q.size()){
long long x=q.front();
q.pop();
vis[h[x]]=0;
div(x);
for(int i=1;i<=3;++i){
for(int j=1;j<=3;++j){
if(m[i][j])continue;
for(int k=0;k<4;++k){
int xx=dx[k]+i;
int yy=dy[k]+j;
if(xx<1||xx>3||yy<1||yy>3)continue;
if(!m[xx][yy])continue;
swap(m[xx][yy],m[i][j]);
long long temp=zip();
swap(m[xx][yy],m[i][j]);
int u=h[x];
int v=h[temp];
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
if(!vis[v]){
vis[v]=1;
q.push(temp);
}
}
}
}
}
}
}
int main(){
dfs(1,1000000000);
cin>>s;
s+=1000000000;
t=1123804765;
bfs();
cout<<dis[h[t]];
return 0;
}

压状态bfs的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS

    BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...

  3. UVA1600 状态BFS

    刚开是我用了一种很笨的bfs过掉的,后来看到原来还可以三维带状态BFS,觉得是一个不错的思路. d[x][y][k]表示坐标位于(x,y)经过K个障碍到达时的最短路径,当然如果(x,y)处的数字是0就 ...

  4. POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)

    The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...

  5. 【状压+状态转移】A Famous Airport Managere

    https://www.bnuoj.com/v3/problem_show.php?pid=25653 [题意] 给定一个3*3的九宫格,模拟一个停机坪.第一个格子一定是'*',代表take off ...

  6. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

  7. HDU 2209 翻纸牌游戏 状态BFS

    翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem De ...

  8. 搜索(另类状态BFS):NOIP 华容道

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  9. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

随机推荐

  1. CentOS7.4-btrfs管理及使用

    btrfs, B-tree File System, GPL开源文件系统, 支持CoW即读时写入. 核心特性: 多物理卷支持; btrfs可由多个底层磁盘组成 支持RAID mkfs.btrfs 命令 ...

  2. 记录一次Permission denied解决过程

    热烈推荐:超多IT资源,尽在798资源网 朋友网站碰到一个奇怪的问题,程序所在目录无法进行上传操作. 具体细节是这样的:网站所在目录 /www/web/xiangmua 路径下,/www/web 都可 ...

  3. ubuntu 安装Gremlin 的图形化环境

    参考文档:https://www.jianshu.com/p/618cf6667381 部署HugeGraphServer # 直接下载release包 网址:https://github.com/h ...

  4. 洛谷 P1096 Hanoi双塔问题

    P1096 Hanoi双塔问题 题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情 ...

  5. js使用offsetHeight获取div高度为0的问题

    今晚试了好久没弄出来,后来获取子一层的div就能获取到高度了 我的情况是这样的:我在最外面写一个<div id="mainBody">,  里面写bootstrap的d ...

  6. JVM学习心得

    出处:http://blog.csdn.net/qq_16143915/article/details/51195438 一.JAVA内存管理与GC机制 Java在JVM所虚拟出的内存环境中执行,ja ...

  7. spring定时器中如何获取servletcontext

    spring定时器中如何获取servletcontext 学习了:https://zhidao.baidu.com/question/406212574.html @Scheduled(cron = ...

  8. Equals和==比較

    总结一下: 对于字符串来说.两个比較的都是对象的值,而且是等效的,这是由于MS重写了==运算符和Equals方法所致             对于非字符串的其它引用类型(非匿名类型)两个比較的都是对象 ...

  9. poj 2240 Bellman-Flod 求环

    http://poj.org/problem?id=2240 深刻体现了自己代码能力有问题外加改模板能力有问题.外加Debug有问题.以后做到: 1.算法原理能够轻易弄出来. 2.代码模板自己收集各种 ...

  10. TCP/IP具体解释--TCP的分段和IP的分片

    写在前面: 分组能够发生在运输层和网络层.运输层中的TCP会分段,网络层中的IP会分片.IP层的分片很多其它的是为运输层的UDP服务的,因为TCP自己会避免IP的分片,所以使用TCP传输在IP层都不会 ...