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把,所以把这个作为题目的突破口, ...
随机推荐
- 修改hosts文件在本地使域名解析到指定IP
# Additionally, comments (such as these) may be inserted on individual # lines or following the mac ...
- Wpf usercontrol dispose
窗口关闭时组件"析构": public UserControl() { InitializeComponent(); ...
- sql server 多列转多行实现方法
select * from b_workOrder select * from( SELECT work_order_id,work_level, roles,code FROM (SELECT wo ...
- Git中如何利用生成SSH个人公钥访问git仓库
Git中如何利用生成SSH个人公钥访问git仓库方法(这里以coding平台为例): 1. 获取 SSH 协议地址 在项目的代码页面点击 SSH 切换到 SSH 协议, 获得访问地址, 请使用这个地址 ...
- java编码
/** * @Comments:default:ISO-8859-1 -> UTF-8 乱码则转码 * @param: str(乱码字符),coding(default:ISO- ...
- [Tip]重写PanGestureRecognizer
目标 识别当前pan方向是指定方向 需要API 重写- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 获取所在view ...
- html5中画布和SVG的比较
SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...
- js自动轮播图片的两种循环方法(原创)
用5个div,布局从左到右5张图片,从左到右5个div分别指定ID为img1,img2,img3,img4,img5.(背景是relative,5个div是相对于背景absolute定位) 显示如下: ...
- 【10-25】intelliji ide 学习笔记
快捷键 /** alter+enter 导包,异常处理等提示 psvm 快速main函数 sout 快速sysout语句 fi 快速for循环 ctrl+d 重复一行 Ctrl+X 删除行 Ctrl+ ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...