zoj3675 BFS+状态压缩
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n;
int vis[];
int mode1,mode2;
struct node
{
int step,status;
};
void print(int x)
{
int tmp=x%;
if (!(x== || x==))
print(x>>);
printf("%d",tmp);
} int main()
{
int i,j,m,n;
char str[];
while (scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
scanf("%s",str);
scanf("%d",&m);
// printf("!!1");
mode1=mode2=;
//printf("%s\n",str);
for (i=;str[i]!='\0';i++)
mode1=(mode1<<)+(str[i]=='*'?:);
for (i=strlen(str)-;i>=;i--)
mode2=(mode2<<)+(str[i]=='*'?:);
// printf("mode1=%d\n",mode1); node tmp;
tmp.step=;
tmp.status=;
int end=(<<m)-;
queue<node> q;
q.push(tmp);
vis[tmp.status]=;
int full=;
for (i=;i<m;i++)
full=(full<<)+;
node tmp2;
int flag=;
while (!q.empty())
{
// printf("@@@\n");
tmp=q.front();
q.pop(); tmp.status=tmp.status<<(n-);
for (i=;i<(n+m);i++)
{
int nw=((tmp.status|(mode1<<i))>>(n-))&(full);
// printf("nw=");
// print(nw);
// printf(" %d\n",i);
if (vis[nw]==)
{
vis[nw]=;
tmp2.status=nw;
tmp2.step=tmp.step+;
if (tmp2.status==end)
{
printf("%d\n",tmp2.step);
flag=;
break;
} q.push(tmp2);
// printf("%d %d\n",tmp2.status,tmp2.step);
}
}
if (!flag) break;
for (i=;i<(n+m);i++)
{
int nw=((tmp.status|(mode2<<i))>>(n-))&(full);
// printf("nw=%d\n",nw);
// printf("nw2=");
// print(nw);
// printf(" %d\n",i);
if (vis[nw]==)
{
vis[nw]=;
tmp2.status=nw;
tmp2.step=tmp.step+;
if (tmp2.status==end)
{
printf("%d\n",tmp2.step);
flag=;
break;
}
q.push(tmp2);
// printf("%d %d\n",tmp2.status,tmp2.step);
}
}
if (!flag) break;
}
if (flag==)
printf("-1\n");
}
return ;
}
zoj3675 BFS+状态压缩的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
随机推荐
- mysql连接错误解决(ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol ref used (client option 'secure_auth' enabled))
当使用mysql的新版本是,连接老版本的mysql,就会有可能报: ERROR 2049 (HY000): Connection using old (pre-4.1.1) authenticatio ...
- Django-REST-Framework JWT 实现SSO认证(下)
在上一篇博客中,我已经对JSON Web 认证做了简单的解释,这篇博客是续篇,若不了解,请看上一篇博客:https://www.cnblogs.com/yushenglin/p/10863184.ht ...
- Android写入到mysql里的中文总是乱码?
中文编码的问题总是让人头疼,之前在python爬虫就折腾得死去活来,现在写app又是这样. 总结下来,就是三点吧: 数据库: 确定字符编码是utf8, collate: utf8_general_ci ...
- i2c中应答信号信号总结
i2c如果用到主从的关系的时候,需要考虑: give_ack();//从器件发送,来表示占用总线,让sda总线保持低电平. get_ack();//主器件判断是否有器件占用总线,sda有器件占用,是低 ...
- 吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝,自己写的就当是研究咯---https://www.cnblogs.com/NieXiaoHui/p/7150928.html
吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个ja ...
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- Android layer-list:联合shape(2)
Android layer-list:联合shape(2) 附录文章3简单说明了Android layer-list的用法,现在把Android layer-list联合shape做出一些特殊的 ...
- 文化之旅 2012年NOIP全国联赛普及组
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超 ...
- 用API中的raf复制文件图片等及系统找不到指定的文件的解决办法
该运行是在eclipse中进行的操作,小白的基础理解,如有不妥之处,请大佬们指正.QQ:1055802635 package raf; import java.io.IOException;impor ...
- Junit4使用总结
常用注解 @Before:初始化方法,在任何一个测试方法执行之前,必须执行的代码. @After:释放资源,在任何一个测试方法执行之后,需要进行的收尾工作. @Test:测试方法,表明这是一个测试 ...