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. c# Queue实现生产者(Producer)消费者(Consumer)模式

    我们在开发过程中经常会遇到需要从一个地方不断获取数据然后又需要交给另一个线程对数据进行二次加工的情况,这种场景适合使用生产者-消费者模式. Demo展示 //中间的容器 public static c ...

  2. 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)

    我们知道,MFC程序如果检测到存在内存泄漏,退出程序的时候会在调试窗口提醒内存泄漏.例如: class CMyApp : public CWinApp{public:BOOL InitApplicat ...

  3. LDA主题模型(理解篇)

    何谓“主题”呢?望文生义就知道是什么意思了,就是诸如一篇文章.一段话.一个句子所表达的中心思想.不过从统计模型的角度来说, 我们是用一个特定的词频分布来刻画主题的,并认为一篇文章.一段话.一个句子是从 ...

  4. TCP端口状态LISTENING ESTABLISHED CLOSE_WAIT TIME_WAIT SYN_SENT

    TCP状态转移要点 TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死状态,连接本身占用的资源不 会被释放.网络服务器程序要同时管理 ...

  5. CSS:div/img水平垂直居中

    div水平垂直居中方法一: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  6. mac系统,鼠标移动太慢

    to check your speed: defaults read -g com.apple.mouse.scaling to set your speed defaults write -g co ...

  7. 梦想CAD控件系统变量说明

    这里介绍一些常用系统变量有String.double.long.McGePoint3d等类型,其中有部分系统变量是随图纸保存,再次打开时就会读取图纸中的系统变量,有些系统变量不随图纸保存,其作用来控制 ...

  8. [Linux]正则表达式和grep使用【转载】

    [Linux]正则表达式和grep使用 2018年12月05日 23:45:54 祥知道 阅读数 78 标签: 正则表达式grepLinuxegrep 更多 个人分类: Linux 所属专栏:  Li ...

  9. 'dict' object is not callable

    今天学py的map函数时,由于在上面定义了一个dict类型的变量(取的名是map),所以编译后报了这么一个错,哎,以后学py命名要小心了

  10. 16监听器、Filter、Nginx、Spring、AOP

    16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...