好久没写搜索题了,就当练手吧。

vis[][][1025]第三个维度用来维护不同key持有状态的访问情况。

对于只有钥匙没有对应门的位置,置为'.',避免不必要的状态分支。

//
// main.cpp
// hdu_1429
//
// Created by Luke on 16/10/8.
// Copyright © 2016年 Luke. All rights reserved.
// #include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
int n,m,t;
int ex,ey;
char Map[][];
int bit[];
struct Node
{
int y,x;
int key;
int step;
};
bool vis[][][];
int dir[][]={,,-,,,,,-};
bool ok(Node &te)
{
if(te.step>=t)
return false;
if(te.y<||te.y>=n||te.x<||te.x>=m)
return false;
if(vis[te.y][te.x][te.key])
return false;
if(Map[te.y][te.x]=='*')
return false;
if(Map[te.y][te.x]>='A'&&Map[te.y][te.x]<='J')
{
int fix=bit[Map[te.y][te.x]-'A'];
if(!(fix&te.key))
return false;
}
if(Map[te.y][te.x]>='a'&&Map[te.y][te.x]<='j')
te.key|=bit[Map[te.y][te.x]-'a'];
vis[te.y][te.x][te.key]=;
return true;
}
int bfs(int sy,int sx)
{
queue<Node> q;
q.push((Node){sy,sx,,});
memset(vis,,sizeof(vis));
Node now,nx;
while(!q.empty())
{
now=q.front(),q.pop();
if(now.y==ey&&now.x==ex)
return now.step;
for(int i=;i<;i++)
{
nx=now;
nx.y+=dir[i][],nx.x+=dir[i][];
nx.step++;
if(ok(nx))
q.push(nx);
}
}
return -;
}
int main(int argc, const char * argv[]) {
cin.sync_with_stdio(false);
bit[]=;
for(int i=;i<;i++)
bit[i]=bit[i-]<<;
while(cin>>n>>m>>t)
{
int sx,sy;
bool fix[];
memset(fix,,sizeof(fix));
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>Map[i][j];
if(Map[i][j]>='A'&&Map[i][j]<='J')
fix[Map[i][j]-'A']=;
if(Map[i][j]=='^')
ex=j,ey=i;
if(Map[i][j]=='@')
sx=j,sy=i;
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(Map[i][j]>='a'&&Map[i][j]<='j')
if(!fix[Map[i][j]-'a'])
Map[i][j]='.';
cout<<bfs(sy,sx)<<endl;
}
return ;
}

BFS+二进制状态压缩 hdu-1429的更多相关文章

  1. hdu 1429 bfs+二进制状态压缩

    开始时候只用了BFS,显然超时啊,必然在结构体里加一个数组什么的判重啊,开始用的一个BOOL数组,显然还是不行,复杂度高,每次都要遍历数组来判重:后百度之,学习了二进制状态压缩,其实就用一个二进制数来 ...

  2. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

  3. # 最短Hamilton路径(二进制状态压缩)

    最短Hamilton路径(二进制状态压缩) 题目描述:n个点的带权无向图,从0-n-1,求从起点0到终点n-1的最短Hamilton路径(Hamilton路径:从0-n-1不重不漏的每个点恰好进过一次 ...

  4. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  5. leetcode 864. 获取所有钥匙的最短路径(BFS,状态压缩)

    题目链接 864. 获取所有钥匙的最短路径 题意 给定起点,要求在最短步骤内收集完所有钥匙,遇到每把锁之前只有 有对应的钥匙才能够打开 思路 BFS+状态压缩典型题目 先确定起点和总的钥匙数目,其次难 ...

  6. Fiber Network ZOJ 1967(Floyd+二进制状态压缩)

    Description Several startup companies have decided to build a better Internet, called the "Fibe ...

  7. 二进制状态压缩dp(旅行商TSP)POJ3311

    http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  8. hdu Swipe Bo(bfs+状态压缩)错了多次的题

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成

    昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...

随机推荐

  1. CF1131D tarjan,拓扑

    题目链接 541div2 http://codeforces.com/contest/1131/problem/D 思路 给出n序列和m序列的相对大小关系 构造出最大值最小的序列 缩点+拓扑 小的向大 ...

  2. SP10707 COT2 - Count on a tree II 莫队

    链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...

  3. 最大公约数gcd与最小公倍数lcm

    最大公约数:gcd 最大公倍数:lcm gcd和lcm的性质:(我觉得主要是第三点性质) 若gcd (

  4. CentOS7设置定时任务 每隔30分钟执行一次命令

    ref   https://blog.csdn.net/xiangxianghehe/article/details/78149094 一.安装 crontabs服务并设置开机自启: yum inst ...

  5. 题解——loj6280 数列分块入门4 (分块)

    分块维护一个区间和 然后记得更新的时候左边角块的tag不要打错到右边角块 #include <cstdio> #include <algorithm> #include < ...

  6. 论文笔记:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention

    Show, Attend and Tell: Neural Image Caption Generation with Visual Attention 2018-08-10 10:15:06 Pap ...

  7. ORM之EF

    本文大部分内容截取自博客:  http://www.cnblogs.com/VolcanoCloud/p/4475119.html (一) 为什么用ORM 处理关系数据库时,我们依据由行和列组成的表, ...

  8. Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp

    E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. tarjan 缩点(模板)

    描述: 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 注:允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 思路: ...

  10. Spring boot 整合JSP开发步骤

    1. 新建Springboot项目,war <dependency> <groupId>org.springframework.boot</groupId> < ...