#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+状态压缩的更多相关文章

  1. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  2. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

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

  5. BFS+状态压缩 HDU1429

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

  6. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  7. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  8. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

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

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

随机推荐

  1. mac下Redis安装和使用

    前言 本篇文章主要讲述了Mac下Redis的安装和使用的经验,并将python如何操作Redis做了简单介绍. 1. redis 安装 和启动 1.1 用brew安装 查看系统是否已经安装了Redis ...

  2. [bzoj4300][绝世好题] (动规)

    Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...

  3. #if 0的意义和好处

    在调试中经常遇到,写好的程序,需要调试相反的两方面,如:有两款单片机的程序,分别对应着不同的硬件引脚,我们把代码都写上了,但是不能同时让他们起效,通常的办法是/**/屏蔽一些段落,但是调试起来很麻烦. ...

  4. CSS中具有继承性的属性:

    CSS中具有继承性的属性:   color:#eee font: font-style font-variant: font-weight:bold font-size font-family: fo ...

  5. vb 运行ppt示例代码

    来源:http://support.microsoft.com/kb/222929 通过使用 PowerPoint 中的自动运行功能,您可以以编程方式打印.显示幻灯片及执行以交互式执行的大多数事情.按 ...

  6. Self Centos + Windows server 2016

    Set up by Derek: 2019-1-25 登陆个人物理机: license  60天Free , 如果过期,就在 VMware ESXI 6.5.0的黑屏界面去reset. https:/ ...

  7. 深刻理解Python中的元类(metaclass)--代码实践

    根据http://blog.jobbole.com/21351/所作的代码实践. 这篇讲得不错,但以我现在的水平,用到的机会是很少的啦... #coding=utf-8 class ObjectCre ...

  8. JAVA虚拟机运行时内存划分--运行时数据区域

    Java虚拟机在执行java程序时会把内存划分为以下几个不同的数据区域: java虚拟机内存划分(运行时)1.线程私有的: 程序计数器(Program Counter Register):可以看作当前 ...

  9. CentosOS 7: 创建Nginx+Https网站

    参考文章: 1. https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 2. http://songchenwen.com/tech/ ...

  10. 【APIO2012】【BZOJ2809】派遣dispatching

    2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1932 Solved: 967 [Submit ...