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

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. topcoder srm 680 div1

    problem1 link 将限制按照$x$排序.那么$[upTo_{i}+1,upTo_{i+1}]$中数字个数为$quantity_{i+1}-quantity_{i}$.然后进行动态规划.$f[ ...

  2. Python3 tkinter基础 Label justify 多行字符串左对齐

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. php知识点-1

    global 是在函数内部 声明一个 函数外部的变量(即所谓的全局变量, 而所谓的超全局变量是指 像 $_POST, $GLOBALS等之类的自动系统变量) 的一个别名. 在函数内部使用 unset( ...

  4. 轻重搭配|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采 ...

  5. 如何查看sonarqube的版本

    Server Logs & System Info The System Info page is found at Administration > System. It gives ...

  6. cmd使用管理员权限运行,启动路径不是当前目录

    https://stackoverflow.com/questions/672693/windows-batch-file-starting-directory-when-run-as-admin B ...

  7. (转) The care and maintenance of your adviser

    The care and maintenance of your adviser Ever since the advent of graduate school, students have com ...

  8. Hadoop技术内幕1——源代码环境准备

    Hadoop核心 1.HDFS:高容错性.高伸缩性……,允许用户将Hadoop部署在廉价的硬件上,构建分布式系统 2.MapReduce:分布式计算框架,允许用户在不了解分布式系统底层细节的情况下,开 ...

  9. mysql行转列(多行转一列)

    场景 比如说一个订单对应多条数据,当状态(status)=1的时候,  数量(num)=25,当状态(status)=2的时候,  数量(num)=45,现在想用一条sql记录下不同状态对应的数量为多 ...

  10. hdu 3208 Integer’s Power 筛法

    Integer’s Power Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...