HDU 3427Clickomania

来源:互联网 发布:陆小凤传奇知乎 编辑:程序博客网 时间:2024/06/14 20:36


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
ABBAABBAABABBAAAAB
 

Sample Output
solvableunsolvable
区间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;}


0 0
原创粉丝点击