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找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可。
代码:
#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[<<];
int cut[]; //两个方向
queue<node> que;
int n,m; int bfs(int s)
{
int i,j,k;
memset(vis,,sizeof(vis));
while(!que.empty())
que.pop();
int E = (<<m)-;
que.push(node(s,));
vis[s] = ;
while(!que.empty())
{
node tmp = que.front();
que.pop();
int state = tmp.state;
int step = tmp.step;
int tms = state;
for(i=;i<;i++) //direction
{
for(j=;j<n;j++) //move
{
int end = ((cut[i]>>j) | tms) & E; // &E : keep m bit
if(vis[end])
continue;
vis[end] = ;
if(end == E)
return step+;
que.push(node(end,step+));
}
for(j=;j<m;j++)
{
int to = ((cut[i]<<j) | tms) & E;
if(vis[to])
continue;
vis[to] = ;
if(to == E)
return step+;
que.push(node(to,step+));
}
}
}
return -;
} int main()
{
int i,j;
char ss[];
while(scanf("%d",&n)!=EOF)
{
cut[] = cut[] = ;
scanf("%s",ss);
for(i=;i<=n;i++)
{
if(ss[i] == '*')
{
cut[] |= (<<i);
cut[] |= (<<(n--i));
}
}
scanf("%d",&m);
if(cut[] == )
{
puts("-1");
continue;
}
printf("%d\n",bfs());
}
return ;
}
2014 Super Training #6 G Trim the Nails --状态压缩+BFS的更多相关文章
- 2014 Super Training #8 G Grouping --Tarjan求强连通分量
原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- 2014 Super Training #4 G What day is that day? --两种方法
原题: ZOJ 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785 题意:当天是星期六,问经过1^1+2^2+ ...
- 2014 Super Training #4 B Problem Arrangement --状压DP
原题:ZOJ 3777 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- 2014 Super Training #7 C Diablo III --背包问题(DP)
原题: ZOJ 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 一个带有一些限制的背包问题. 假设在没有限 ...
- 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS
原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...
- 2014 Super Training #6 F Search in the Wiki --集合取交+暴力
原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
随机推荐
- ASP.NET WebAPI 12 Action的执行
Action的激活大概可以分为如下两个步骤:Action对应方法的调用,执行结果的协商.在WebAPI中由HttpActionInvoker(System.Web.Http.Controllers)进 ...
- [翻译]:SQL死锁-为什么会出现死锁
下面这篇对理解死锁非常重要,首先死锁是如何产生的我们要清楚. We already know why blocking occurs in the system and howto detect an ...
- ORACLE 中ROWNUM用法总结!
ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...
- java多线程生产者消费者
//Java Thread producer customer class ThreadTest { public static void main(String[] args) { Q q=new ...
- Sublime Text3 快捷键汇总
Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...
- vh属性-- 一个永远垂直居中的弹出框
下面的demo,无论浏览器大小如何改变,滚动条是否滚动,弹出框永远水平垂直居中 <html> <head> <title></title> </h ...
- ABAP 表格控制(Table Control)和步循环
表格控制(Table Control)和步循环 1.两个标准Demo: SAPMTZ60,SAPMTZ61 2.简介 3.建立Table Control程序的基本流程 4.使用步循环 5.表格 ...
- .NET破解之百分百营销软件系列
今天在52中看到了一个邮件批量发送工具,感觉不怎么好用,百度一下,找到了百分百系统,虽然也不怎么好用,但还是忍不住P它. 官网:http://www.100qunfa.com/ 百分百不加群提取群成员 ...
- Button 对 TreeView1 所有节点的全选
protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < this.Tre ...
- 【读书笔记】iOS-引用计数
Cocoa采用了一种称为引用计数的技术,有时也叫做保留计数.每个对象有一个与之相关联的整数,称作它的引用计数器或保留计数器.当某段代码需要访问一个对象时,该代码将该对象的保留计数器值加1,表示“我要访 ...