#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. 九九乘法表-Java

    public class Test1 { public static void main(String[] args){ for(int i=1;i<=9;i++){ for(int j=1;j ...

  2. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)

    题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...

  3. linux 简单实用小操作

    mysql改密码 通过root以后,(root密码忘记就没法了) alter user username@'%' identified by 'password' 端口被占用 sudo fuser - ...

  4. Linux - 模块编程初试

    计算机网络的课程设计要做防火墙,老师没有限制在什么系统上面做,所以决定在Linux上实现.找了一下相关的资料,发现其实Linux有提供Netfilter/Iptables,为用户提供防火墙的功能,稍微 ...

  5. kendo Cannot read property 'call' of undefined

    造成这个错误的原因是在 必须有id的定义

  6. Servlet监听器(Listener)实例

    以下内容是翻译自http://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example: 说明:web.xml的加 ...

  7. 3deye-demo-8-14-26-51

    源码

  8. 非常适合新手的jq/zepto源码分析03

    zepto.fragment = function(html, name, properties) { var dom, nodes, container // 如果是简单的标签<div> ...

  9. Oracle中的 row_number() over (partition by order by ) 用法

    oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_n ...

  10. Setting .xap MIME Type for Silverlight

    http://www.adefwebserver.com/dotnetnukehelp/misc/Silverlight/SettingMimeType.html Windows 2003: In I ...