uva10944 状态压缩bfs or DP

又是一道状压搜索,题解有的是状压DP做的目前不会日后补
写好了以后一直蜜汁WA,看别人代码把判断再次回到原点的语句写在了Q.pop()之后而不是for里,对我也是一种启发吧这样写确实有好处比如起点就是答案的情况也能处理,很方便,我i改过之后就A了。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f;
bool vis[22][22][(1<<16)+1];
char e[22][22];
int nut[22][22];
int N,M,Count,ans;
int fx[8][2]={1,0,-1,0,0,1,0,-1,1,-1,1,1,-1,1,-1,-1};
struct node
{
int x,y,hal,bs;
node(){}
node(int x,int y,int hal,int bs):x(x),y(y),hal(hal),bs(bs){}
};
void bfs(int sx,int sy)
{
memset(vis,0,sizeof(vis));
queue<node>Q;
node t1(sx,sy,0,0),t2;
Q.push(t1);
vis[sx][sy][0]=1;
while(!Q.empty()){
t1=Q.front(); Q.pop();
if(e[t1.x][t1.y]=='L'&&t1.hal==ans) {cout<<t1.bs<<endl;return;}
for(int i=0;i<8;++i){
t2.x=t1.x+fx[i][0];
t2.y=t1.y+fx[i][1];
t2.bs=t1.bs+1;
t2.hal=t1.hal;
if(t2.x>0&&t2.y>0&&e[t2.x][t2.y]=='#') t2.hal=(t2.hal|(1<<nut[t2.x][t2.y]));
if(t2.x<1||t2.y<1||t2.x>N||t2.y>M||vis[t2.x][t2.y][t2.hal]) continue;
vis[t2.x][t2.y][t2.hal]=1;
Q.push(t2);
}
}
}
int main()
{
int i,j,k;
while(scanf("%d%d",&N,&M)!=EOF){Count=0;
int sx,sy;
memset(e,0,sizeof(e));
memset(nut,-1,sizeof(nut));
for(i=1;i<=N;++i)
for(j=1;j<=M;++j){
scanf(" %c",&e[i][j]);
if(e[i][j]=='L') {sx=i;sy=j;}
else if (e[i][j]=='#'){
nut[i][j]=Count++;
}
}
if(Count==0) {puts("0");continue;}
ans=(1<<Count)-1;
bfs(sx,sy);
}
return 0;
}
/*
5 5
L....
#....
#....
.....
#....
*/
uva10944 状态压缩bfs or DP的更多相关文章
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...
- POJ 1753 Flip Game (状态压缩 bfs+位运算)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- HDU 5025 Saving Tang Monk 【状态压缩BFS】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...
- POJ - 1324 Holedox Moving (状态压缩+BFS/A*)
题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...
- 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)
首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...
- [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)
快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...
随机推荐
- The Rise of Database Sharding DATABASE SHARDING
w玻璃碎片.0共享 http://www.agildata.com/database-sharding/ The Rise of Database Sharding The concept of Da ...
- Feature extraction using convolution
http://ufldl.stanford.edu/wiki/index.php/Feature_extraction_using_convolution http://ufldl.stanford. ...
- poi根据模板导出word文档
POI结构与常用类 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI ...
- postman:模拟发送一个需要cookie认证的请求
1.chrome 已安装插件intercept. 由于chrome安全的限制,发不出带cookie的请求.如果想要发送带cookie的请求,需要开启Interceptor. 2.chrome 浏览器要 ...
- ROS 命令学习记录
#catkin_init_workspace //just get CMakeList.txt #catkin_make //增加了信文件夹 build和devel #Catkin_creat_pk ...
- SAP GUI常用快捷键
F1:帮助 F2:双击.比如TC行的双击,LIST行的双击等 F3:后退(Back),后退按钮 Shift+F3:退出(Exit),退出按钮 F4:搜索帮助 F8:执行 F10:菜单 F12:取消(C ...
- HDU1575:Tr A(矩阵快速幂模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1575 #include <iostream> #include <string.h> ...
- 牛客国庆集训派对Day2 Solution
A 矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...
- uva10817 dijkstra
大白书P330 #include <iostream> #include <cstdio> #include <algorithm> #include <st ...
- MySQL connector c++使用笔记
MySQL的connector官方地址: http://dev.mysql.com/downloads/connector/ 针对c++来说, 可以选择c或者c++的库. c++的实现是参考了java ...