孤岛营救问题 (BFS+状压)

BFS + 状压
写过就好想,注意细节debug
#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(register int i = (l);i <= (r);i++)
#define down(i,l,r) for(register int i = (l);i >= (r);i--)
#define traversal_vedge(i) for(register int i = head[u]; i ;i = e[i].nxt)
#define ll long long
using namespace std;
int read
{
int x = , f = ; char ch = getchar();
while(ch < || ch > ) {if(ch == '-')f = -; ch = getchar();}
while(ch >= && ch <=) {x = * x + ch - ;ch = getchar();}
return x * f;
}
void write(int x)
{
if(x < ) x = -x,putchar('-');
if(x > ) write(x/);
putchar(x% + );
}
//-----------------------------------------------------------------
const int N = ;
int n,m,p,k,s;
int maps[N][N][N][N],cnt[N][N],type[N][N][N],vis[N][N][(<<N)];
int dx[] = {,,,-,},dy[] = {,-,,,}; void readdata()
{
n = read; m = read; p = read; k = read;
int x1,x2,y1,y2,G;
while(k--)
{
x1 = read; y1 = read; x2 = read; y2 = read; G = read;
if(G) maps[x1][y1][x2][y2] = maps[x2][y2][x1][y1] = G;
else maps[x1][y1][x2][y2] = maps[x2][y2][x1][y1] = -;
}
s = read; int q;
while(s--)
{
x1 = read; x2 = read; q = read;
type[x1][x2][++cnt[x1][x2]] = q;
}
} struct node{
int x,y,step,state;
}; int get_state(int x,int y)
{
int ans = ;
up(i,,cnt[x][y]) ans |= (<<(type[x][y][i] - ));
return ans;
} int bfs()
{
queue <node> q;
int s = get_state(,);
q.push((node){,,,s} );
vis[][][s] = ;
while(!q.empty())
{
node u = q.front(); q.pop();
int x = u.x,y = u.y;
if(x == n && y == m) return u.step;
up(i,,)
{
int nx = x + dx[i],ny = y + dy[i];
if(nx < || ny < || nx > n || ny > m) continue;
int obstacle = maps[x][y][nx][ny];
if(obstacle == -) continue;
if( obstacle > && (u.state & <<(obstacle - )) == ) continue;//debug obstacle - 1 <- 1 - obstacle
int ns = u.state|get_state(nx,ny);//degug u.state <- s;
if(vis[nx][ny][ns]) continue;
vis[nx][ny][ns] = ;
q.push( (node){nx,ny,u.step+,ns} );
}
}
return -;
} int main()
{
freopen("input.txt","r",stdin);
readdata();
printf("%d\n",bfs());
return ;
}
孤岛营救问题 (BFS+状压)的更多相关文章
- 孤岛营救问题(BFS+状压DP)
孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...
- 洛谷P4011 孤岛营救问题(状压+BFS)
传送门 和网络流有半毛钱关系么…… 可以发现$n,m,p$都特别小,那么考虑状压,每一个状态表示位置以及钥匙的拥有情况,然后每次因为只能走一步,所以可以用bfs求出最优解 然后是某大佬说的注意点:每个 ...
- 【洛谷4011】孤岛营救问题(状压SPFA)
点此看题面 大致题意: 有一个\(N*M\)的四联通迷宫,相邻两个可能互通,可能有一扇门,也可能有一堵墙.对于第\(i\)类的门,你需要有第\(i\)类的钥匙才可以通过.问你从\((1,1)\)到达\ ...
- hdu 5094 Maze (BFS+状压)
题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi 含义是(x1,y1)与(x2,y2)之间有gi ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP
这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
随机推荐
- Python发送微信消息
针对此 需要安装itchat第三方模块 采用pip安装就可以了 pip install itchat import itchatimport time#引入时间函数进行测试time_format= ...
- [UE4]Circular Throbber,圆形的、环形的动态图标
一.就是圆形的,转动的很多小圆点. 二.Circular Throbber.Appearance.Number Of Pieces:可以通知显示的小圆点个数. 三.Circular Throbber. ...
- Linux下安装jdk8步骤详述
作为Java开发人员,在Linux下安装一些开发工具是必备技能,本文以安装jdk为例,详细记录了每一步的操作命令,以供参考. 0.下载jdk8 登录网址:http://www.oracle.com/t ...
- 【学习】DataFrame&Series类【pandas】
参考链接:http://blog.csdn.net/yhb315279058/article/details/50226027 DataFrame类: DataFrame有四个重要的属性: index ...
- py库:把python打包成exe文件(pyinstaller)
http://blog.csdn.net/be_quiet_endeavor/article/details/73929077 用Pyinstaller把Python3.4程序打包成可执行文件exe ...
- Spring MVC 之 ContentNegotiatingViewResolver
我们已经知道 对于 RequestMappingInfoHandlerMapping, 它在对带有后缀的http 请求进行匹配的时候,如果找不到精确的pattern, 那么就会 pattern+.* ...
- 深度学习原理与框架-Tensorboard可视化展示(代码) 1.tf.reuse_default_graph(进行结构图的重置) 2.tf.summary.FileWriter(writer实例化) 3. write.add_graph(graph的写入) 4. tf.summary.merge_all(将summary进行合并) 5.write.add_summary(将所有summary)
1. tf.reuse_default_graph() # 对graph结构图进行清除和重置操作 2.tf.summary.FileWriter(path)构造writer实例化,以便进行后续的gra ...
- Selenium + Chrome headless 报ERROR:gpu_process_transport_factory.cc(1007)] Lost UI shared context 可忽略并配置不输出日志
Selenium不再推荐使用PhantomJS,会报如下警告 UserWarning: Selenium support for PhantomJS has been deprecated, plea ...
- Python 高阶函数map(),filter(),reduce()
map()函数,接收两个参数,一个是函数,一个是序列,map()把传入的函数依次作用于序列的每个元素,并把结果作为新的序列返回: aa = [1, 2, 3, 4, 5] print("ma ...
- linux下启动tomcat服务
Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/local ...