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. ...
随机推荐
- iOS_字典数组 按key分组和排序
int main(int argc, const charchar * argv[]) { @autoreleasepool { // 1.定义一个测试的字典数组 NSMutableArray *di ...
- Chrome下强制http重定向到https的问题
问题: Chrome会强制将http重定向到https,就算是在浏览器手动输入http://xxx也不可以. 解决方案: 1. 在chrome的地址栏输入chrome://net-internals/ ...
- Uri详解之——Uri结构与代码提取
目录(?)[+] 前言:依然没有前言…… 相关博客:1.<Uri详解之——Uri结构与代码提取>2.<Uri详解之二——通过自定义Uri外部启动APP与Notification启动& ...
- DatagramPacket,DatagramSocket
package test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSo ...
- Java基础学习过程
转载:http://blog.csdn.net/scythe666/article/details/51699954JVM 1. 内存模型( 内存分为几部分? 堆溢出.栈溢出原因及实例?线上如何排查? ...
- Fragment简单用法
一.示意图 二.新建一个左侧碎片布局left_fragment.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...
- uva 12627 - Erratic Expansion(递归求解)
递归的边界条件写的多了--不是必需写呢么多的.. 不明确可共同探讨~ #include<cstdio> #include<iostream> #include<cmath ...
- javascript---》arguments对象
使用arguments可以直接访问函数传入的实参 如:arguments[0]访问第一个参数,arguments[1]访问第二个参数 arguments.length检测函数的参数个数 如: func ...
- <转>Windows平台下Makefile学习笔记(二)
本文转自:http://blog.csdn.net/clever101/article/details/8286066 上次我们学习了怎么用Makefile编译一个控制台工程.这次我们学习一下如何使用 ...
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...