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

典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到了2号钥匙

那么原有的00000变为了00010,当到大了对应的二号门的时候,利用位运算00010来&上1<<2也就是00010就是非0,如果不是二号门的时候

那么00010&上1<<x都是0,所以此时其它门都进不去,如果再拿到了三号钥匙加上之后变成了00110,代表二号三号钥匙都拿到了,然后到了三号门的

时候00110&(1<<3)00100或者到了二号门的时候00110&(1<<2)00010都是非0的,到其他门都是为0的-----

这样就很好的解决了钥匙的记录问题,这里有10把钥匙,所以数组要大于2的10次方

code

 #include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int dx[]={,-,,};
int dy[]={,,-,};
char dzm[]={'A','B','C','D','E','F','G','H','I','J'};
char xzm[]={'a','b','c','d','e','f','g','h','i','j'};
struct point {
int x,y;
int step,dir;
};
int n,m,sx,sy;
int visit[][][];//记录钥匙
char map[][];
int bfs()
{
int i,j;
memset(visit,,sizeof(visit));
queue<point>Q;
point now,next;
now.x=sx;now.y=sy;
now.step=now.dir=;
visit[now.x][now.y][now.dir]=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop();
if (map[now.x][now.y]=='^')
return now.step;
for (i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.step=now.step+;
next.dir=now.dir;
if (map[next.x][next.y]=='*')continue;
if (next.x<||next.x>n||next.y<||next.y>m)continue;
if (map[next.x][next.y]<='J'&&map[next.x][next.y]>='A')
{
for (j=;j<;j++)
{
if (map[next.x][next.y]==dzm[j])
{
if ((next.dir&(<<j))!=)
{
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
}
else if (map[next.x][next.y]<='j'&&map[next.x][next.y]>='a')
{
for (j=;j<;j++)
{
if (map[next.x][next.y]==xzm[j])
{
if ((next.dir&(<<j))==)
next.dir+=(<<j);
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
else
{
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
return -;
}
int main()
{
int i,j,t;
while (~scanf("%d %d %d",&n,&m,&t))
{
getchar();
for (i=;i<=n;i++)
{
for (j=;j<=m;j++)
{
scanf(" %c",&map[i][j]);
if (map[i][j]=='@')
sx=i,sy=j;
}
}
int q=bfs();
if (q>=t)
printf("-1\n");
else
printf("%d\n",q);
}
return ;
}

hdu 1429 (bfs+状态压缩) 胜利大逃亡续的更多相关文章

  1. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  2. HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

  3. hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】

    题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...

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

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

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

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

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

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

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

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

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

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

  9. HDU 1429 胜利大逃亡(续)(bfs)

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

随机推荐

  1. ROS:ROS操作类MK.cs

    class MK { Stream connection; TcpClient con; public MK(string ip,int port) { con = new TcpClient(); ...

  2. 判断素数(翁凯男神MOOC)

    从2到x-1测试是否可以整除 int isPrime(int x); int main(int argc, char **argv) { int x; scanf("%d",&am ...

  3. js 乘法 4.39*100 出现值不对问题解决

    https://www.jianshu.com/p/a026245661bb //除法函数,用来得到精确的除法结果 //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显. ...

  4. 解决iframe在iphone不兼容的问题

    <div class="scroll-wrapper"> <iframe src="地址"></iframe> </d ...

  5. kafka清理

    由于项目原因,最近经常碰到Kafka消息队列拥堵的情况.碰到这种情况为了不影响在线系统的正常使用,需要大家手动的清理Kafka Log.但是清理Kafka Log又不能单纯的去删除中间环节产生的日志, ...

  6. 求前n项的斐波那契数列、求两个数的最小公倍数、求两个数的最大公约数

    class Fib(object):    def __call__(self,n):        a=[0,1]        for i in range(n-2):            an ...

  7. sass 的安装 http://blog.csdn.net/weixin_38362146/article/details/78035971?locationNum=10&fps=1

    http://blog.csdn.net/weixin_38362146/article/details/78035971?locationNum=10&fps=1

  8. AlertDialog 无法去掉自带的白边

    项目中开始采用AlertDialog ,根据要求要显示加圆角.但是设置圆角的背景后,会存在白边. 按网上提示的设置透明的背景都不可以. 最后采用Dialog.

  9. SpringBoot @Aspect

    1.添加maven依赖注解 <!--springBoot的aop--> <dependency> <groupId>org.springframework.boot ...

  10. Link & Redirect

    [Link] Link标签,用于实现React-Router功能的跳转.(意思是就不要使用a标签了) 1)to:string,指明要跳转的path. import { Link } from 'rea ...