TJU4070ROAD dfs条件回溯

4070.   Road
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 61    Accepted Runs: 55



There is a one-dimensional road. The road is separated into N consecutive parts. The parts are numbered 0 through N-1, in order. MJ is going to walk from part 0 to part N-1.

MJ also noticed that each part of the road has a color: either red, green, or blue. Part 0 is red.

MJ is going to perform a sequence of steps. Each step must lead in the positive direction. That is, if his current part is i, the next step will take him to one of the parts i+1 through N-1, inclusive. His steps can be arbitrarily long. However, longer steps are harder: a step of length j costs j*j energy.

Additionally, MJ wants to step on colors in a specific order: red, green, blue, red, green, blue, ... That is, he starts on the red part 0, makes a step to a green part, from there to a blue part, and so on, always repeating red, green, and blue in a cycle. Note that the final part N-1 also has some color and thus MJ must reach it in a corresponding step.

You are given a String road containing N elements. For each i, element i of road is the color of part i: 'R' represents red, 'G' green, and 'B' blue. If MJ can reach part N-1 in the way described above, return the smallest possible total cost of doing so. Otherwise, return -1.

Input

First line is a integer T, the number of test cases.

The next T lines, each line is a string S represent the road.The length of S is no longer than 20.

Output

For each test case, output the smallest possible total cost or -1 if it's impossible.

Sample Input

4
RGGGB
RGBRGBRGB
RBBGGGRR
RBRRBGGGBBBBR

Sample Output

8
8
-1
50


dfs当发现从当前节点无法到达最终目标时回溯,直到可以到达的字母,比如RGBG
走R-G-B-/-G,回溯至R-G

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
char s[100];
char rear;
char step[3] = {'R', 'G', 'B'};
int l;
int minn;
char des;
int sqr(int n)
{
    return n * n;
}
char get(char pre)
{
    if (pre == 'R')
        return 'G';
    else if (pre == 'G')
        return 'B';
    else
        return 'R';
}
bool flag;
void dfs(char pre, int k, int sum)
{
    if (!flag && pre != des)
    {
        return;
    }
    if (pre == rear && k == l - 1)
    {
        minn = min(minn, sum);
        return ;
    }
    char next = get(pre);
    int nowsum;

    for (int i = k; i < l; i++)
    {
        flag = false;
        if (s[i] == next)
        {
            flag = true;
            nowsum = sum + sqr(i - k);
            dfs(s[i], i, nowsum);
        }
    }
    return ;
}
// RGBRGG
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n) + 1)
    {
        scanf("%s", s);
        l = strlen(s);
        rear = s[l - 1];
        minn = INF;
        if (rear == 'R')
        {
            des = 'B';
        }
        else if (rear == 'G')
        {
            des = 'R';
        }
        else
        {
            des = 'G';
        }
        flag = true;
        int sum=0;
        int start=1;
        for(int i=1;i<l;i++)
        {
            if(s[i]=='G')
            {
                sum=sqr(i);
                start=i;
                break;
            }
        }
        dfs('R', start, sum);
        if (minn == INF)
            printf("-1\n");
        else
            printf("%d\n", minn);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值