HDU3427

来源:互联网 发布:钱咖淘宝返利 编辑:程序博客网 时间:2024/06/06 00:49

1.题目描述:

Clickomania

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 628    Accepted Submission(s): 318


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
 

Source
Rocky Mountain Regional Contest 2009
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3424 3425 3432 3428 3429 
 

2.题意概述:

Clickomania(彩球消除)是一款游戏,有几种颜色不同的方块排成一列。每次可以将一段连续的颜色相同的方块消除掉,消除后原本这段方块两端的方块连接在一起,比如:ABBBA,将中间的BBB消除后,就变成了AA。现在给你一段字符串,不同的字符代表不同的颜色,问能不能将整个字符串消除完。

3.解题思路:

将字符串缩减成两个数组,一个存出现过的字符,另一个存这个字符出现的次数。例ABBBAACC压缩成ABAC 和 1322。从前往后扫描,每一个当前字符要么自成一串,构成合法序列,然后判断剩余字符串是否合法,要么和后面某一个相同字符合并,前提是他们中间的得是合法序列,按照这样分类方法直接dfs() + 标记。做这种类似的题,关键在于找到构成这些字符串的规则或叫词法,然后分类搜索+标记。

4.AC代码:

// 3427.cpp : 定义控制台应用程序的入口点。//#include <stdio.h>#include <string.h>#define N 155 using namespace std;char str[N], cha[N];int dp[N][N], num[N];int dfs(int l, int r){int i, t;if (l == r) {   /*如果就剩一种字符,直接判断是相连的个数是否大于1*/if (num[l] > 1)return 1;elsereturn 0;}if (dp[l][r] >= 0)return dp[l][r];/*当前字符和后面的任一个相同字符合并,前提是夹在他们中间的字符串是合法的*/char ch = cha[l];for (i = l + 1; i <= r; i++) {if (cha[i] == ch){if (dp[l + 1][i - 1] = dfs(l + 1, i - 1)) {t = dp[i][r];dp[i][r] = -1;num[i] += num[l];dp[i][r] = dfs(i, r);num[i] -= num[l];if (dp[i][r] == 1){dp[i][r] = t;return 1;}dp[i][r] = t;}}}/*当前字符是合法的,直接判断后一部分是否合法,即如果x,y都合法,则xy也合法*/if (num[l] > 1 && (dp[l + 1][r] = dfs(l + 1, r)))return 1;return 0;}int main(){while (scanf("%s", str) != EOF) {int len = strlen(str);if (len == 0) {puts("solvable");continue;}/*将字符串缩减成两个数组,一个存出现过的字符,另一个存这个字符出现的次数例ABBBAACC        缩成ABAC 和 1322*/int time = 1, step = 0;for (int i = 1; i <= len; i++) {if (str[i] != str[i - 1]) {cha[step] = str[i - 1];num[step] = time;time = 1;step++;}elsetime++;}memset(dp, -1, sizeof(dp));if (dfs(0, step - 1))puts("solvable");elseputs("unsolvable");}return 0;}


0 0
原创粉丝点击