翻硬币

    xiaoxiao2021-03-25  68

    题目:

    小明正在玩一个“翻硬币”的游戏。

    桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

    比如,可能情形是:**oo***oooo

    如果同时翻转左边的两个硬币,则变为:oooo***oooo

    现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

    我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:

    输入格式

    两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000

    输出格式

    一个整数,表示最小操作步数。

    样例输入1 ********** o****o**** 样例输出1 5 样例输入2 *o**o***o*** *o***o**o*** 样例输出2

    1

    参考代码:

    #include<iostream> using namespace std; int first[1000]; int answer[1000]; int len = 0; int start,end; int times = 0; void turn(int i) { first[i] = 1 - first[i]; first[i+1] = 1 - first[i+1]; } void move(int s) { if(s == end) return; if(first[s] != answer[s]) { turn(s); times ++; } move(s+1); } int main() { char s1[1000],s2[1000]; cin >> s1 >> s2; int flag = 0; for(int i = 0;s1[i] != '\0';i ++) { if(s1[i] == '*') first[i] = 1; if(s1[i] == 'o') first[i] = 0; if(s2[i] == '*') answer[i] = 1; if(s2[i] == 'o') answer[i] = 0; len ++; if(!flag && first[i] != answer[i]) { start = i; flag = 1; } if(flag && first[i] != answer[i]) end = i; } move(start); cout << times << endl; return 0; }

    这个题跟cowboys很相似,但要容易很多

    关于cowboys的参考解答http://blog.csdn.net/qq_35078631/article/details/60466206

    转载请注明原文地址: https://ju.6miu.com/read-39156.html

    最新回复(0)