ZOJ - 3983 Crusaders Quest(思维)

ZOJ Problem Set - 3983

Crusaders Quest


Time Limit: 1 Second      Memory Limit: 65536 KB


Crusaders Quest is an interesting mobile game. A mysterious witch has brought great darkness to the game world, and the only hope for your kingdom is to save the Goddesses so that they can unleash their power to fight against the witch.

In order to save the game world, you need to choose three heroes to fight for victory and use their skills wisely. Nine skill blocks of three different types (three blocks per type) will be presented at the bottom of the screen. If  () consecutive blocks are of the same type, you can tap on them and eliminate them, thus triggering the powerful skill they represent. After the elimination, the blocks to their left will be connected with the blocks to their right. Moreover, if  consecutive blocks of the same type are eliminated, the powerful skill they unleash will be upgraded to a super skill, which is the most powerful skill of all.

DreamGrid is a newbie in this game, and he wants to trigger the super skill as many times as he can. Given nine skill blocks satisfying the description above, please help DreamGrid calculate the maximum number of times he can trigger the super skill.

Input

There are multiple test cases. The first line of input contains an integer  (about 50), indicating the number of test cases. For each test case:

The first line contains a string  () consisting of three 'g's, three 'a's and three 'o's, representing the nine skill blocks of three different types. Each type of character represents one type of skill block.

Output

For each test case, output an integer denoting the maximum number of times DreamGrid can trigger the super skill.

Sample Input

7
gggaaaooo
aaoogggoa
googgaaao
agogaooag
goooggaaa
gogogoaaa
gaogaogao

Sample Output

3
3
2
1
3
2
1

Hint

For the first sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "gggooo". He can then eliminate "ggg" (another super skill triggered) and finally eliminate "ooo" (a third super skill triggered). So the answer is 3.

For the second sample test case, DreamGrid can first eliminate "ggg" (one super skill triggered), thus changing the skill blocks to "aaoooa". He can then eliminate "ooo" (another super skill triggered) and finally eliminate "aaa" (a third super skill triggered). So the answer is also 3.

For the third sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "googgo". He can then eliminate "oo" to obtain "gggo", and eliminate "ggg" (another super skill triggered) to obtain "o". So the answer is 2. It is easy to prove that he cannot trigger the super skill three times under this arrangement of skill blocks.


Author: DAI, Longao
Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site

Submit    Status

题意:玩游戏打BOSS,需要消耗技能,现在有9个技能格,一共有3种技能,每种技能可以释放3次,3个同样的技能连续释放能够打出强有力的大招伤害。消耗了的技能就没有了,有点像“开心消消乐”。求我能够释放大招的最多次数。

思路:根据案例,我们需要考虑的情况太多了,会考虑不全:比如以下的情况

只能释放一次大招:

ooaggoaag 
oggoagoaa
oggoagoao
aoggoaaog 
ggoaagooa

只能释放二次大招

ogooagaga
oggaoaaog
oaaggaoog
goaaggaoo
ggaooaago
aaogaggoo
aggagoaoo

可以释放三次大招

gggaaaooo
aaoogggoa
goooggaaa 

找规律的做法太麻烦了,还容易找不全。

其实我们可以透过现象看本质。

1.遇见3个连续的技能,直接释放。如果最技能都释放完了,说明我可以释放3次大招;如果释放了一次,说明还剩6个技能,那我最多再释放一次大招。比如gooogaag , gooogaaga->ggaaga ;如果一次都没释放,说明还有9个技能,进入第2步

2.在没有连续的3个相同的技能这种情况下,我们最多释放二次大招,假如我释放的大招分别为"aaa"和“ggg”。组成这样的情况只有两种,①“aaa”和"ggg"不交叉,例如"aoaoaggog",“o”的技能随它怎么放都可以②“aaa”位于“ggg”的内部或“ggg”位于“aaa”的内部,例如"gaoaoaggo",“o”的技能随它怎么放都可以。

3.以上情况均不满足,当然只能释放一次大招了。

AC代码:

#include <cstdio>
using namespace std;
const int MAXN = 1e6+10;


//消除大招
int eliminate(char str[],int len){
	int flag=1;
	while(flag){
		flag=0;
		for(int i=1;i+2<=len;i++){
			if(str[i]==str[i+1]&&str[i+1]==str[i+2]){
				//ans++;
				flag=1;
				for(int j=i;j+3<=len;j++){
					str[j]=str[j+3];
				}
				len-=3;
				break;
			}
		}
	}
	return len;
}


int main(){
	char str[12],tmp[12];
	int T,n,m,cnt;
	scanf("%d",&T);
	while(T--){
		scanf("%s",str+1);
		//消除大招
		int len=eliminate(str,9);
		if(len==0){
			printf("3\n");
			continue;
		}
		if(len==6){
			printf("2\n");
			continue;
		}
		//消除'a',能否全部清除
		cnt=0;
		for(int i=1;i<=9;i++){
			if(str[i]!='a'){
				tmp[++cnt]=str[i];
			}
		}
		len=eliminate(tmp,cnt);
		if(len==0){
			printf("2\n");
			continue;
		}
		//消除'g',能否全部清除
		cnt=0;
		for(int i=1;i<=9;i++){
			if(str[i]!='g'){
				tmp[++cnt]=str[i];
			}
		}
		len=eliminate(tmp,cnt);
		if(len==0){
			printf("2\n");
			continue;
		}
		//消除'o',能否全部清除
		cnt=0;
		for(int i=1;i<=9;i++){
			if(str[i]!='o'){
				tmp[++cnt]=str[i];
			}
		}
		len=eliminate(tmp,cnt);
		if(len==0){
			printf("2\n");
			continue;
		}
		//以上情况均不满足,只能是1了。
		printf("%d\n",1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/83017539
ZOJ