#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. CCF201509-2 日期计算 java(100分)

    试题编号: 201509-2 试题名称: 日期计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有2 ...

  2. hash扩展长度攻击及hashdump使用

    摘自: 1.http://www.freebuf.com/articles/web/69264.html 2.https://www.cnblogs.com/pcat/p/5478509.html 0 ...

  3. Uva10305 Ordering Tasks

    John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...

  4. hdu 1811拓扑排序+并查集(容器实现)

    http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729566.html #include<stdio.h> #includ ...

  5. 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验

    出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...

  6. 转载 - C - 枚举类型详解

    出处:http://www.cnblogs.com/JCSU/articles/1299051.html 注:以下全部代码的执行环境为VC++ 6.0 在程序中,可能需要为某些整数定义一个别名,我们可 ...

  7. vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 总结for循环及for循环增强遍历数组,list,set和map

    一.对于集合 (1)普通for循环 int[] arr = { 2, 1, 2 }; for(int i=0;i<arr.length;i++){ System.out.println(arr[ ...

  9. tyvj1271 零式求和

    描述 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N.现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白(例如1-2 3就等于1- ...

  10. - > 动规讲解基础讲解四——最大子段和问题

    给出一个整数数组a(正负数都有),如何找出一个连续子数组(可以一个都不取,那么结果为0),使得其中的和最大?   例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13.和为20. ...