zoukankan      html  css  js  c++  java
  • Codevs 1225 八数码难题

    1225 八数码难题
    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 钻石 Diamond
    题目描述 Description
    Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.
    问题描述
    在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。
    输入描述 Input Description
    输入初试状态,一行九个数字,空格用0表示
    输出描述 Output Description
    只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据)
    样例输入 Sample Input
    283104765
    样例输出 Sample Output
    4
    数据范围及提示 Data Size & Hint
    详见试题
    分类标签 Tags
    启发式搜索 广度优先搜索 深度优先搜索 迭代搜索 搜索

    /*赶脚这题可以状态打表o(1)查询*/
    /*
    BFS 53ms.
    */
    #include<iostream>
    #include<cstdio>
    #define N 500000
    using namespace std;
    struct node
    {
        int mp[4][4];
    }a[N];
    int g[4][4]={{0,0,0,0},{0,1,2,3},{0,8,0,4},{0,7,6,5}};
    int xx[4] = {0,0,1,-1};
    int yy[4] = {1,-1,0,0};
    int hash[3733800];
    int step[N];
    int h,t=1,flag;
    int check()
    {
        for (int i=1;i<=3;i++)
            for (int j=1;j<=3;j++)
            if (a[t].mp[i][j] != g[i][j])
                return 0;
        return 1;
    }
    int Hash()
    {
        int s = 0,k = 1;
        for (int i=1;i<=3;i++)
            for (int j=1;j<=3;j++)
                s += a[t].mp[i][j]*k,k*=7;
        s %= 3733799;
        if (!hash[s])   {hash[s] = 1;return 1;}
        return 0;
    }
    int pd(int x,int y)
    {
        if (x && x<=3 && y && y<=3) return 1;
        return 0;
    }
    void move(int x,int y)
    {
        for (int i=0;i<4;i++)
        {
            int p = x+xx[i], q = y+yy[i];
            if (pd(p,q))
            {
                for (int j=1;j<=3;j++)
                    for (int k=1;k<=3;k++)
                    a[t].mp[j][k] = a[h].mp[j][k];
                swap(a[t].mp[x][y],a[t].mp[p][q]);
                step[t] = step[h] +1;
                if (check())    {cout<<step[t]; flag = 1;return;}
                if (Hash()) t++;
            }
        }
    }
    void search()
    {
        while (h<t)
        {
            for (int i=1;i<=3;i++)
                for (int j=1;j<=3;j++)
                {
                    if (a[h].mp[i][j] == 0)
                        move(i,j);
                    if (flag) return;
                }
            h++;
        }
    }
    int main()
    {
        string str;
        cin>>str;
        for (int i=1;i<=3;i++)
            for (int j=1;j<=3;j++)
            a[0].mp[i][j] = str[(i-1)*3+j-1]-'0';
        search();
    }
    /*
    双向Bfs+map 9ms.
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<queue>
    using namespace std;
    string s=" 123804765",s1;
    queue<string>q;
    map<string,int>f,t;
    int bfs(){
        q.push(s1);q.push(s);f[s1]=1;f[s]=2;t[s1]=t[s]=0;
        while(!q.empty()){
            string x=q.front();q.pop();
            int p=x.find('0');
            if(p>=4){
                string k=x;int p1=p-3;//向上扩展.
                swap(k[p1],k[p]);
                if(!f[k]) f[k]=f[x],t[k]=t[x]+1,q.push(k);
                else if(f[k]!=f[x]) return t[k]+t[x]+1;
            }
            if(p<=6){
                string k=x;int p1=p+3;//向下扩展. 
                swap(k[p1],k[p]);
                if(!f[k]) f[k]=f[x],t[k]=t[x]+1,q.push(k);
                else if(f[k]!=f[x]) return t[k]+t[x]+1;
            }
            if(p!=1&&p!=4&&p!=7){
                string k=x;int p1=p-1;//向左扩展.
                swap(k[p1],k[p]);
                if(!f[k]) f[k]=f[x],t[k]=t[x]+1,q.push(k);
                else if(f[k]!=f[x]) return t[k]+t[x]+1;
            }
            if(p!=3&&p!=6&&p!=9){
                string k=x;int p1=p+1;//向右扩展.
                swap(k[p1],k[p]);
                if(!f[k]) f[k]=f[x],t[k]=t[x]+1,q.push(k);
                else if(f[k]!=f[x]) return t[k]+t[x]+1;
            }
        }
        return -1;
    }
    int main()
    {
        cin>>s1;s1=" "+s1;
        printf("%d",bfs());
        return 0;
    }
  • 相关阅读:
    附加作业
    个人总结
    wordcount
    vs2013安装及测试
    结对作业电梯调度问题
    阅读下面程序,请回答如下问题:
    补作业:随机生成二元四则运算
    软件工程的认识
    结对编程完结
    结对编程加团队编程
  • 原文地址:https://www.cnblogs.com/nancheng58/p/6070794.html
Copyright © 2011-2022 走看看