【ZOJ3983】Crusaders Quest(思维+dfs)

题目链接

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.

【题意】

给一个9个字符的字符串,当有连续的3个字符时可释放1次大招,如果没有连续的3个字符时可选择删去中间的1个或2个连续字符从而组成3个连续字符,问这一串字符最多可以释放多少次大招。

【解题思路】

将连续字符的下标存入vector中,然后遍历这个vector,当遍历到的数据的两个下标相减<3时,删去继续递归,当>=3时说明删去这一串字符可以释放1个大招所以cnt++,并继续递归,当递归后的字符串长度为0时,本次递归结束,更新ans。

【代码】

#include<bits/stdc++.h>
using namespace std;
int ans;
void dfs(string s1,int cnt)
{
    int len=s1.length();
    vector<pair<int,int> >vec;
    if(!len)ans=max(ans,cnt);
    else
    {
        int a=0;
        for(int i=0;i<len;i++)
        {
            if(s1[a]!=s1[i])
            {
                vec.push_back(make_pair(a,i));
                a=i;
            }
        }
        vec.push_back(make_pair(a,len));
        vector<pair<int,int> >::iterator it;
        for(it=vec.begin();it!=vec.end();it++)
        {
            string s2=s1;
            s2.erase(s2.begin()+it->first,s2.begin()+it->second);
            int c=it->second-it->first;
            if(c>=3)
                dfs(s2,cnt+1);
            else
                dfs(s2,cnt);
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        ans=0;
        string s;
        cin>>s;
        dfs(s,0);
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81939873