hdu 1685 Booksort (搜索)

来源:互联网 发布:最后的舞者 知乎 编辑:程序博客网 时间:2024/06/05 07:33
//从初始情况搜索两层并记录每次状态,这两层中如果有解,直接输出//若是无解,从有序状态往回搜一层和两层,//如果能达到与之前的相同的状态就是有解。#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-8using 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
原创粉丝点击