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相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- .Net实战之反射相关类之间的人体经络关系
--1.类的部分组成成员 --2.巧记成员之间的关系 [MyTable("T_UserInfo")] public class UserInfo : Person, UserSer ...
- npm install的时候报错 npm err code 1
在学习vue的时候,npm install的时候报错 npm err code 1,当时很郁闷,是‘vue init webpack my-project’命令新建的模版项目 ,怎么会报错,第一次遇 ...
- MFC_1.3 控件子类化 消息反射
控件子类化 如果想要在默认的控件类中添加一些功能,就需要子类化一个控件类 在类内可以响应控件所有的消息,并且可以添加自己的函数和数据 通过类向导子类化控件的步骤 打开类向导,创建一个 MFC 类,不要 ...
- Springboot + SLF4j + Log4j2 打印异常日志时,耗时要5-6秒
1.使用jps -l 查看springboot项目的进程ID 2.使用命令jstack -l 进程ID > log.txt 打印堆栈信息到文件,内容如下: "http-nio-8065 ...
- windows如何统计端口的连接数
习惯了linux的系统管理员,对linux的命令行工具总是印象极深,几乎所有的管理都可以在命令行下完成.命令行工具是linux系统管理的主流. 而使用windows是,因为图形化的界面,大家习惯了图形 ...
- JavaScipt30(第一个案例)(主要知识点:键盘事件以及transitionend)
今天得到一个github练习项目,是30个原生js写成的小例子,麻雀虽小五脏俱全,现在记录一下第一个. 第一个是键盘按键时页面上对应的键高亮,同时播放音频,松开后不再高亮. 我自己实现了一下,然后查看 ...
- ThinkPHP---thinkphp实用项
[一]代码调试 (1)跟踪信息 ①简介:用于展示系统执行的相关状况,类似于快递的物流信息.ThinkPHP中默认关闭.如需使用,则通过配置项SHOW_PAGE_TRACE(显示页面跟踪)来配置. ②位 ...
- nginx+keepalived+tomcat+memcache实现双VIP高可用及Session会话保持
Nginx+Keepalived+Tomcat+Memcached 实现双VIP负载均衡及Session会话保持 IP 信息列表: 名称 IP ...
- JAVA基础——异常--解析
简介 异常处理是java语言的重要特性之一,<Three Rules for effective Exception Handling>一文中是这么解释的:它主要帮助我们在debug的 ...
- Android中ProgressDialog自动消失
主要函数部分代码: final ProgressDialog proDialog = android.app.ProgressDialog .show(MainActivity.this, " ...