ZOJ 3675 Trim the Nails(bfs+状态压缩搜索)
来源:互联网 发布:linux 修改dns配置 编辑:程序博客网 时间:2024/05/18 19:38
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****..**46*..***7
Sample Output
12
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.题目大意:罗伯特的指甲长为m,他有一把刀,刀的长度为n(有'*'和'.'构成),'*'表示刀在该位置处是好的,能削减m的一个单位长度,'.'表示刀在该处是坏的,不能削减任何指甲。给定n和刀,还有m,求最少他需要多少次修剪才能将指甲削减好(长度为m的指甲全部被削减完)。如果,不能削减完,则输出-1.思路: 状态压缩搜索+bfs.前者是用来枚举状态空间,后者是用来求最短的修剪次数。状态压缩:用二进制位表示每一位被修剪的状态,1表示该位已经被修剪,0表示没有被修建。那么最终的状态就是m个1所表示的二进制数,即(1<<m)-1;/* @author : liuwen*/#include <iostream>#include <cstdio>#include <cstring>#include <climits> //INT_MAX INT_MIN LONG_LONG_MAX LONG_LONG_MIN#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>#include <algorithm>using namespace std;const int maxn=(1<<21)-1;struct Node{ int status; int step; Node(int status=0,int step=0):status(status),step(step){};}st,ed;int vis[maxn],minStep,n,m;int up,down;int toInt(string& s){ int sum=0; string::size_type i=0; for(i=0;i<s.size();i++){ if(s[i]!='0') break; } for(;i<s.size();i++){ sum=(sum<<1)|(s[i]=='1'); } return sum;}int bfs(Node st,Node ed){ int i,j,state,nowStatus; memset(vis,0,sizeof(vis)); queue<Node>que; que.push(st); vis[st.status]=1; while(!que.empty()){ Node tmp=que.front(); que.pop(); if(tmp.status==ed.status){ minStep=tmp.step; return minStep; } for(j=0,state=up;j<2;j++,state=down){ for(i=0;i<=n;i++){ int tmpStatus=(state>>i); //枚举向右移i位,即 tmpStatus=(tmpStatus|tmp.status);//在父节点的指甲状态上的修建后 nowStatus=tmpStatus&((1<<m)-1); //子节点修建后的指甲状态 if(!vis[nowStatus]){ vis[nowStatus]=1; que.push(Node(nowStatus,tmp.step+1)); } } } } return -1;}int main(){ //freopen("in.txt","r",stdin); string str; while(cin>>n>>str>>m){ for(string::size_type i=0;i<str.size();i++){ if(str[i]=='*') str[i]='1'; else str[i]='0'; } up=toInt(str); reverse(str.begin(),str.end()); down=toInt(str); up=up<<m; down=down<<m; n=n+m; st=Node(0,0); ed=Node((1<<m)-1,0); cout<<bfs(st,ed)<<endl; } return 0;}
- ZOJ 3675 Trim the Nails(bfs+状态压缩搜索)
- ZOJ 3675 Trim the Nails (状态压缩+BFS)
- ZOJ 3675 Trim the Nails(状态压缩 + BFS)
- zoj 3675 Trim the Nails (BFS+状态压缩)
- ZOJ 3675 Trim the Nails(bfs)
- ZOJ 3675 Trim the Nails
- ZOJ 3675 Trim the Nails
- ZOJ 3675 Trim the Nails(bfs+位运算处理)
- ZOJ 3675 Trim the Nails 小水题
- ZOJ 3675(Trim the Nails)
- ZOJ 3675 Trim the Nails(DFS)
- Trim the Nails (DFS+状态压缩) #by Plato
- ZOJ3675:Trim the Nails
- ZOJ 1301 The New Villa (BFS + 状态压缩)
- ZOJ 3611 BFS+状态压缩
- ZOJ 3814:模拟和状态压缩BFS
- [ZOJ 3814 Sawtooth Puzzle] bfs+状态压缩
- ZOJ-2050-Flip Game【状态压缩bfs】
- 黑马程序员—访问修饰符与ArrayList集合
- 基于OpenCV读取摄像头进行人脸检测和人脸识别
- Summary of this week, Dec.21,2013
- 使用动画实现组件抖动
- 编写你的第一个垃圾收集器
- ZOJ 3675 Trim the Nails(bfs+状态压缩搜索)
- STM32 SysTick定时器
- 【SVN命令】之 revert
- JUnit 4 单元测试利器
- mysql添加账号的数据库管理权限
- 黑马程序员—装箱拆箱与IO流重点
- 五种模式的表级锁
- Flash Builder编译的swf为什么在bin-debug下运行正常,复制到其他文件夹就不正常
- 先序遍历和中序遍历求后序遍历