zoukankan      html  css  js  c++  java
  • 八数码八境界代码

     本文转自:http://www.cnblogs.com/zufezzt/p/5659276.html

    判断无解的情况(写完七种境界才发现有直接判断无解的方法):

    一个状态表示成一维的形式,求出除0之外所有数字的逆序数之和,也就是每个数字前面比它大的数字的个数的和,称为这个状态的逆序。

    若两个状态的逆序奇偶性相同,则可相互到达,否则不可相互到达。

    POJ提交记录(从下往上依次为第1,2,3,4,5,6,7,8境界):

     本文转自:http://www.cnblogs.com/zufezzt/p/5659276.html

    HDU提交记录(从下往上依次为第1,2,3,4,5,6,7,8境界):

    PS:因为HDU是多组测试数据,所以一般POJ上要100ms以上才能AC的方法,在HDU上是无法通过的(境界3除外)

    境界一、 暴力广搜+STL (HDU 内存超限,POJ 时间超限)

    map存路径,set判重,string存状态,毫无疑问,炸了。

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<queue>
    #include<set>
    #include<map>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    char input[1000];
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d = "durl";
    set<string>f;
    map<string, string>m;
    int sz = 0;
    
    struct node
    {
        string s;
        string path;
        int pos;
    
        node() {}
        node(string str, string pa, int Pos)
        {
            s = str;
            path = pa;
            pos = Pos;
        }
    };
    
    bool g(int a, int b)
    {
        if (a >= 0 && a <= 2 && b >= 0 && b <= 2) return 1;
        return 0;
    }
    
    void pre()
    {
        queue<node>q;
        q.push(node("12345678x", "", 8));
        m["12345678x"] = "";
        f.insert("12345678x");
    
        while (!q.empty())
        {
            node h = q.front(); q.pop();
            int a = h.pos / 3, b = h.pos % 3;
            for (int i = 0; i<4; i++)
            {
                int x = a + dir[i][0], y = b + dir[i][1];
                if (!g(x, y)) continue;
                int pos = 3 * x + y;
                swap(h.s[h.pos], h.s[pos]);
                if (f.find(h.s) != f.end())
                {
                    swap(h.s[h.pos], h.s[pos]);
                    continue;
                }
                q.push(node(h.s, d[i] + h.path, pos));
                f.insert(h.s);
                m[h.s] = d[i] + h.path;
                swap(h.s[h.pos], h.s[pos]);
            }
        }
    
    }
    
    int main()
    {
        pre();
        while(~scanf("%s",input))
        {
            string v="";
            v = v + input[0];
            for (int i = 1; i <= 8; i++)
            {
                scanf("%s", input);
                v = v + input[0];
            }
            if (m[v] == "") cout << "unsolvable" << endl;
            else cout << m[v] << endl;
        }
    
        return 0;
    }
    View Code

    境界二、广搜+哈希(POJ 453ms)

    利用康托展开对状态进行hash,hash值对应0--(9!-1),因此可以开数组判重,路径的记录可以记录到达某状态的最后一步操作是什么与父节点是什么。

    从输入的状态开始进行BFS,直到找到最终状态。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    
    char t[1000];
    int c[10];
    int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    char path[10]={'u','d','l','r'};
    char op[5],input[20];
    int ans;
    stack<int>S;
    
    struct Node
    {
        int s,p;
        Node(){}
        Node(int S,int P){s=S,p=P;}
    };
    
    struct Path
    {
        int from,dir;
    }pa[400000];
    bool f[400000];
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    void bfs(int val,int Pos)
    {
        queue<Node>Q;
        Q.push(Node(val,Pos));
        f[val]=1; pa[val].from=-1,pa[val].dir=-1;
    
        while(!Q.empty())
        {
            Node h=Q.front(); Q.pop();
    
            if(h.s==0)
            {
                ans=1;
                int now=h.s;
                while(1)
                {
                    if(pa[now].from==-1) break;
                    S.push(pa[now].dir);
                    now=pa[now].from;
                }
                break;
            }
    
    
            int a=h.p/3, b=h.p%3; getstr(h.s);
    
            for(int i=0;i<4;i++)
            {
                int x=a+dir[i][0],y=b+dir[i][1];
                if(!(x>=0&&x<=2&&y>=0&&y<=2)) continue;
                int newpos=3*x+y;
                swap(t[newpos],t[h.p]);
                int news=getnum();
                if(f[news]) {swap(t[newpos],t[h.p]);continue;}
                pa[news].from=h.s, pa[news].dir=i, f[news]=1;
                Q.push(Node(news,newpos));
                swap(t[newpos],t[h.p]);
            }
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            t[0]=op[0];
            int pos;
            for(int i=1;i<=8;i++)
            {
                scanf("%s",op); t[i]=op[0];
                if(t[i]=='x') pos=i;
            }
            int state=getnum();
    
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1) { printf("unsolvable
    "); continue; }
    
            ans=0;
            memset(f,0,sizeof f);
            bfs(state,pos);
    
            while(!S.empty())
            {
                printf("%c",path[S.top()]);
                S.pop();
            }
            printf("
    ");
    
        }
        return 0;
    }
    View Code

    境界三、广搜+哈希+打表(HDU 263ms,POJ 579ms)

    从最终状态(0)开始进行BFS。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    
    char t[1000];
    int c[10];
    int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    char path[10]={'d','u','r','l'};
    char op[5],input[20];
    
    struct Node
    {
        int s,p;
        Node(){}
        Node(int S,int P){s=S,p=P;}
    };
    
    struct Path
    {
        int from,dir;
    }pa[400000];
    bool f[400000];
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    void pre()
    {
        queue<Node>Q;
        Q.push(Node(0,8));
        f[0]=1; pa[0].from=-1,pa[0].dir=-1;
    
        while(!Q.empty())
        {
            Node h=Q.front(); Q.pop();
            int a=h.p/3, b=h.p%3; getstr(h.s);
            for(int i=0;i<4;i++)
            {
                int x=a+dir[i][0],y=b+dir[i][1];
                if(!(x>=0&&x<=2&&y>=0&&y<=2)) continue;
                int newpos=3*x+y;
                swap(t[newpos],t[h.p]);
                int news=getnum();
                if(f[news]) {swap(t[newpos],t[h.p]);continue;}
                pa[news].from=h.s, pa[news].dir=i, f[news]=1;
                Q.push(Node(news,newpos));
                swap(t[newpos],t[h.p]);
            }
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        pre();
    
        while(~scanf("%s",op))
        {
            t[0]=op[0];
            for(int i=1;i<=8;i++) {scanf("%s",op); t[i]=op[0];}
            int state=getnum();
            if(f[state]==0) printf("unsolvable
    ");
            else
            {
                while(1)
                {
                    if(pa[state].from==-1) break;
                    printf("%c",path[pa[state].dir]);
                    state=pa[state].from;
                }
                printf("
    ");
            }
        }
        return 0;
    }
    View Code

    境界四、双向广搜+哈希(HDU 2636ms, POJ 32ms)

    从起点和终点同时开始搜,当某个状态被两个方向的搜索同时搜过时,搜索结束,输出路径。

    HDU 不加无解判断剪枝会超时。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    char input[1000],t[1000],op[5];
    int c[10];
    struct Node
    {
        int s,p;
        Node(){}
        Node(int S,int P){s=S,p=P;}
    };
    struct Path
    {
        int from,dir;
    }path[400000];
    int f[400000];
    
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d[3] = {"","udlr","durl"};
    
    queue<Node>q[3];
    int ans;
    
    stack<int>S;
    queue<int>Q;
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    bool g(int a, int b)
    {
        if (a >= 0 && a <= 2 && b >= 0 && b <= 2) return 1;
        return 0;
    }
    
    void bfs(int now)
    {
        Node h=q[now].front(); q[now].pop();
        int a=h.p/3,b=h.p%3;
        getstr(h.s);
    
        for(int i=0;i<4;i++)
        {
            int x=a+dir[i][0],y=b+dir[i][1];
            if(!g(x,y)) continue;
            int pos = 3 * x + y;
    
            swap(t[h.p],t[pos]);
    
            if(f[getnum()]==now) { swap(t[h.p],t[pos]); continue; }
            else if(f[getnum()]!=0)
            {
                ans=1;
                if(now==1)
                {
                    S.push(i);
                    int u=h.s;
                    while(path[u].from!=-1) { S.push(path[u].dir); u=path[u].from; }
                    u=getnum();
                    while(path[u].from!=-1) { Q.push(path[u].dir); u=path[u].from; }
                }
                else
                {
                    Q.push(i);
                    int u=h.s;
                    while(path[u].from!=-1) { Q.push(path[u].dir); u=path[u].from; }
                    u=getnum();
                    while(path[u].from!=-1) { S.push(path[u].dir); u=path[u].from; }
                }
                break;
            }
            else
            {
                f[getnum()]=now;
                path[getnum()].from=h.s;
                path[getnum()].dir=i;
                q[now].push(Node(getnum(),pos));
                swap(t[h.p],t[pos]);
            }
        }
    }
    
    void read()
    {
        t[0]=op[0];
        for(int i=1;i<=8;i++)
            {scanf("%s",op); t[i]=op[0];}
        for(int i=0;i<=9;i++) input[i]=t[i];
    }
    
    void init()
    {
        memset(f,ans=0,sizeof f);
        while(!q[1].empty()) q[1].pop();
        while(!q[2].empty()) q[2].pop();
    }
    
    void work(int s,int pos)
    {
        q[1].push(Node(s,pos)); q[2].push(Node(0,8));
        f[s]=1; path[s].from=path[s].dir=-1;
        f[0]=2; path[0].from=path[0].dir=-1;
        while((!q[1].empty())&&(!q[2].empty()))
        {
            if(ans==1) break;
            bfs(1); if(ans==1) break;
            bfs(2); if(ans==1) break;
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            read();
    
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1)
            {
                printf("unsolvable
    ");
                continue;
            }
             init();
            for(int i=0;i<9;i++)
                if(input[i]=='x'){work(getnum(),i); break; }
            if(ans==1)
            {
                while(!S.empty()) { printf("%c",d[1][S.top()]); S.pop(); }
                while(!Q.empty()) { printf("%c",d[2][Q.front()]); Q.pop(); }
            }
            else printf("unsolvable");
            printf("
    ");
        }
        return 0;
    }
    View Code

    境界五、A*+哈希+简单估价函数(POJ 391ms)

    之后的境界采用最小堆优化,这里我直接用了,f小的先取

    试了一下用普通队列的,G++跑了680ms左右,C++TLE(主要原因是写法不好,我常数写的有点大了)。显然最小堆优化在效率上有极大的提高。

    g(n)是深度,即从初始到目前的操作次数,h(n)是简单估价函数,表示目前状态与最终状态同一位置不同数字的个数。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    char input[1000],t[1000],op[5];
    int c[10];
    struct Node
    {
        int s,p;
        int f,g,h;
        Node(){}
        Node(int S,int P,int G,int H)
        {
            s=S,p=P;
            g=G,h=H;
            f=g+h;
        }
        bool operator < (const Node &a) const {
            return f>a.f;
        }
    };
    
    struct Path
    {
        int from,dir;
    }path[400000];
    int flag[400000];
    int G[400000];
    
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d = "udlr";
    
    priority_queue<Node>Q;
    int ans;
    
    stack<int>S;
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    bool g(int a, int b)
    {
        if (a>=0 && a<=2 && b>=0 && b<=2) return 1;
        return 0;
    }
    
    void read()
    {
        t[0]=op[0];
        for(int i=1;i<=8;i++) {scanf("%s",op); t[i]=op[0];}
        for(int i=0;i<=9;i++) input[i]=t[i];
    }
    
    void init()
    {
        memset(G,-1,sizeof G);
        memset(flag,0,sizeof flag);
        while(!Q.empty()) Q.pop();
    }
    
    int H(int val)
    {
        char tmp[10];
        for(int i=0;i<=9;i++) tmp[i]=t[i];
    
        getstr(val);
    
        int res=0;
        for(int i=0;i<9;i++)
        {
            if(i<8) if(t[i]-'0'!=i+1) res++;
            if(i==8&&t[i]!='x') res++;
        }
    
        for(int i=0;i<=9;i++) t[i]=tmp[i];
        return res;
    }
    
    void A_star(int s,int pos)
    {
        flag[s]=2; G[s]=0;
        path[s].from=-1;
        path[s].dir=-1;
        Q.push(Node(s,pos,0,H(s)));
        while(!Q.empty())
        {
            Node h=Q.top(); Q.pop(); flag[h.s]=2;
            getstr(h.s);
            if(h.s==0)
            {
                ans=1; int now=h.s;
                while(1)
                {
                    if(path[now].from==-1) return;
                    S.push(path[now].dir);
                    now=path[now].from;
                }
            }
    
            int a=h.p/3,b=h.p%3;
    
            for(int i=0;i<4;i++)
            {
                int x=a+dir[i][0],y=b+dir[i][1];
                if(!g(x,y)) continue;
                int newpos=3*x+y;
    
                swap(t[h.p],t[newpos]);
                int news=getnum();
                swap(t[h.p],t[newpos]);
    
                if(flag[news]==0||(flag[news]==1&&h.g+1<G[news]))
                {
                    flag[news]=1; G[news]=h.g+1;
                    path[news].from=h.s;
                    path[news].dir=i;
                    Q.push(Node(news,newpos,h.g+1,H(news)));
                }
            }
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            read();
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1)
            {
                printf("unsolvable
    ");
                continue;
            }
            init();
    
            for(int i=0;i<9;i++)
                if(input[i]=='x'){A_star(getnum(),i); break; }
            while(!S.empty()){printf("%c",d[S.top()]); S.pop();}
            printf("
    ");
        }
        return 0;
    }
    View Code

    境界六、A*+哈希+曼哈顿距离 (POJ 735ms)

    与境界5唯一不同的是 估价函数h()。这里估价函数采用曼哈顿距离。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    char input[1000],t[1000],op[5];
    int c[10];
    struct Node
    {
        int s,p;
        int f,g,h;
        Node(){}
        Node(int S,int P,int G,int H)
        {
            s=S,p=P;
            g=G,h=H;
            f=g+h;
        }
    };
    
    struct Path
    {
        int from,dir;
    }path[400000];
    int flag[400000];
    int G[400000];
    
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d = "udlr";
    
    queue<Node>Q;
    int ans;
    
    stack<int>S;
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    bool g(int a, int b)
    {
        if (a>=0 && a<=2 && b>=0 && b<=2) return 1;
        return 0;
    }
    
    void read()
    {
        t[0]=op[0];
        for(int i=1;i<=8;i++) {scanf("%s",op); t[i]=op[0];}
        for(int i=0;i<=9;i++) input[i]=t[i];
    }
    
    void init()
    {
        memset(G,-1,sizeof G);
        memset(flag,0,sizeof flag);
        while(!Q.empty()) Q.pop();
    }
    
    int H(int val)
    {
        char tmp[10];
        for(int i=0;i<=9;i++) tmp[i]=t[i];
    
        getstr(val);
    
        int res=0;
    
        for(int i=0;i<9;i++)
        {
            if(t[i]=='x') continue;
    
            int num=t[i]-'0'-1;
            int a=i/3,b=i%3;
            int x=num/3,y=num%3;
    
            res=res+abs(a-x)+abs(b-y);
        }
    
    
        for(int i=0;i<=9;i++) t[i]=tmp[i];
        return res;
    }
    
    void A_star(int s,int pos)
    {
        flag[s]=2; G[s]=0;
        path[s].from=-1;
        path[s].dir=-1;
        Q.push(Node(s,pos,0,H(s)));
        while(!Q.empty())
        {
            Node h=Q.front(); Q.pop(); flag[h.s]=2;
            getstr(h.s);
            if(h.s==0)
            {
                ans=1; int now=h.s;
                while(1)
                {
                    if(path[now].from==-1) return;
                    S.push(path[now].dir);
                    now=path[now].from;
                }
            }
    
            int a=h.p/3,b=h.p%3;
    
            for(int i=0;i<4;i++)
            {
                int x=a+dir[i][0],y=b+dir[i][1];
                if(!g(x,y)) continue;
                int newpos=3*x+y;
    
                swap(t[h.p],t[newpos]);
                int news=getnum();
                swap(t[h.p],t[newpos]);
    
                if(flag[news]==0||(flag[news]==1&&h.g+1<G[news]))
                {
                    flag[news]=1; G[news]=h.g+1;
                    path[news].from=h.s;
                    path[news].dir=i;
                    Q.push(Node(news,newpos,h.g+1,H(news)));
                }
            }
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            read();
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1)
            {
                printf("unsolvable
    ");
                continue;
            }
            init();
            for(int i=0;i<9;i++)
                if(input[i]=='x'){A_star(getnum(),i); break; }
    
            while(!S.empty()){printf("%c",d[S.top()]); S.pop();}
            printf("
    ");
        }
        return 0;
    }
    View Code

    境界七、A*+哈希+曼哈顿距离+小顶堆 (HDU 1404ms,POJ 16ms)

    境界六加上最小堆优化,这个优化的效率改进是巨大的!!!!

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    char input[1000],t[1000],op[5];
    int c[10];
    struct Node
    {
        int s,p;
        int f,g,h;
        Node(){}
        Node(int S,int P,int G,int H)
        {
            s=S,p=P;
            g=G,h=H;
            f=g+h;
        }
        bool operator < (const Node &a) const {
            return f>a.f;
        }
    };
    
    struct Path
    {
        int from,dir;
    }path[400000];
    int flag[400000];
    int G[400000];
    
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d = "udlr";
    
    priority_queue<Node>Q;
    int ans;
    
    stack<int>S;
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    bool g(int a, int b)
    {
        if (a>=0 && a<=2 && b>=0 && b<=2) return 1;
        return 0;
    }
    
    void read()
    {
        t[0]=op[0];
        for(int i=1;i<=8;i++) {scanf("%s",op); t[i]=op[0];}
        for(int i=0;i<=9;i++) input[i]=t[i];
    }
    
    void init()
    {
        memset(G,-1,sizeof G);
        memset(flag,0,sizeof flag);
        while(!Q.empty()) Q.pop();
    }
    
    int H(int val)
    {
        char tmp[10];
        for(int i=0;i<=9;i++) tmp[i]=t[i];
    
        getstr(val);
    
        int res=0;
    
        for(int i=0;i<9;i++)
        {
            if(t[i]=='x') continue;
    
            int num=t[i]-'0'-1;
            int a=i/3,b=i%3;
            int x=num/3,y=num%3;
    
            res=res+abs(a-x)+abs(b-y);
        }
    
    
        for(int i=0;i<=9;i++) t[i]=tmp[i];
        return res;
    }
    
    void A_star(int s,int pos)
    {
        flag[s]=2; G[s]=0;
        path[s].from=-1;
        path[s].dir=-1;
        Q.push(Node(s,pos,0,H(s)));
        while(!Q.empty())
        {
            Node h=Q.top(); Q.pop(); flag[h.s]=2;
            getstr(h.s);
            if(h.s==0)
            {
                ans=1; int now=h.s;
                while(1)
                {
                    if(path[now].from==-1) return;
                    S.push(path[now].dir);
                    now=path[now].from;
                }
            }
    
            int a=h.p/3,b=h.p%3;
    
            for(int i=0;i<4;i++)
            {
                int x=a+dir[i][0],y=b+dir[i][1];
                if(!g(x,y)) continue;
                int newpos=3*x+y;
    
                swap(t[h.p],t[newpos]);
                int news=getnum();
                swap(t[h.p],t[newpos]);
    
                if(flag[news]==0||(flag[news]==1&&h.g+1<G[news]))
                {
                    flag[news]=1; G[news]=h.g+1;
                    path[news].from=h.s;
                    path[news].dir=i;
                    Q.push(Node(news,newpos,h.g+1,H(news)));
                }
            }
        }
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            read();
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1)
            {
                printf("unsolvable
    ");
                continue;
            }
            init();
            for(int i=0;i<9;i++)
                if(input[i]=='x'){A_star(getnum(),i); break; }
    
            while(!S.empty()){printf("%c",d[S.top()]); S.pop();}
            printf("
    ");
        }
        return 0;
    }
    View Code

    境界八、IDA*+曼哈顿距离(HDU 2106ms,POJ 79ms)

    采用迭代深搜的思想,利用A*估价函数剪枝。

    第一次允许搜索到的深度为limit,如果有解,直接输出。

    在最大值深度为limit的情况下,如果无解,则将limit++,继续从头开始搜索。直到搜到解。

    剪枝:if(deep+h(s)>limit) return; deep表示这一次搜索达到此状态的操作次数,h(s)为估价函数。

    IDA*策略占用空间极小,一般情况下效率会比A*高。

    至于为什么我的IDA*比A*慢.....估计我这种写法常数太大了吧......

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    char input[1000],t[1000],op[5];
    int c[10];
    int dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
    string d = "udlr";
    int flag,limit,length;
    int path[1000];
    
    int getnum()
    {
        int res=0;
        for(int i=0;t[i];i++)
            for(int j=i+1;t[j];j++)
                if(t[j]<t[i]) res=res+c[8-i];
        return res;
    }
    
    void getstr(int val)
    {
        int tmp[10],flag[10];
        memset(flag,0,sizeof flag);
        for(int i=0;i<9;i++) tmp[i]=val/c[8-i],val=val%c[8-i];
        for(int i=0;i<9;i++)
        {
            int num=0;
            for(int j=0;j<9;j++)
            {
                if(flag[j]==0) num++;
                if(num==tmp[i]+1)
                {
                    t[i]=j+'0'+1; if(t[i]=='9') t[i]='x';
                    flag[j]=1;break;
                }
            }
        }
    }
    
    bool g(int a, int b)
    {
        if (a>=0 && a<=2 && b>=0 && b<=2) return 1;
        return 0;
    }
    
    void read()
    {
        t[0]=op[0];
        for(int i=1;i<=8;i++) {scanf("%s",op); t[i]=op[0];}
        for(int i=0;i<=9;i++) input[i]=t[i];
    }
    
    int H(int val)
    {
        char tmp[10];
        for(int i=0;i<=9;i++) tmp[i]=t[i];
        getstr(val); int res=0;
    
        for(int i=0;i<9;i++)
        {
            if(t[i]=='x') continue;
    
            int num=t[i]-'0'-1;
            int a=i/3,b=i%3;
            int x=num/3,y=num%3;
            res=res+abs(a-x)+abs(b-y);
        }
    
        for(int i=0;i<=9;i++) t[i]=tmp[i];
        return res;
    }
    
    void dfs(int pos,int s,int deep,int pre_dir)
    {
        if(s==0)
        {
            length=deep;
            flag=1;return;
        }
        if(deep+H(s)>limit) return;
    
        char u[10]; getstr(s);
        for(int i=0;i<=9;i++) u[i]=t[i];
    
        int a=pos/3,b=pos%3;
        for(int i=0;i<4;i++)
        {
            if(pre_dir==0&&i==1) continue;
            if(pre_dir==1&&i==0) continue;
            if(pre_dir==2&&i==3) continue;
            if(pre_dir==3&&i==2) continue;
    
            int x=a+dir[i][0],y=b+dir[i][1];
            if(!g(x,y)) continue;
    
            int new_pos=3*x+y;
    
            swap(u[pos],u[new_pos]); for(int i=0;i<=9;i++) t[i]=u[i];
            int new_s=getnum(); path[deep]=i;
            dfs(new_pos,new_s,deep+1,i); if(flag) return;
            swap(u[pos],u[new_pos]);
        }
    
    }
    
    int main()
    {
        c[0]=1; for(int i=1;i<=8;i++) c[i]=c[i-1]*i;
        while(~scanf("%s",op))
        {
            read();
    
            int sum=0;
            for(int i=0;t[i];i++)
            {
                if(t[i]=='x') continue;
                for(int j=0;j<i;j++)
                {
                    if(t[j]=='x') continue;
                    if(t[i]<t[j]) sum++;
                }
            }
    
            if(sum%2==1) { printf("unsolvable
    "); continue; }
    
            for(int i=0;i<9;i++)
            {
                if(input[i]!='x') continue;
                limit=H(getnum()); flag=0;
                while(!flag)
                {
                    for(int i=0;i<=9;i++) t[i]=input[i];
    
                    int ggg=getnum();
                    dfs(i,ggg,0,-1);
    
                    if(flag) break;
                    limit++;
                }
    
                for(int i=0;i<limit;i++) printf("%c",d[path[i]]);
                printf("
    ");
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Html学习_简易个人网页制作
    Html学习_style属性应用
    xode9.0的xcodebuild -exportArchive -archivePath 导出ipa包报错Error Domain=IDEProvisioningErrorDomain Code=3
    xcode8,Application Loader上传ipa包iTunes connect不显示构建版本问题
    git统计代码总行数
    Gerri review 代码管理规范
    ! [remote rejected] HEAD -> refs/for/ToonV3.0 (missing Change-Id in commit message footer)
    git分支管理和冲突
    Gerrit出现 Submitted, Merge Pending 和 Merge Conflict
    Git命令大全
  • 原文地址:https://www.cnblogs.com/mhpp/p/6603807.html
Copyright © 2011-2022 走看看