孤岛营救问题 (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个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
随机推荐
- npm安装与使用
NPM 使用介绍 摘自:http://www.runoob.com/nodejs/nodejs-npm.html NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题, ...
- 长短记忆神经网络LSTM
转载: https://www.jianshu.com/p/dcec3f07d3b5 https://blog.csdn.net/dream_catcher_10/article/details/48 ...
- C#获取当前路径的七种方法
//1.获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // ...
- Vue 封装的noData组件
<template> <div :style="{color: fontColor}" :class="['noDataView', iconType] ...
- 可视化神器--Plotly
数据分析离不开数据可视化.我们最常用的就是pandas,matplotlib,pyecharts当然还有Tableau,看到一篇文章介绍plotly制图后我也跃跃欲试,查看了相关资料开始学习plotl ...
- #161: 给定n*n由0和1组成的矩阵,如果矩阵的每一行和每一列的1的数量都是偶数,则认为符合条件。 你的任务就是检测矩阵是否符合条件
试题描述 给定n*n由0和1组成的矩阵,如果矩阵的每一行和每一列的1的数量都是偶数,则认为符合条件. 你的任务就是检测矩阵是否符合条件,或者在仅改变一个矩阵元素的情况下能否符合条件. "改变 ...
- 说下spring生命周期
面试官:说下spring生命周期 程序员:不会 那你先回去等消息吧 Bean实现了BeanNameAware,Spring会将Bean的ID透传给setBeanName java.后端开发.程 ...
- 网络编程 tftp下载文件的编程
一.代码 操作码 功能 1 读请求,即下载 2 写请求,即上传 3 表示数据包,即DATA 4 确认码,即ACK 5 错误 from socket import * import struct s=s ...
- linux 僵尸进程查看方式
ps -A -ostat,ppid,pid,cmd |grep -e '^[Zz]' # 结果 Z 169925 49893 [sw] <defunct> Z 169925 120690 ...
- ---mingw Linux交叉编译给Window的工具
https://arrayfire.com/cross-compile-to-windows-from-linux/