UVa 10651 Pebble Solitaire (DP 卵石游戏 记忆化搜索)



题意  给你一个长度为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;
}

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 AB, 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

时间: 2024-10-24 16:24:15

UVa 10651 Pebble Solitaire (DP 卵石游戏 记忆化搜索)的相关文章

uva 10651 Pebble Solitaire (BFS)

uva 10651 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

uva 11927 - Games Are Important(组合游戏+记忆化)

题目链接:uva 11927 - Games Are Important 题目大意:给出一张无环有向图,并给出每个节点上的石子数,每次操作可以选择一个石子,向下一个节点移动.两人轮流操作,直到不能操作为失败者. 解题思路:有了图之后,用记忆化的方式处理出每个节点的SG值,取所有石子数为奇数的节点的Nim和. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后按照每份的个数排定字典序,并且划分时要求ai?1≤ai,然后输出字典序排在k位的划分方法. 解题思路:因为有ai?1≤ai的条件,所以先记忆化搜索处理出组合情况dp[i][j][s]表示第i位为j,并且剩余的未划分数为s的总数为dp[i][j][s],然后就是枚举每一位上的值,判断序列的位置即可. #include <cstdio> #include

hdu4753 状态压缩dp博弈(记忆化搜索写法)

http://acm.hdu.edu.cn/showproblem.php?pid=4753 Problem Description There is a 3 by 3 grid and each vertex is assigned a number. It looks like JiuGongGe, but they are different, for we are not going to fill the cell but the edge. For instance, adding

NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索

矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中.你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内. 输入 第一行是一个正正数N(0<N<10),表示测试数据组数,每组测

UVa 10651 - Pebble Solitaire

题目:有一个类似跳棋的游戏,一共有12个位置'o'代表棋子'-'代表空位, 'oo-'可以转化成'--o','-oo'可以转化成'o--',给你初始状态,问最后,至少剩下几个棋子. 分析:dp,记忆化搜索,位运算.利用搜索在相邻状态间dp即可. 每个状态的最优解为他能转化所有状态中的最优解. 因为,一共有2^12 = 4096个状态,每次找到解即存储,不会重复计算,所以时间方面没问题. 利用一个12位整数表示一个状态,'o'用1表示,'-'用0表示,则'---oo-------'为24(2进制0

ACM学习历程—ZOJ3471 Most Powerful(dp &amp;&amp; 状态压缩 &amp;&amp; 记忆化搜索 &amp;&amp; 位运算)

Description Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way

poj 1609 Tiling Up Blocks dp入门之记忆化搜索

题意: 给n个二元组(a,b),要在其中找最长的序列,使得对序列中的任意i<j,有ai<=aj且bi<=bj. 分析: 设dp[a][b]代表以(a,b)结尾的最长序列长度,记忆化搜索即可. 代码: //poj 1609 //sep9 #include <iostream> using namespace std; const int max_p=128; int n; int num[max_p][max_p]; int dp[max_p][max_p]; int sear

洛谷P1057 传球游戏(记忆化搜索)

点我进入题目 题目大意:n个小孩围一圈传球,每个人可以给左边的人或右边的人传球,1号小孩开始,一共传m次,请问有多少种可能的路径使球回到1号小孩. 输入输出:输入n,m,输出路径的数量. 数据范围:40% 3<=n<=30 1<=m<=20 100% 3<=n<=30 1<=m<=30 我是这么想的:膜拟过程,从1号小孩开始dfs,然后加一个记忆化搜索节省时间. dfs(num,tim)表示球传到第num个小孩,已经传过tim次时候,d[num][tim]表