传送门

题意

给出一个迷宫,门需要钥匙来打开,t秒内能否从起点到达终点

分析

这题我用以前一道题的代码改了改就过了,具体思想:设置vis[status][x][y],status记录到达该点拥有的钥匙,每次更新,这样可以保证最短

trick

代码

/*
每个点在拥有相同钥匙(状态)下都只走一遍,这样可以最短
*/
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; #define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#define cpy(a,b) memcpy(a,b,sizeof(b))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}} int n,m,ans,t;
char s[25][25];
bool vis[2048][25][25];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
int x,y,status,time;
node(){}
node(int xx,int yy,int s,int t)
{
x=xx,y=yy,status=s,time=t;
}
}tmp,p;
int bfs(int sx,int sy)
{
mem(vis,0);
queue<node>q;
p={sx,sy,0,0};
q.push(p);
while(!q.empty())
{
tmp=q.front();
q.pop();
for(int i=0;i<4;++i)
{
int x=tmp.x+dir[i][0],y=tmp.y+dir[i][1],status=tmp.status,time=tmp.time;
if(x>=0&&x<n&&y>=0&&y<m&&s[x][y]!='*'&&!vis[status][x][y])
{
if(s[x][y]>='A'&&s[x][y]<='J')
{
int ret=s[x][y]-'A';
if(status&(1<<ret))
{
vis[status][x][y]=1;
q.push(node(x,y,status,time+1));
}
}
else if(s[x][y]>='a'&&s[x][y]<='j')
{
int ret=s[x][y]-'a';
status|=(1<<ret);
vis[status][x][y]=1;
q.push(node(x,y,status,time+1));
}
else if(s[x][y]=='.')
{
vis[status][x][y]=1;
q.push(node(x,y,status,time+1));
}
else if(s[x][y]=='^') return time+1;
}
}
}
return -1;
}
int main()
{
int sx,sy;
while(scanf("%d %d %d",&n,&m,&t)==3)
{
R(i,0,n)
{
scanf("%s",&s[i]);
R(j,0,m) if(s[i][j]=='@') { sx=i;sy=j;s[i][j]='.'; break; }
}
ans=bfs(sx,sy);
if(ans==-1||ans>=t) puts("-1");else printf("%d\n",ans);
}
return 0;
}

HDU1429:胜利大逃亡(续)的更多相关文章

  1. Hdu1429 胜利大逃亡(续) 2017-01-20 18:33 53人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  2. hdu1429 胜利大逃亡(续) 【BFS】+【状态压缩】

    题目链接:https://vjudge.net/contest/84620#problem/K 题目大意:一个人从起点走到终点,问他是否能够在规定的时间走到,在走向终点的路线上,可能会有一些障碍门,他 ...

  3. 胜利大逃亡(续)hdu1429(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. hdu 1429 胜利大逃亡(续)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...

  6. 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 ...

  7. HDOJ 1429 胜利大逃亡(续)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. HDU 1429 胜利大逃亡(续)(DP + 状态压缩)

    胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...

  10. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 12.1——类的定义与声明,隐含的this指针

    类的定义与声明: (1)将const放在成员函数的形参列表之后,可以将将成员函数声明为常量,而它的意思是函数不能改变所操作的数据成员 这里必须在声明和定义处都加上const. (2)成员函数有一个隐含 ...

  2. spring mvc技术

                                                     spring mvc之访问路径        1. @RequestMapping这个注解  在实际项 ...

  3. SeaGlass:手工搭建伪基站监控系统

    “伪基站”即假基站,设备一般由主机和笔记本电脑或手机组成,通过短信群发器.短信发信机等相关设备能够搜取以其为中心.一定半径范围内的手机卡信息,利用2G移动通信的缺陷,通过伪装成运营商的基站,冒用他人手 ...

  4. There is no PasswordEncoder mapped for the id "null"

    There is no PasswordEncoder mapped for the id "null" 学习了:https://blog.csdn.net/dream_an/ar ...

  5. Office EXCEL 如何设置最大行高

    对于单个单元格行来说,行高必须在0-409之间   但是如果合并了两个单元格,则行高就扩展了一倍,不止409,而是两倍的409.

  6. Visual Studio VS如何修改代码字体

    工具-选项-环境-字体和颜色

  7. eclipse Alt+/ 不能提示

    普通情况下alt+/有代码提示作用,还有代码提示的快捷代码也不是alt+/,因此要恢复代码提示用alt+/.须要做两件事.  在 Window - Preferences - General - Ke ...

  8. php进一法取整、四舍五入取整、忽略小数等的取整数方法大全

    PHP取整数函数常用的四种方法,下面收集了四个函数:经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已--主要是:ceil,floor,round,intval PHP取整数函数常用 ...

  9. 找中位数O(n)算法

    题目描写叙述: 给定一个未排序的整数数组,找到当中位数. 中位数是排序后数组的中间值,假设数组的个数是偶数个.则返回排序后数组的第N/2个数. 例子 给出数组[4, 5, 1, 2, 3], 返回 3 ...

  10. HDU 3469 Catching the Thief (博弈 + DP递推)

    Catching the Thief Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...