TJU4070ROAD dfs条件回溯
来源:互联网 发布:手机淘宝延迟收货 编辑:程序博客网 时间:2024/06/08 10:02
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;}
- TJU4070ROAD dfs条件回溯
- !ZOJ 2100--DFS(递归结束条件和回溯还是没理解好)
- DFS回溯遍历
- DFS BFS 回溯法
- 回溯,BFS,DFS学习
- hdu 1045 DFS+回溯
- POJ1321 DFS+回溯
- hdu1258 DFS+回溯
- 1691(dfs + 回溯)
- hdu1010-dfs回溯
- HDU 1015 dfs回溯
- dfs + 回溯 +剪枝
- 棋盘问题 DFS+回溯
- 素数环 DFS +回溯
- hdu 1045 DFS回溯
- 素数环 DFS 回溯
- hdu1518 dfs,回溯
- 0.8poj2078(回溯||dfs)
- 对象所产生的构造函数,复制构造函数,析构,赋值函数
- form表单传递数组数据、php脚本接收
- 我写过的软件之FileExpert
- php和javascript中Json操作总结
- 查找数组中只出现一次的数(一)——Leetcode系列(十四)
- TJU4070ROAD dfs条件回溯
- 菜鸟,大牛和教主,三者的区别
- 机器学习中的相似性度量
- LUA 编辑器 易语言版 源码
- 新版的ViewHolder
- mybatis实现继承映射
- Android UI 优化——使用HierarchyViewer工具
- Web基础架构:负载均衡和LVS
- struts2的%{ } # $