https://loj.ac/problem/6121

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+状压)的更多相关文章

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

    孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...

  2. 洛谷P4011 孤岛营救问题(状压+BFS)

    传送门 和网络流有半毛钱关系么…… 可以发现$n,m,p$都特别小,那么考虑状压,每一个状态表示位置以及钥匙的拥有情况,然后每次因为只能走一步,所以可以用bfs求出最优解 然后是某大佬说的注意点:每个 ...

  3. 【洛谷4011】孤岛营救问题(状压SPFA)

    点此看题面 大致题意: 有一个\(N*M\)的四联通迷宫,相邻两个可能互通,可能有一扇门,也可能有一堵墙.对于第\(i\)类的门,你需要有第\(i\)类的钥匙才可以通过.问你从\((1,1)\)到达\ ...

  4. hdu 5094 Maze (BFS+状压)

    题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi ...

  5. hdu 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

  6. 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP

    这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...

  7. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

  8. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  9. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

随机推荐

  1. 饥饿的牛(dp一维最大覆盖)

    问题 H: 饥饿的牛 时间限制: 1 Sec  内存限制: 128 MB提交: 12  解决: 12[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目 ...

  2. Python【每日一问】04

    问:a =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],求出列表a中所有奇数并构造新列表 答: 利用列表的元素下标遍历列表 a = [1, 2, 3, 4, 5, 6, 7, 8 ...

  3. 虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法

    使用VMware安装了CentOS虚拟机,克隆之后使用service network restart指令来重新启动网络服务时,会看到有eth0网卡不存在的提示.出现这种现象的原因是,很多Linux d ...

  4. Java笔记Spring(四)

    spring web项目启动入口 1.首先看一下传统Java Web的配置文件web.xml,网上找的一个,参考地址:https://blog.csdn.net/github_36301064/art ...

  5. 消息对话框 MessageBoxButtons

    消息对话框MessageBox经常用于向用户显示通知信息.例如,在操作过程中遇到错误或程序异常,经常会使用这种方式给用户以提示,它是特殊类型的对话框. 在C#中,MessageBox消息对话框位于Sy ...

  6. 【Linux】【Jenkins】编译过程中遇到ERROR: Failed to parse POMs的解决方案

    自动化构建的时候报错,网搜查询说是maven的jenkinks配置问题导致的.修改系统工具配置的maven配置就可以了 Started by user XX Building in workspace ...

  7. Android Studio2.0 教程从入门到精通Windows版 - 入门篇

    http://www.open-open.com/lib/view/open1468121363300.html 本文转自:深度开源(open-open.com)原文标题:Android Studio ...

  8. suse linux安装lrzsz

    1.从下面的网站下载 lrzsz-1.12.20.tar.gz http://www.filewatcher.com/m/lrzsz-0.12.20.tar.gz.280938.0.0.html 2. ...

  9. Delphi TXLSReadWriteII 导出EXCEL

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. centos 7 一键安装gitlab

    # cat /etc/redhat-release CentOS release 6.5 (Final) # strings /lib64/libc.so.6 |grep GLIBC_ 首先升级 如果 ...