HDU3427 - Clickomania - 区间dp+dfs+字符串处理

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
  
  
ABBAABBAAB ABBAAAAB
 

Sample Output
  
  
solvable unsolvable
 

Source
 

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;
		else
			return 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++;
			}
			else
				time++;
		}
		memset(dp, -1, sizeof(dp));
		if (dfs(0, step - 1))
			puts("solvable");
		else
			puts("unsolvable");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值