1. 程式人生 > >ZOJ - 3983 Crusaders Quest(思維)

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;
}