2014 Super Training #6 G Trim the Nails --状态压缩+BFS
原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675
由m<=20可知,可用一个二进制数表示指甲的状态,最多2^20,初始状态为0,表示指甲都没剪,然后BFS找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> using namespace std; #define N 10007 struct node { int state,step; node(int _state,int _step) { state = _state; step = _step; } node(){} }; int vis[1<<21]; int cut[2]; //两个方向 queue<node> que; int n,m; int bfs(int s) { int i,j,k; memset(vis,0,sizeof(vis)); while(!que.empty()) que.pop(); int E = (1<<m)-1; que.push(node(s,0)); vis[s] = 1; while(!que.empty()) { node tmp = que.front(); que.pop(); int state = tmp.state; int step = tmp.step; int tms = state; for(i=0;i<2;i++) //direction { for(j=0;j<n;j++) //move { int end = ((cut[i]>>j) | tms) & E; // &E : keep m bit if(vis[end]) continue; vis[end] = 1; if(end == E) return step+1; que.push(node(end,step+1)); } for(j=0;j<m;j++) { int to = ((cut[i]<<j) | tms) & E; if(vis[to]) continue; vis[to] = 1; if(to == E) return step+1; que.push(node(to,step+1)); } } } return -1; } int main() { int i,j; char ss[13]; while(scanf("%d",&n)!=EOF) { cut[0] = cut[1] = 0; scanf("%s",ss); for(i=0;i<=n;i++) { if(ss[i] == '*') { cut[0] |= (1<<i); cut[1] |= (1<<(n-1-i)); } } scanf("%d",&m); if(cut[0] == 0) { puts("-1"); continue; } printf("%d\n",bfs(0)); } return 0; }
作者:whatbeg
出处1:http://whatbeg.com/
出处2:http://www.cnblogs.com/whatbeg/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
更多精彩文章抢先看?详见我的独立博客: whatbeg.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!