首页 > 搜索 > BFS搜索 > HDU 3095-Eleven puzzle-BFS-[解题报告]HOJ
2014
03-02

HDU 3095-Eleven puzzle-BFS-[解题报告]HOJ

Eleven puzzle

问题描述 :

Partychen invents a new game named “Eleven Puzzle” .Just like the classic game “Eight Puzzle”,but there some difference between them:The shape of the board is different and there are two empty tiles.
A tree game

The tile in black means it’s empty

Each step you can move only one tile.
Here comes the problem.How many steps at least it required to done the game.

输入:

The first line of input contains one integer specifying the number of test cases to follow.
Every case contains five lines to describe the initial status of the board. 0 means empty.

It’s confirmed that the board is legal.

输出:

The first line of input contains one integer specifying the number of test cases to follow.
Every case contains five lines to describe the initial status of the board. 0 means empty.

It’s confirmed that the board is legal.

样例输入:

3
2
1 0 3
4 5 6 7 8
9 0 11
10
0
1 2 3
4 5 6 7 8
9 10 11
0
0
11 10 9
8 7 6 5 4
3 2 1
0

样例输出:

2
0
No solution!

题目

八数码变成了11数码,方法不变,注意如果步数已经>20,就可以直接退出了;

用的双广,map标记,看来我其实还不会双广,跑了4187Ms,差点没过呀.

看着讨论区,有个人用双广200+ms过的,太猛了…..

居然还有0ms的 …Orz…

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
#include<stdlib.h>
#include<queue>
#include<map>
#include<sstream>
using namespace std;

struct node
{
    int a[15];
    string s;
    int t;
    int z1,z2;//两个0的位置
};
queue<node>sq,eq;
map<string,int>sm,em;
int tCase,a[15],ans;
const string se="012345678910110";
int d[13][5]={{2,-1},{2,5,-1},{0,1,3,6,-1},{2,7,-1},{5,-1},{1,4,6,9,-1},{2,5,7,10,-1},{3,6,8,11,-1},{7,-1},{5,10,-1},{6,9,11,12,-1},{7,10,-1},{10,-1}};//方向
inline string change(int a[])
{
    string s;
    for(int i=0;i<=12;i++)
    {
        stringstream ss;
        string str;
        ss<<a[i];
        ss>>str;
        s+=str;
    }
    return s;
}


inline void bfs_ex(queue<node>&q,map<string,int>&s,map<string,int>&e,node v)
{
    int a1=v.z1,a2=v.z2;
    for(int i=0;d[a1][i]!=-1&&ans==-1;i++)
    {
        node w=v;
        swap(w.a[a1],w.a[d[a1][i]]);
        w.z1=d[a1][i];
        w.z2=a2;
        w.t=v.t+1;
        if(w.t>20)
        {
            ans=-2;break;
        }
        w.s=change(w.a);
        if(e[w.s]!=0)
        {
            ans=w.t+e[w.s]-2;
            if(ans>20) ans=-2;
            break;
        }
        else if(s[w.s]==0)
        {
            s[w.s]=w.t;
            q.push(w);
        }
    }
    for(int i=0;d[a2][i]!=-1&&ans==-1;i++)
    {
        node w=v;
        swap(w.a[a2],w.a[d[a2][i]]);
        w.z2=d[a2][i];
        w.z1=a1;
        w.t=v.t+1;
        if(w.t>20)
        {
            ans=-2;break;
        }
        w.s=change(w.a);
        if(e[w.s]!=0)
        {
            ans=w.t+e[w.s]-2;
            if(ans>20) ans=-2;
            break;
        }
        else if(s[w.s]==0)
        {
            s[w.s]=w.t;
            q.push(w);
        }
    }
}
inline void bfs()
{
    while(!sq.empty()&&!eq.empty()&&ans==-1)
    {
        node vn,ve;
        vn=sq.front();
        ve=eq.front();
        int t=vn.t+ve.t-2;
        if(t>20)
        {
            ans=-2;
            break;
        }
        else if(sq.size()<=eq.size())
        {
            sq.pop();
            if(em[vn.s]!=0)
            {
                ans=em[vn.s]+vn.t-2;
                if(ans>20) ans=-2;
                break;
            }
            else if(t==20)
            {
                ans=-2;break;
            }
            else if(t<20)
            {
                bfs_ex(sq,sm,em,vn);
            }
        }
        else if(sq.size()>eq.size())
        {
            eq.pop();
            if(sm[ve.s]!=0)
            {
                ans=sm[ve.s]+ve.t-2;
                if(ans>20) ans=-2;
                break;
            }
            else if(t==20)
            {
                ans=-2;break;
            }
            else if(t<20)
            {
                bfs_ex(eq,em,sm,ve);
            }
        }
    }
}


int main()
{
    scanf("%d",&tCase);
    for(int j=1;j<=tCase;j++)
    {
        sm.clear();
        em.clear();
        while(!sq.empty()) sq.pop();
        while(!eq.empty()) eq.pop();
        node v;
        bool f=0;
        for(int i=0;i<=12;i++)
        {
            scanf("%d",&v.a[i]);
            if(v.a[i]==0&&f==0)
            {
                v.z1=i;f=1;
            }
            else if(v.a[i]==0&&f==1)
            {
                v.z2=i;
            }
        }
        string s=change(v.a);
        v.s=s;
        v.t=1;
        sm[v.s]=1;
        sq.push(v);

        node e;
        e.t=1;
        e.s=se;
        em[se]=1;
        for(int i=0;i<=12;i++) e.a[i]=i;
        e.a[12]=0;
        e.z1=0;
        e.z2=12;
        eq.push(e);

        ans=-1;
        bfs();
        if(ans>=0)
        printf("%d\n",ans);
        else
        printf("No solution!\n");
    }
    return 0;
}

参考:http://blog.csdn.net/zhuhuangjian/article/details/9668689


,
  1. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  2. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  3. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。