【每日一题】Shuffle'm Up

Shuffle’m Up

题意:

S1和S2是两叠牌,每叠C张,现在把S1和S2交替插入,S2的最后一张在最下面,构成新的牌叠S12。S12和模板牌叠比较,如果不一样,S12的上半部分为S1,下半部分为S2,继续操作,问几次操作后可以和模板牌叠完全一样?

题解:

可以暴力,我是好人,不暴力。用搜索做,就是最简单的搜索。

个人问题:

我卡了两个小时,菜,没话说。首先我高估了题目的难度,我以为分S1和S2的时候可以随机分,我错了,我英语不好;其次我高估了自己对函数的应用,好多函数我都是后来百度才知道的,惭愧,惭愧。

代码:
///Shuffle'm Up
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include <cstring> 
using namespace std;
struct xiao {
    char dataa[105];
    int shitt;
}now1, now2, well1, well2;
int C, t;
char crr[205], arr[100], brr[100];
void BFS(char A[], char B[]) {
    queue <xiao> Q;
    for (int i = 0; i < C; i++) {
        well1.dataa[i] = A[i];
        well2.dataa[i] = B[i];
    }
    well1.shitt = 1;
    well2.shitt = 1;
    Q.push(well1);
    Q.push(well2);
    while (!Q.empty()) {
        now1 = Q.front();
        Q.pop();
        now2 = Q.front();
        Q.pop();
        char  ans[300];
        int j = 0;
        for (int i = 0; i < C; i++) {
            ans[j++] = now2.dataa[i];
            ans[j++] = now1.dataa[i];
            ans[j] = '\0';
        }
        if (strcmp(crr, ans) == 0) {///比较函数
            cout << t << ' ' << now1.shitt << endl;
            return;
        }

        for (int i = 0; i < C; i++) {
            well1.dataa[i] = ans[i];
            well2.dataa[i] = ans[i + C];
        }
        well1.shitt = now1.shitt + 1;
        well2.shitt = now2.shitt + 1;
        if (well1.shitt > 1000) {
            cout << t << ' ' << -1 << endl;
            return;
        }
        Q.push(well1);
        Q.push(well2);
    }
}
int main() {
    int a;
    cin >> a;
    while (a--) {
        t++;
        cin >> C;
        cin >> arr;
        cin >> brr;
        cin >> crr;
        BFS(arr, brr);
    }
    return 0;
}
写在最后:

推荐两篇博客个人关于搜索的总结关于这个题一位同学的题解

发布了41 篇原创文章 · 获赞 16 · 访问量 1462

猜你喜欢

转载自blog.csdn.net/weixin_43824551/article/details/104846163
今日推荐