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 ...
随机推荐
- php实现快速排序
下午练习时候,把经典排序快速排序做了,以下是我的代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-8-29 ...
- 股票投资组合-前进优化方法(Walk forward optimization)
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- jar包和war包的区别(转)
jar包和war包的区别:war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运 ...
- CentOS安装Erlang
1.首先要安装编译源码用的编译器gcc&g++,安装方式很简单,先用yum search gcc搜索出包,然后选择适合自己的版本复制全名,用yum intall gcc_XXX来进行安装即可. ...
- Windows 下Apace tomcat
java JDK安装: 1. 官方www.oracle.com 下载jdk 2. 环境变量配置 (1)新建->变量名:JAVA_HOME变量值:C:\Program Files (x86)\Ja ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q32-Q34)
Question 32You create a custom Web Part.You need to ensure that a custom property is visible in Edit ...
- 【读书笔记】iOS-GCD-Dispatch Queue
一,Dispatch Queue的实现: 1,用于管理追加的Block的C语言层实现的FIFO队列. 2,Atomic函数中实现的用于排他控制的轻量级信号. 3,用于管理线程的C语言层实现的一些容器. ...
- iOS 开发技巧-制作环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...
- iOS 使用SDwebImage缓存图片并在断网时候显示
[_loadImageView setShowActivityIndicatorView:YES]; [_loadImageView setIndicatorStyle:UIActivityI ...
- 关于tableView中tableHeaderView/tableFooterView/sectionHeader/sectionFooter/contentInset的理解
其实每个人的理解有所不同,找到最有利于自己的理解方式即可.有人把Cell,tableHeaderView,tableFooterView,sectionHeader,sectionFooter这些属性 ...