zoj--3983(CCPC秦皇岛)

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 k (k>=1) 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 k=3consecutive 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 T (about 50), indicating the number of test cases. For each test case:

The first line contains a string  s(|s|=9) 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

解题思路:
对于每组的测试样例,进行第一次扫描,可能出现的情况两种:
1.第一次扫描出现了三个相连的情况,把这个三个相连的去掉(也可以用‘0’表示已经去掉),然后再次进行扫描(因为可能去掉后,又出现相连的)这是这时又出现三个相连的,则说明原来的字符串的结果是3,否则是2(因为 在剩下的六个子母中,已经宝山正没有三个连续的字母,所以无论怎么消除只能出一个三个连续 )    
2.第一次扫描不出现三个相连的情况。这时最后的结果可能是1或2.。
在这个时候我们采用枚举的方法,要么去除g,要么去除a,要么去除o。
比如当去除g后的字符串,扫描接下来的字符串,结果要么2,要么0.
但是当等于0的时候 ,要加1。(因为在剩下的六个子母中,已经宝山正没有三个连续的字母,所以无论怎么消除只能出一个三个连续 )。
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
char ans[9],cns[9];
int compare(char bns[9])
{
    int x=1;
    int num=0;
    while(x)
    {
        x=0;
        for(int i=0; i<7; i++)
        {
            int num1,num2;
            num1=num2=10;
            if(bns[i]!='0')
            {
                for(int j=i+1; j<9; j++)
                {
                    if(bns[j]!='0'&&num1==10)
                        num1=j;
                    else if(bns[j]!='0'&&num2==10)
                        num2=j;
                    if(num1!=10&&num2!=10) break;
                }
            }
            if(num1==10||num2==10) continue;
            else
            {
                if(bns[i]==bns[num1]&&bns[num1]==bns[num2])
                {
                    num++;
                    x=1;
                    bns[i]='0';
                    bns[num1]='0';
                    bns[num2]='0';
                }
            }
        }
    }
    ///printf("**********%d*****************",num);
    return num;
}
int main()
{
    int t,num,flag;
    ///char ans[9];
    ///char cns[9];
    scanf("%d",&t);
    while(t--)
    {
        num=flag=0;
        scanf("%s",ans);
        num=compare(ans);
        if(num==1) printf("2\n");
        else if(num==3) printf("3\n");
        else if(num==0)
        {
            int num2,num3,num4;
            num2=num3=num4=0;
            for(int i=0; i<9; i++)
            {
                cns[i]=ans[i];
            }
            for(int i=0; i<9; i++)
            {
                if(cns[i]=='g') cns[i]='0';
            }
            num2=compare(cns);
            if(num2==0) num2=1;
            for(int i=0; i<9; i++)
            {
                cns[i]=ans[i];
            }
            for(int i=0; i<9; i++)
            {
                if(cns[i]=='a') cns[i]='0';
            }
            num3=compare(cns);
            if(num3==0) num3=1;
            for(int i=0; i<9; i++)
            {
                cns[i]=ans[i];
            }
            for(int i=0; i<9; i++)
            {
                if(cns[i]=='o') cns[i]='0';
            }
            num4=compare(cns);
            if(num4==0) num4=1;
            printf("%d\n",max(num2,max(num4,num3)));
        }
    }
    return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值