Clickomania(区间DP)

Posted zdragon1104

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Clickomania(区间DP)相关的知识,希望对你有一定的参考价值。

描述

Clickomania is a puzzle in which one starts with a rectangular grid of cells of different colours. In each step, a player selects ("clicks") a cell. All connected cells of the same colour as the selected cell (including itself) are removed if the selected cell is connected to at least one other cell of the same colour. The resulting "hole" is filled in by adjacent cells based on some rule, and the object of the game is to remove all cells in the grid. In this problem, we are interested in the one-dimensional version of the problem. The starting point of the puzzle is a string of colours (each represented by an uppercase letter).
At any point, one may select (click) a letter provided that the same letter occurs before or after the one selected. The substring of the same letter containing the selected letter is removed, and the string is shortened to remove the hole created. To solve the puzzle, the player has to remove all letters and obtain the empty string. If the player obtains a non-empty string in which no letter can be selected, then the player loses. For example, if one starts with the string "ABBAABBAAB", selecting the first "B" gives "AAABBAAB". Next, selecting the last "A" gives "AAABBB". Selecting an "A" followed by a "B" gives the empty string. On the other hand, if one selects the third "B" first, the string "ABBAAAAB" is obtained. One may verify that regardless of the next selections, we obtain either the string "A" or the string "B" in which no letter can be selected. Thus, one must be careful in the sequence of selections chosen in order to solve a puzzle. Furthermore,
there are some puzzles that cannot be solved regardless of the choice of selections. For example, "ABBAAAAB" is not a solvable puzzle. Some facts are known about solvable puzzles: The empty string is solvable. If x and y are solvable puzzles, so are xy, AxA, and AxAyA for any uppercase letter
A. All other puzzles not covered by the rules above are unsolvable.
Given a puzzle, your task is to determine whether it can be solved or not.

输入

Each case of input is specified by a single line. Each line contains a string of uppercase letters. Each string has at least one but no more than 150 characters. The input is terminated by the end of file.

输出

For each input case, print solvable on a single line if there is a sequence of selections that solves the puzzle. Otherwise, print unsolvable on a line.

样例输入

ABBAABBAAB
ABBAAAAB

样例输出

solvable
unsolvable

题目大意:

每次去掉一段字符相同(两个以上)的去掉,问最后能不能去完。

dp[i][j]代表区间[i,j]能不能去完。然后就是讨论AxA,AA,AxAyA,xy几种情况。

#include <bits/stdc++.h>
using namespace std;
char s[155];
int dp[155][155];
int main()
{
    while(~scanf("%s",s+1))
    {
        memset(dp,0,sizeof dp);
        int len=strlen(s+1);
        for(int l=1;l<=len;l++)
        {
            for(int i=1;i+l<=len;i++)
            {
                int j=i+l;
                if(s[i]==s[j])
                {
                    if(dp[i+1][j-1]||l==1)///AxA||AA
                        dp[i][j]=1;
                    for(int k=i;k<=j;k++)///AxAyA
                        if(s[i]==s[k])
                            if((k-1<i+1||dp[i+1][k-1])&&(k+1>j-1||dp[k+1][j-1]))///AAyA||AxAA||AAA
                                dp[i][j]=1;
                }
                for(int k=i;k<=j;k++)///xy
                    if(dp[i][k]&&dp[k+1][j]) dp[i][j]=1;
            }
        }
        if(dp[1][len]) printf("solvable
");
        else printf("unsolvable
");
    }
    return 0;
}

 







以上是关于Clickomania(区间DP)的主要内容,如果未能解决你的问题,请参考以下文章

区间DP经典模型

HDU 5115 Dire Wolf ——(区间DP)

HDU 5115 Dire Wolf(区间dp)

区间DP小结(附经典例题) 转载

poj2955 区间dp

BZOJ1260 [CQOI2007]涂色paint(区间dp)