UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble
Solitaire
Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as possible
from the board. Pebbles disappear from the board as a result of a move. A move is possible if there is a straight line of three adjacent cavities, let us call them A, B, and C,
with B in the middle, where A is vacant, but B and C each contain a pebble. The move
constitutes of moving the pebble from C to A, and removing the pebble in B from the board. You may continue to make moves until no more moves
are possible.
In this problem, we look at a simple variant of this game, namely a board with twelve cavities located along a line. In the beginning of each game, some of the cavities are occupied by pebbles. Your mission is to find a sequence
of moves such that as few pebbles as possible are left on the board.
Input
The input begins with a positive integer n on a line of its own. Thereafter n different games follow. Each game consists of one line of input
with exactly twelve characters, describing the twelve cavities of the board in order. Each character is either '-' or'o' (The fifteenth character of English alphabet in lowercase). A '-' (minus)
character denotes an empty cavity, whereas a 'o'character denotes a cavity with a pebble in it. As you will find in the sample that there may be inputs where no moves is possible.
Output
For each of the n games in the input, output the minimum number of pebbles left on the board possible to obtain as a result of moves, on a row of its own.
Sample Input Output for Sample Input
5 ---oo------- -o--o-oo---- -o----ooo--- oooooooooooo oooooooooo-o |
1 2 3 12 1
|
题意 给你一个长度为12的字符串 由字符'-'和字符'o'组成 当中"-oo"和"oo-"分别能够通过一次转换变为"o--"和"--o" 能够发现每次转换o都少了一个 仅仅需求出给你的字符串做多能转换多少次即可了。
令d[s]表示字符串s最多能够转换的次数 若s能够通过一次转换变为字符串t 有d[s]=max(d[s],d[t]+1);
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, int> d;
int n, ans;
string t, S; int dp (string s)
{
if (d[s] > 0) return d[s];
d[s] = 1;
for (int i = 0; i < 10; ++i)
{
if (s[i] == 'o' && s[i + 1] == 'o' && s[i + 2] == '-')
{
t = s;
t[i] = t[i + 1] = '-';
t[i + 2] = 'o';
d[s] = max (d[s], dp (t) + 1);
}
if (s[i] == '-' && s[i + 1] == 'o' && s[i + 2] == 'o')
{
t = s;
t[i] = 'o';
t[i + 1] = t[i + 2] = '-';
d[s] = max (d[s], dp (t) + 1);
}
}
return d[s];
} int main()
{
cin >> n;
while (n--)
{
ans = 1;
cin >> S;
for (int i = 0; i < 12; ++i)
if (S[i] == 'o') ans++;
ans -= dp (S);
cout << ans << endl;
}
return 0;
}
UVa 10651 Pebble Solitaire(DP 记忆化搜索)的更多相关文章
- UVa 10599【lis dp,记忆化搜索】
UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...
- UVa 1252 (状压DP + 记忆化搜索) Twenty Questions
题意: 有n个长为m的各不相同的二进制数(允许存在前导0),别人已经事先想好n个数中的一个数W,你要猜出这个数. 每次只可以询问该数的第K为是否为1. 问采用最优询问策略,则最少需要询问多少次能保证猜 ...
- UVa 10817 (状压DP + 记忆化搜索) Headmaster's Headache
题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两 ...
- uva 10123 - No Tipping dp 记忆化搜索
这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /********************** ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索
题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...
- 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索
[题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...
- [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树
树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...
- poj1664 dp记忆化搜索
http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...
随机推荐
- 手把手教你使用FineUI+动软代码生成器开发一个b/s结构的取送货管理信息系统(附源码)之开篇
一 本系列随笔概览及产生的背景 近阶段接到一些b/s类型的软件项目,但是团队成员之前大部分没有这方面的开发经验,于是自己选择了一套目前网上比较容易上手的开发框架(FineUI),计划录制一套视频讲座, ...
- JAVA实现通用日志记录
原文:http://blog.csdn.net/jinzhencs/article/details/51882751 前言: 之前想在filter层直接过滤httpServerletRequest请求 ...
- php设计模式之建造者模式
建造者模式 建造者设计模式的目的是消除其他对象的复杂创建过程.使用建造者设计模式不仅是最佳的做法,而且在摸个对象的构造和配置方法改变时候,可以尽可能的减少重复更改代码. <?php /** *p ...
- 关于并发,异步,非阻塞(python)疑惑的一些资料解答
从iterable/iterator到generator到coroutine理解python的迭代器: http://python.jobbole.com/81916/理解python的生成器: ht ...
- Jquery.data()的值存放再什么地方的问题?
Where is jQuery.data() stored? Where does jQuery store the values of the data() that it sets to DOM ...
- Elasticsearch教程(七) elasticsearch Insert 插入数据(Java)
首先我不赞成再采用一些中间件(jar包)来解决和 Elasticsearch 之间的交互,比如 Spring-data-elasticsearch.jar 系列一样,用就得依赖它.而 Elastic ...
- Angular 学习笔记——filter
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- 设置快速的debian源的方法:
1)设置临时源 vi /etc/apt/sources.list #添加以下一行到文件最后 deb http://http.us.debian.org/debian stable main 2)更新软 ...
- HTTP常用的请求头和响应头
1.请求头 Connection:表示是否需要持久连接.若值为Keep-Alive,就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间.要实现这一点, ...
- java 字符深入知识,待整理
'编',"编", 为什么获取到的字节数组长度不一样 http://www.cnblogs.com/yongdaimi/p/5899328.html Unicode 官网 http ...