#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. Vue.Draggable实现拖拽效果(快速使用)

    1.下载包:npm install vuedraggable 配置:package.json "dependencies": { "element-ui": & ...

  2. PAT 1127 ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  3. Poor Hanamichi

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)

    传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...

  5. noip模拟赛 序

    [问题背景]zhx 给他的妹子们排序.[问题描述]zhx 有 N 个妹子, 他对第 i 个妹子的好感度为 ai,且所有 ai两两不相等. 现 在 N 个妹子随意站成一排, 他要将她们根据好感度从小到大 ...

  6. 临时起异,要进入C++领域耍一个程序

    没办法.两周之内可以搞定吧. 就一个SESSION 0的问题. 网上有类似源码,调一下应该就可以吧..保佑顺利. 基本语法都还记得,快N年啦... #include <iostream> ...

  7. 武大OJ 574. K-th smallest

    Description Give you a number S of length n,you can choose a position and remove the number on it.Af ...

  8. BZOJ(3) 1051: [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7365  Solved: 3937[Submit][Sta ...

  9. js部分基础

    1.js的基本类型有哪些?引用类型有哪些?null和undefined的区别. 基础类型:number,null,regex,string,boolean 引用类型 : object,function ...

  10. Handling PnP Paging Request

    The following explains why the DO_POWER_PAGABLE bit must be set on the way down and not on the way u ...