ZOJ 3675 Trim the Nails(bfs)
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)的更多相关文章
- ZOJ 3675 Trim the Nails
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 DP+状态压缩. http://www.cnblogs.com/dgsr ...
- ZOJ 2411 Link Link Look(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1411 题目大意:连连看,给出每次连线的两个坐标,求能消去多少方块,拐 ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- 13 继续C#中的方法,带返回值的方法介绍
在这一个练习中,我们要使用带返回值的方法.如果一个方法带返回值,那么它的形式是这样的. 定义一个带返回值的C#方法 static 返回类型 方法名字 (参数类型 参数1的名字,参数类型 参数2的名字) ...
- 利用freemarker导出页面格式复杂的excel
刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...
- dede手机访问网站跳转到手机端模板
如何手机访问的时候跳转到自己的手机端模板,这时候需要一个js跳转代码:当手机访问的时候直接跳转到手机端 那手机端前提要有手机端的模板 <script> if(navigator.platf ...
- 【PostgreSQL-9.6.3】LOG: unrecognized configuration parameter "dynamic_shared_memory_type"
报错如下: 输入如下命令启动PG数据库时,报错: [postgres@drz ~]$ pg_ctl -D /opt/postgresql/data/ start server starting FAT ...
- nodejs——避免判断创建多级目录
基本概念 fs.exists已经弃用,可以使用fs.access判断文件夹是否存在,但是官方的建议是在进行文件操作前不要使用fs.access,官方推荐的方式的是直接进行文件操作,有错误再修改 不建议 ...
- jQuery.fn.extend和jQuery.extend
<script src="http://www.cssrain.cn/demo/JQuery+API/jquery-1[1].2.1.pack.js" type=" ...
- Lazarus 字符集转换 Utf8ToAnsi,UTF8ToWinCP,UTF8ToSys,UTF8ToConsole
由于Lazarus从1.2版开始默认字符集就是UTF8,如果要转到系统正常显示或文本保存,就必须对字符集进行转换.Lazarus提供了很多函数.如题. 那么这里面有什么关系呢? UTF8ToSys 需 ...
- HDU_1505_矩阵中的最大矩形_dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1505 City Game Time Limit: 2000/1000 MS (Java/Others) ...
- js异步请求
目前async / await特性并没有被添加到ES2016标准中,但不代表这些特性将来不会被加入到Javascript中.在我写这篇文章时,它已经到达第三版草案,并且正迅速的发展中.这些特性已经被I ...
- 10Java Server Pages 隐式对象
Java Server Pages 隐式对象 JSP隐式对象是Web容器加载的一组类的实例,它不像一般的Java对象那样用“new”去获取实例,而是可以直接在JSP页面使用的对象.JSP提供的隐式对象 ...