Hduoj3427【DP】

/*Clickomania 
Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
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
Rocky Mountain Regional Contest 2009 
*/ 
#include<stdio.h>
#include<string.h>
char str[160];
int dp[160][160];//mark the solvable of the substring
bool Dp(int x, int y)
{
	int i;
	if(y < x)//empty
	return true;
	if(y == x)//one character
	return false;
	if(dp[x][y] == 1)//may have been marked
	return true;
	if(dp[x][y] == -1)
	return false;
	for(i = x+1; i < y-1; ++i)//xy
	{
		if(Dp(x,i) && Dp(i+1, y))
		{
			dp[x][y] = 1;
			return true;
		}
	}	
	if(str[x] == str[y])
	{
		if(Dp(x+1, y-1))//AxA
		{
			dp[x][y] = 1;
			return true;
		}
		for(i = x+1; i < y; ++i)
		{
			if(str[x] == str[i])//AxAyA
			{
				if(Dp(x+1,i-1) && Dp(i+1, y-1))
				{
					dp[x][y] = 1;
					return true;
				}
			}
		}
	}
	dp[x][y] = -1;
	return false;
}
int main()
{
	int i, j, k;
	while(scanf("%s", str) != EOF)
	{
		j = 0;
		k = strlen(str);
		memset(dp, 0, sizeof(dp));//initial
		if(Dp(j,k-1))
		printf("solvable\n");
		else
		printf("unsolvable\n");
	}
	return 0;
}


题意:消方块游戏,两个或两以上相同的相连的方块可以相消,这里方块用的大写字母来表示,问给出一个字符串,是否能将这个字符串消除为空。

思路:首先得看到题目中给了一个通式The empty string is solvable. If x and y are solvable puzzles, so are xy, AxA, and AxAyA for any uppercase letter A  。所以可以往dp方面想,即由公式从繁化简,用一个二维数组来dp表示字符串从x到y这一段是否能够完全消除掉,若能则标记为1,都则为-1。然后根据分类进行判断,首先是空的情况,即y小于x,空的时候返回1,即消除掉了。若x==y,说明消除到了最后只剩下一个字符,无法继续消除了,返回-1。这是最基本的返回条件,其次就是用xy,AxA,AxAyA三种类型来判断字符串,不断的递归,直到整个字符串有了返回值为止。

难点:这道题可以看做是搜索,也可以是dp,其实是一样的,dp不好理解,但好在于容易实现,搜索就是按照题意去模拟,比较容易想到。

附上原文链接:http://blog.sina.com.cn/s/blog_6635898a0100ql0a.html

你可能感兴趣的:(c)