原题: 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的更多相关文章

  1. 2014 Super Training #8 G Grouping --Tarjan求强连通分量

    原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...

  2. 2014 Super Training #10 G Nostop --矩阵快速幂

    原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...

  3. 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+ ...

  4. 2014 Super Training #4 B Problem Arrangement --状压DP

    原题:ZOJ 3777  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...

  5. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  6. 2014 Super Training #7 C Diablo III --背包问题(DP)

    原题: ZOJ 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 一个带有一些限制的背包问题. 假设在没有限 ...

  7. 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS

    原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...

  8. 2014 Super Training #6 F Search in the Wiki --集合取交+暴力

    原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...

  9. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

随机推荐

  1. Linux Shell系列教程之(十七) Shell文件包含

    本文是Linux Shell系列教程的第(十七)篇,更多Linux Shell教程请看:Linux Shell系列教程 通过文件包含,可以引用其他文件的内容,也可以将复杂内容分开,使程序结构更加清晰. ...

  2. PHP simplexml_load_string 过滤<![CDATA[XXXX]]>

    首先说说过滤<![CDATA[XXXX]]>干嘛用的. 这东西主要是防止XML里面嵌套HTML标签导致XML样式错乱的. 过滤很简单: $response = str_replace( a ...

  3. 微信公共平台开发5 .net

    每次在于微信交互时,都要用到access_token,但是这个值限制的是有时间的,但是access_token,在以后的高级功能里面会经常用到,所以这里不得不这里对前面所讲解的access_token ...

  4. GridControl列自动匹配宽度

    //自动调整所有字段宽度this.gridView1.BestFitColumns(); //调整某列字段宽度this.gridView1.Columns[n].BestFit(); 大多是网上零散找 ...

  5. 使用SharedPreferences进行简单的储存

    博客地址 http://www.cnblogs.com/mmyblogs/p/6082512.html(转载请保留) SharedPreferences定义 1.是一种轻型的数据存储的方式 2.本质是 ...

  6. IOS UILabel 根据内容自适应高度

    iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelVie ...

  7. NSOperation创建队列

    #pragma mark - NSOperation //NSOperation不能直接进行多线程的创建,需要帮助:NSOperationQueue // 使用NSOperation的第一个子类去创建 ...

  8. CSS(一)

    开始学习css之旅:先照样式做一个 一.使用DIV设置 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  9. 走着官方的教程入门Material Design(一)

    又到期末了,学习下Google的材料设计.写下此文记录自己的同时,分享给需要的同学,若发现文中有什么问题和不对,欢迎指出 使用 Material Design 创建新应用 首先需要使用材料主题 如果是 ...

  10. Java集合 之 List 集合

    1.什么是Lsit集合? List集合是一种元素有序的,可重复的集合,集合中每个元素都有自己的元素索引.List集合允许可重复的元素,可以通过索引来访问指定位置的元素. 2.Java8改进的List接 ...