http://acm.hdu.edu.cn/showproblem.php?pid=5094

bfs,vis[x][y][z],z表示钥匙的状态,用二进制来表示,key[x][y]储存当前位置钥匙的二进制表示。

注意起始点有钥匙的情况。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct point
{
int x,y,counts,mykey;
}start; int n,m,p,k,s,flag,door[][][][],key[][],vis[][][],dir[][] = {-,,,-,,,,}; int main()
{
while(~scanf("%d%d%d",&n,&m,&p))
{
memset(door,-,sizeof(door));
memset(key,,sizeof(key));
memset(vis,,sizeof(vis));
scanf("%d",&k);
int x1,y1,x2,y2,type;
while(k--)
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&type);
door[x1][y1][x2][y2] = type;
door[x2][y2][x1][y1] = type;
}
scanf("%d",&s);
int x,y,keytype;
while(s--)
{
scanf("%d%d%d",&x,&y,&keytype);
key[x][y] |= <<(keytype-);
}
queue<point> q;
start.x = ;
start.y = ;
start.counts = ;
start.mykey = key[][];
vis[][][start.mykey] = ;
q.push(start);
flag = ;
while(!q.empty())
{ point now = q.front();
q.pop();
if(now.x == n && now.y == m)
{
flag = ;
printf("%d\n",now.counts);
break;
}
for(int i = ;i < ;i++)
{
point temp;
temp.x = now.x+dir[i][];
temp.y = now.y+dir[i][];
if(temp.x < || temp.x > n || temp.y < || temp.y > m) continue;
if(door[now.x][now.y][temp.x][temp.y] != - &&(now.mykey & <<(door[now.x][now.y][temp.x][temp.y]-)) == ) continue;
temp.counts = now.counts+;
temp.mykey = now.mykey | key[temp.x][temp.y];
if(vis[temp.x][temp.y][temp.mykey]) continue;
vis[temp.x][temp.y][temp.mykey] = ;
q.push(temp);
}
}
if(flag) printf("-1\n");
}
}

HDU_5094_dfs的更多相关文章

随机推荐

  1. spring boot 中AOP的使用

    一.AOP统一处理请求日志 也谈AOP 1.AOP是一种编程范式 2.与语言无关,是一种程序设计思想 面向切面(AOP)Aspect Oriented Programming 面向对象(OOP)Obj ...

  2. 深度学习论文翻译解析(六):MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications

    论文标题:MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications 论文作者:Andrew ...

  3. Java并发-几种常见的锁

    这几天在忙着投提前批内推,前面看的好多东西没有总结,正好这两天补上顺带复习一下 synchronized:Java之重型锁

  4. 2018湘潭邀请赛 AFK题解 其他待补...

    A.HDU6276:Easy h-index Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. Redis 高可用之"持久化"

    Redis高可用概述 在Redis中,实现高可用的技术主要包括:持久化.复制(读写分离).哨兵.集群. 持久化: 持久化是最简单的高可用方法(有时甚至不被归为高可用手段),主要作用是数据备份,即将数据 ...

  6. P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小

    P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...

  7. Iaas/paas/saas 三种模式分别都是做什么?

    任何一个在互联网上提供其服务的公司都可以叫做云计算公司.其实云计算分几层的,分别是Infrastructure(基础设施)-as-a- Service,Platform(平台)-as-a-Servic ...

  8. vue学习笔记1:el 与 data

    一.vue介绍 vue是目前三大主流框架之一(React.Angular.Vue) vue特点: 易用 灵活 高效 vue官网:官网链接 二,知识点 vue实例选项: el 注:不能 让el直接管理h ...

  9. (分块)楼房重建 HYSBZ - 2957

    题意 长度为n的坐标轴上,从1-n上的每一点都有一栋楼房,楼房的初识高度都为0,每一天都有一栋楼房的高度被修改(也可以不变),一栋楼房能被看见当且仅当其最高点与远点的连线不会与其他之前连线相交,问你每 ...

  10. Thematic002.字符串专题

    目录 Trie字典树 KMP AC自动机 Manacher 回文自动机 后缀数组 后缀自动机 Trie字典树 概念 我们先来看看什么是Trie字典树 可以发现,这棵树的每一条边都有一个字符 有一些点是 ...