Trim the Nails


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is potholed.

The nail clipper's edge is N millimeters wide. And we use N characters('.' or '*') to represent the potholed nail clipper. '.' represents 1 bad millimeter edge,
and '*' represents 1 good millimeter edge.(eg. "*****" is a 5 millimeters nail clipper with the whole edge good. "***..." is a 6 millimeters nail clipper with half of its edge good and half of its edge bad.)

Notice Robert can turn over the clipper. Turning over a "**...*"-nail clipper will make a "*...**"-nail clipper.

One-millimeter good edge will cut down Robert's one-millimeter fingernail. But bad one will not. It will keep the one-millimeter unclipped.

Robert's fingernail is M millimeters wide. How many times at least should Robert cut his fingernail?

Input

There will be multiple test cases(about 15). Please process to the end of input.

First line contains one integer N.(1≤N≤10)

Second line contains N characters only consists of '.' and '*'.

Third line contains one integer M.(1≤M≤20)

Output

One line for each case containing only one integer which is the least number of cuts. If Robert cannot clipper his fingernail then output -1.

Sample Input

8
****..**
4
6
*..***
7

Sample Output

1
2

Hint

We use '-' to present the fingernail.
For sample 1:
fingernail: ----
nail clipper: ****..**
Requires one cut. For sample 2:
fingernail: -------
nail clipper: *..***
nail clipper turned over: ***..*
Requires two cuts.


题意:  Robert须要剪指甲。可是他的指甲刀有缺陷,有些是剪不到的,

他的指甲刀形如是一个字符串。符号'.'代表指甲刀这处有缺陷这处的指甲不能修剪到,

符号'*'代表这处是完善的,这处的能够修剪到。如指甲刀**..**,要剪长度为6的指甲,

则剪出来的指甲(1代表该处指甲已修剪。0则没有)是110011,这须要再剪一次;

指甲刀能够左右移动,还能够翻转;

题解:一直从最左端有指甲的位置開始 剪,指甲钳分正反两种状态剪指甲,bfs求出最小步数。

-1的情况全是‘.’。

能够先处理出两把指甲钳,正反各一把,用一个数表示。指甲原始状态能够用(1<<L)-1表示,即所有

都是1,然后进行位运算,把剩下的状态求出来。知直到0.

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue> using namespace std; int n,L,ans;
char s[14];
int Jidong,_Jidong; struct node
{
int nail;
int step;
};
queue<node>que;
bool vis[(1<<20)+50]; void debug(int x)
{
while(x)
{
printf("%d",x&1 );
x>>=1;
}
printf("\n");
}
void bfs()
{
while(que.size())que.pop();
memset(vis,false,sizeof vis);
node it;
it.nail=L;
it.step=0;
que.push(it);
vis[L]=true;
while(que.size())
{
it=que.front();
if(it.nail==0)
{
ans=it.step;
return;
}
que.pop();
while((it.nail&1)==0)
{
it.nail>>=1;
}
int nit=it.nail,_nit=it.nail;
for(int i=0;i<=20;i++)
{
if((nit&(1<<i))&&(Jidong&(1<<i)))
nit^=(1<<i);
if((_nit&(1<<i))&&(_Jidong&(1<<i)))
_nit^=(1<<i);
}
node _it;
_it.nail=nit;
_it.step=it.step+1;
if(!vis[nit])
{
vis[nit]=true;
que.push(_it);
}
_it.nail=_nit;
if(!vis[_nit])
{
vis[_nit]=true;
que.push(_it);
}
}
} int main()
{
//freopen("test.in","r",stdin);
while(cin>>n)
{
scanf("%s",s);
cin>>L;
Jidong=0,_Jidong=0;
int length=strlen(s);
for(int i=0,j=length-1;i<length;i++,j--)
{
if(s[i]=='*')
{
Jidong|=(1<<i);
_Jidong|=(1<<j);
}
}
//debug(Jidong);
if(!Jidong)
{
printf("-1\n");
continue;
}
while((Jidong&1)==0)Jidong>>=1;
while((_Jidong&1)==0)_Jidong>>=1;
L=(1<<L)-1;
ans=-1;
bfs();
printf("%d\n",ans );
}
return 0;
}

ZOJ 3675 Trim the Nails(bfs)的更多相关文章

  1. ZOJ 3675 Trim the Nails

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 DP+状态压缩. http://www.cnblogs.com/dgsr ...

  2. ZOJ 2411 Link Link Look(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1411 题目大意:连连看,给出每次连线的两个坐标,求能消去多少方块,拐 ...

  3. ZOJ 1093 Monkey and Banana (LIS)解题报告

    ZOJ  1093   Monkey and Banana  (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  4. ZOJ Problem Set - 3829Known Notation(贪心)

    ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. Windows下80端口被进程System&PID=4占用的解决方法

    我的占用原因是 SQL Server Reporting Services,停止掉这个服务并设置其为手动启动即可 如果你并没有安装 SQL Server,请参考下文解决 =============== ...

  2. Sonar 规则

    bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. ...

  3. Angular——MVC模式开发实战

    创建项目 创建工作目录 使用bower下载需要插件 git init.add.commit之后得到分支master,再创建developer分支,然后再此分支上进行具体功能开发 MVC架构 之前小项目 ...

  4. android studio使用中碰到Failure [INSTALL_FAILED_OLDER_SDK] 问题

    第一次使用Android studio开发.直接新建一个默认项目运行出现:Failure [INSTALL_FAILED_OLDER_SDK] , 网上很多人说修改build.gradle中的mins ...

  5. 梦想CAD控件关于标注的系统变量说明

    主要用到函数说明: IMxDrawDimension::SetDimVarDouble 设置标注属性的实数类型变量值,详细说明如下: 参数 说明 [in] LONG iType 该属性的类形值 dVa ...

  6. 利用freemarker+SAX解析xml的方式对excel文件字段校验

    利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...

  7. pycharm中将ui文件转换成py文件

    方法一:直接使用命令行 python -m PyQt5.uic.pyuic xx.ui -o xx.py 方法二:直接使用命令 先进到C:\python\pkgs\pyqt-5.9.2-py37h65 ...

  8. extjs动态插入一列

    StdDayWordQuery:function(btn,event){ var form=Ext.getCmp('queryFormSDW'); var userNameORuserCode = f ...

  9. 第四章 模块化React和Redux应用

    第四章 模块化React和Redux应用 4.1 模块化应用要点 构建一个应用的基础: 代码文件的组织结构: 确定模块的边界: Store的状态树设计. 4.2 代码文件的组织方式 4.2.1 按角色 ...

  10. 山建校赛B题公式证明

    原题 证明