首页 > 其他分享 >HDU 3427 Clickomania

HDU 3427 Clickomania

时间:2022-11-09 20:33:57浏览次数:57  
标签:HDU return string Clickomania puzzle 3427 dfs flag letter


Problem Description


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.


Input


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.


Output


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.


Sample Input


ABBAABBAAB
ABBAAAAB


 


Sample Output

solvable
unsolvable



区间dp或者记忆化搜索,按道理区间dp快一些,不过这里偷懒用了dfs


#include<map>
#include<queue>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
const int N=155;
int f[N][N];
char s[N];

bool dfs(int l,int r)
{
if (l==r) return false;
if (l>r) return true;
if (l+1==r) return s[l]==s[r];
if (f[l][r]) return f[l][r]==1;

bool flag=true;
rep(i,l,r) if (s[i]!=s[l]) flag=false;
if (flag) return f[l][r]=1;

flag|=s[l]==s[r]&&dfs(l+1,r-1);
if (flag) return f[l][r]=1;

rep(i,l,r-1)
{
flag|=dfs(l,i)&&dfs(i+1,r);
}
if (flag) return f[l][r]=1;

rep(i,l+1,r-1)
{
flag|=s[l]==s[r]&&s[l]==s[i]&&dfs(l+1,i-1)&&dfs(i+1,r-1);
}
return flag?f[l][r]=1:(f[l][r]=-1)+1;
}

int main()
{
while (~scanf("%s",s+1))
{
memset(f,0,sizeof(f));
printf("%s\n",dfs(1,strlen(s+1))?"solvable":"unsolvable");
}
return 0;
}



标签:HDU,return,string,Clickomania,puzzle,3427,dfs,flag,letter
From: https://blog.51cto.com/u_15870896/5838735

相关文章

  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......
  • HDU 1010 Tempter of the Bone
    DescriptionThedoggiefoundaboneinanancientmaze,whichfascinatedhimalot.However,whenhepickeditup,themazebegantoshake,andthedoggie......
  • HDU 1017 A Mathematical Curiosity
    DescriptionGiventwointegersnandm,countthenumberofpairsofintegers(a,b)suchthat0<a<b<nand(a^2+b^2+m)/(ab)isaninteger. Thi......
  • HDU 5605 geometry
    ProblemDescriptionThereisapoint  atcoordinate .Alinegoesthroughthepoint,andintersectswiththepostivepartof  axesatpoint .Plea......
  • HDU 1329 Hanoi Tower Troubles Again!
    DescriptionPeoplestoppedmovingdiscsfrompegtopegaftertheyknowthenumberofstepsneededtocompletetheentiretask.Butontheotherhand,they......
  • HDU 2475 Box
    DescriptionThereareNboxesontheground,whicharelabeledbynumbersfrom1toN.Theboxesaremagical,thesizeofeachonecanbeenlargedorreduc......
  • HDU 5432 Pyramid Split
    ProblemDescriptionXiaoMingisacitizenwho'sgoodatplaying,hehaslot'sofgoldconeswhichhavesquareundersides,let'scallthempyramids.Anyo......
  • HDU 5496 Beauty of Sequence
    ProblemDescriptionSequenceisbeautifulandthebeautyofanintegersequenceisdefinedasfollows:removesallbutthefirstelementfromeveryconse......
  • HDU 5433 Xiao Ming climbing
    ProblemDescriptionDuetothecursemadebythedevil,XiaoMingisstrandedonamountainandcanhardlyescape.Thismountainisprettystrangethat......