B. Blown Garland-干嘛写dfs过这题啊,明明只要一维数组就可以了

Codeforces Round #392 (Div. 2) B. Blown Garland
题目链接
一开始写了递归来解决,虽然过了,但是赛后看大神的代码,大神思路很简单,
因为题目保证输入是正确的,而且R’, ‘B’, ‘Y’ ,’G’至少有一盏,所以肯定遍历一遍’R’, ‘B’, ‘Y’ ,’G’的最后位置就会有了,注意是最后位置,拿着这个最后位置可以每4个一组的去测试原字符串,肯定是符合的,不符合的输入肯定是错的。
先贴长的递归。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;
const int maxn = 150;
char s[maxn];
char temp[maxn];
vector<int> v;
int r,b,y,g;
int len;
bool testcan(int plast,char *t,int inde){
    bool canR,canB,canY,canG;
    canR = canB = canY = canG = true;
//  printf("%d ",plast);
    if(plast+1<len){    
            if(t[plast+1]=='R')canR = false;
            if(t[plast+1]=='B')canB = false;
            if(t[plast+1]=='Y')canY = false;
            if(t[plast+1]=='G')canG = false;    
    }
    if(plast+2<len){
            if(t[plast+2]=='R')canR = false;
            if(t[plast+2]=='B')canB = false;
            if(t[plast+2]=='Y')canY = false;
            if(t[plast+2]=='G')canG = false;        
    }
    if(plast+3<len){
            if(t[plast+3]=='R')canR = false;
            if(t[plast+3]=='B')canB = false;
            if(t[plast+3]=='Y')canY = false;
            if(t[plast+3]=='G')canG = false;        
    }
    if(plast-1>=0){
            if(t[plast-1]=='R')canR = false;
            if(t[plast-1]=='B')canB = false;
            if(t[plast-1]=='Y')canY = false;
            if(t[plast-1]=='G')canG = false;    
    }
    if(plast-2>=0){
            if(t[plast-2]=='R')canR = false;
            if(t[plast-2]=='B')canB = false;
            if(t[plast-2]=='Y')canY = false;
            if(t[plast-2]=='G')canG = false;    
    }
    if(plast-3>=0){
            if(t[plast-3]=='R')canR = false;
            if(t[plast-3]=='B')canB = false;
            if(t[plast-3]=='Y')canY = false;
            if(t[plast-3]=='G')canG = false;    
    }

    if(canR){
    //      printf("R\n");
        t[plast]='R';
        r++;
        if( inde - 1  < 0 )return true;

        if(testcan(v[inde-1],t,inde-1))return true;
        else canR = false;
        r--;
    }
    if(canB){
    //      printf("B\n");
        t[plast]='B';
        b++;
        if( inde - 1  < 0 )return true;

        if(testcan(v[inde-1],t,inde-1))return true;
        else canB = false;
        b--;
    }
    if(canY){
    //      printf("Y\n");
        t[plast]='Y';
        y++;
        if( inde - 1  < 0 )return true;

        if(testcan(v[inde-1],t,inde-1))return true;
        else canY = false;
        y--;
    }
    if(canG){
        //  printf("G\n");
        t[plast]='G';
        g++;
        if( inde - 1  < 0 )return true;

        if(testcan(v[inde-1],t,inde-1))return true;
        else canG = false;
        g--;
    }
    if(canR == false && canY==false && canB==false && canG==false){
        t[plast] = s[plast];
        return false;
    }
    return true;
}
int main()
{
//  freopen("in.txt","r",stdin);
    scanf("%s",s);

    len = strlen(s);    
    for(int i=0;i<len;++i)temp[i]=s[i];
    temp[len]='\0';

    r = b = y = g =0;
    for(int i=0;i<len;++i)
    if(s[i]=='!')v.push_back(i);

    if(!v.empty())testcan(v[v.size()-1],temp,v.size()-1);   
    else {
        for(int i=0;i<len;++i){
            if(s[i]=='!'){
                if(temp[i]=='R')r++;
                if(temp[i]=='B')b++;
                if(temp[i]=='Y')y++;
                if(temp[i]=='G')g++;
            }
        }       
    }
    printf("%d %d %d %d\n",r,b,y,g);
    return 0;
}

再贴简单的版本,其中ha数组是计数的,a[4]是遍历一遍更新到最后 一定走得通的rygb组合

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long int ll;
const int maxn = 150;
char s[maxn],a[4];
int ha[maxn]={0};
int main()
{
    scanf("%s",s);
    int len = strlen(s);
    for(int i=0;i<len;++i)if(s[i]!='!')a[i%4] = s[i];
    for(int i=0;i<len;++i)if(s[i]=='!')ha[ a[i%4] ]++;
    printf("%d %d %d %d\n",ha['R'],ha['B'],ha['Y'],ha['G']);
    return 0;   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值