hdu 1429
http://acm.hdu.edu.cn/showproblem.php?pid=1429
一个广搜的简单题吧,不过有意思的事这个题目用到了位运算,还有就是很恶心的MLE
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; int m,n,t;
char graph[][];
bool vis[][][<<];
int dic[][] = {-,,,,,-,,};
struct note{ int x,y,state,time; }loc;
queue<note>s;
/*
位运算就是在下面,对于每一个钥匙用二进制的0,1来存
*/
int bfs(int x,int y)
{
while(!s.empty())
s.pop();
loc.state = ;
loc.time = ;
loc.x = x;
loc.y = y;
s.push(loc);
while(!s.empty())
{
note fa,son;
fa = s.front();
s.pop();
// if(fa.time == t) return -1;
for(int i = ; i < ; i++)
{
son.x = fa.x + dic[ i ][ ];
son.y = fa.y + dic[ i ][ ];
if(son.x < || son.y < || son.y > n || son.x > m||!vis[son.x][son.y][fa.state]) continue; //这一行很关键,没有这一行就是MLE
if(graph[son.x][son.y] == '^')
{
if(fa.time == t ) return -;
else return fa.time;
}
if(graph[son.x][son.y] >='a'&& graph[son.x][son.y] <='z' &&vis[son.x][son.y][fa.state| ( <<(graph[son.x][son.y]-'a'))])
{
son.state = fa.state | ( <<(graph[son.x][son.y]-'a'));
son.time = fa.time+;
vis[son.x][son.y][son.state] = false;
s.push(son);
}
else if(graph[son.x][son.y]<='Z' && graph[son.x][son.y] >='A' && fa.state & ( << graph[son.x][son.y]-'A') &&vis[son.x][son.y][fa.state])
{
son.state = fa.state;
son.time = fa.time+;
vis[son.x][son.y][son.state] = false;
s.push(son);
}
else if(graph[son.x][son.y]=='.'||graph[son.x][son.y]=='@'&&vis[son.x][son.y][fa.state])
{
son.state = fa.state;
son.time = fa.time + ;
vis[son.x][son.y][son.state] = false;
s.push(son);
}
}
}
return -;
} int main()
{
// freopen("in.txt","r",stdin);
int x,y;
while(~scanf("%d%d%d",&m,&n,&t))
{
getchar(); //吃掉一个换行符
memset(graph,,sizeof(graph));
memset(vis,true,sizeof(vis));
int ans;
for(int i = ; i <= m; i++)
{
for(int j = ; j <= n ; j++)
{
scanf("%c",&graph[i][j]);
if(graph[i][j]=='@')
x = i,y = j;
}
getchar();
}
ans = bfs(x,y);
printf("%d\n",ans);
}
return ;
}
hdu 1429的更多相关文章
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- hdu 1429 (bfs+状态压缩) 胜利大逃亡续
http://acm.hdu.edu.cn/showproblem.php?pid=1429 典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到 ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
随机推荐
- Android -- 获取网络数据并将数据存到本地数据库中
public static final int downloadDone = 1; // 用户model数组 ArrayList<Loginer> loginers = new Array ...
- read properties
读取配置文件在项目中使用频率很大,但是实际项目中各种人,各种用法,五花八门,往往是一种方式的各种变体,然很多种方式是其中一种方式的复杂化.今天我来总结下读取配置文件的集中方式及一些不能靠copy代码能 ...
- css学习笔记(2)
----------------|-------------------------|------------------ 标签名 英文全拼 中文 ...
- Scala中None, Nil, Nothing的区别
Nil是一个空的List None是一个object,是Option的子类型 List[Nothing]
- UICollectionViewCell 网格显示数据
using System; using System.Collections.Generic; using Foundation; using UIKit; namespace ddd { publi ...
- 14 Iterator和for...of循环
Iterator和for...of循环 首先 Iterator 是一个接口. 标准是 function makeIterator(array) { var nextIndex = 0; return ...
- 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)
目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...
- wamp 局域网访问
1.关闭防火墙 2.设置apache 的httpd.conf 第278行 正确代码如下 # onlineoffline tag - don't remove Require all granted O ...
- Java集合之LinkedList
一.LinkedList概述 1.初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链 ...
- CDDA 源码解析
一.编译1:从 https://github.com/CleverRaven/Cataclysm-DDA 下载源码2:下载IDE CodeBlocks,http://pan.baidu.com/s/1 ...