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. ...
随机推荐
- Android TextView 阴影效果(投影)
Android TextView 阴影效果(投影) 四个参数: 1 2 3 4 android:shadowColor="@color/white" android:shadowD ...
- 用kermit通过串口往nandflash任意地址里烧写任何文件!
1.安装kermit #apt-get install ckermit 2.使用kermit之前,在用户宿主目录下(/home/用户名/)创建一个名为.kermrc的配置文件,内容如下 : set l ...
- Excel设置下拉菜单并隐藏下拉菜单来源单元格内容
一.问题来源 做实验室的进展统计表,老师让加上开始时间和完成时间,时间格式:周几_上午(下午.晚上). 这样就可以了做下拉菜单,方便填写,而且格式统一,方便查看. 二.解决办法 2.1 下来菜单 红框 ...
- linux之ssh无密码访问
1. windows下用putty执行ssh连接vmware中的linux虚拟机 linux虚拟机的网络选择bridge模式,ifconfig 看到ip后与windows local machine之 ...
- SPCOMM控件对串口参数的设置
对于串口来说,一般大家都了解波特率,校验码,数据位之类的参数.然而在实际的数据传输中,有些参数也会影响数据的传输.现总结如下,以便大家查询.在对串口进行编程时,可用portman对串口参数进行跟踪,提 ...
- JavaScript入门:006—JS函数的定义
JS函数的声明. 声明函数的格式例如以下: function 函数名(參数列表){ //函数语句: return 返回值; } 来看详细的函数声明.1.普通函数 <script type=&qu ...
- JAVA之接口与实现
/** * * 功能:接口与实现 * 接口也体现了多态性 */package com.test; public class test5 { /** * @param args */ ...
- C#拦截系统消息的方法-Application.AddMessageFilter
C#拦截系统消息的方法Application.AddMessageFilter Application.AddMessageFilter这个方法可以接收系统发出的消息: 首先定义一个类,继承IMess ...
- Android源码解析系列
转载请标明出处:一片枫叶的专栏 知乎上看了一篇非常不错的博文:有没有必要阅读Android源码 看完之后痛定思过,平时所学往往是知其然然不知其所以然,所以为了更好的深入Android体系,决定学习an ...
- leetcode题解:Search for a Range (已排序数组范围查找)
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...