TJU4070ROAD dfs条件回溯

来源:互联网 发布:手机淘宝延迟收货 编辑:程序博客网 时间:2024/06/08 10:02
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

4RGGGBRGBRGBRGBRBBGGGRRRBRRBGGGBBBBR

Sample Output

88-150
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-6typedef 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 100005char 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 ;}// RGBRGGint 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;}


0 0