hdu 1685 Booksort (搜索)

15 篇文章 0 订阅
//从初始情况搜索两层并记录每次状态,这两层中如果有解,直接输出
//若是无解,从有序状态往回搜一层和两层,
//如果能达到与之前的相同的状态就是有解。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3f3f3f3f
#define zero 1e-8

using namespace std;
int step, n;
int arr[100];

int fin[100];

set<string> ff;

int makenum(int num, char *s, int &k)
{

    int i = 0, now[5] = {};
    for (; num;) {
        now[++i] = num % 10;
        num /= 10;
    }
    for (int j = i; j; j--) {
        s[k++] = now[j] + '0';
    }
    return k;

}

string makestr(int *s, int num)  //样式
{

    string aa;
    char a[100];
    int k = 0;
    for (int i = 0; i < num; ++i) {
        makenum(s[i], a, k);
        a[k++] = ' ';
    }
    a[k] = '\0';
    return a;

}
bool compare(int *s)
{
    for (int i = 0; i < n; ++i)
        if (fin[i] != s[i]) return false;

    return true;
}
void show(int *s)
{
    for (int i = 0; i < n; ++i)
        cout << s[i];
    cout << endl;
}
void cop(int *en, int *be)
{
    for (int i = 0; i < n; ++i)
        en[i] = be[i];
}
void dfs1(int *tem, int cnt, int now)
{
    string str = makestr(tem, n);
    ff.insert(str);
    if (step != inf) return ;
    if (cnt == now) {
        if (compare(tem)) {
            step = cnt;
        }

        return ;
    }

    if (cnt < now) return ;
    int tt[100];

    for (int j = 0; j < n; ++j) {

        int z;
        for (z = 0; z < j; ++z)
            tt[z] = tem[z];
        for (int len = 1; len < n - j; ++len) {
            cop(tt, tem);

            int f = z + len;
            for (int sert = 0; sert < n - j - len; ++sert) {
                tt[z + sert] = tem[f + sert];

                if (f + sert + 1 < n - j - len
                        && tem[f + sert + 1] == tt[z + sert] + 1) continue;

                for (int kk = 1; kk <= len; ++kk)
                    tt[z + sert + kk] = tem[j + kk - 1];

                string str = makestr(tt, n);
                ff.insert(str);
                dfs1(tt, cnt, now + 1);
            }

        }
    }
}

void dfs2(int *tem, int cnt, int now)
{

    if (step != inf) return ;
    string str = makestr(tem, n);
    if (cnt == now) {
        if (ff.find(str) != ff.end())
            step = 0;
        return ;
    }

    int tt[100];

    for (int j = 0; j < n; ++j) {

        int z;
        for (z = 0; z < j; ++z)
            tt[z] = tem[z];
        for (int len = 1; len < n - j; ++len) {
            cop(tt, tem);

            int f = z + len;
            for (int sert = 0; sert < n - j - len; ++sert) {
                tt[z + sert] = tem[f + sert];

                if (f + sert + 1 < n - j - len
                        && tem[f + sert + 1] == tt[z + sert] + 1) continue;

                for (int kk = 1; kk <= len; ++kk)
                    tt[z + sert + kk] = tem[j + kk - 1];
                dfs2(tt, cnt, now + 1);
            }

        }
    }
}

void makefin()
{
    for (int i = 0; i < n; ++i)
        fin[i] = i + 1;
}

int main()
{
    int t;
    cin >> t;
    for (; t--;) {
        scanf("%d", &n);

        for (int i = 0; i < n; ++i) {
            scanf("%d", &arr[i]);
        }
        makefin();
        step = inf;

        for (int i = 0; i <= 2; ++i) {
            step = inf;
            ff.clear();
            dfs1(arr, i, 0);
            if (step != inf) {
                printf("%d\n", step);
                break;
            }
        }
        if (step == inf) {
            for (int i = 1; i <= 2; ++i) {

                dfs2(fin, i, 0);
                if (step != inf) {
                    printf("%d\n", i + 2);
                    break;
                }
            }
            if (step == inf) printf("5 or more\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值