搜索 ( 八数码问题详解:BFS,A*,IDA* )——Eight ( POJ 1077 )

八数码IDA*实现 代码注释清晰,逻辑连贯,有助于理解IDA*和八数码问题 立即下载

参考文章:
1. DBFS,A*,IDA*,转自链接 (侵删)
2. 一份非常详细的实验报告:http://blog.csdn.net/ray58750034/article/details/599897#comments

  • 题目链接:
    http://poj.org/problem?id=2449

  • 分析:
    又称为九宫格问题,给出一个状态的九宫格,判断是否有解,如果有则输出解。

  • 八数码问题有无解的结论:

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

    2. 若两个状态的逆序数奇偶性相同,则可相互到达,否则不可相互到达。由于原始状态的逆序为0(偶数),则逆序为偶数的状态有解。

      证明:当左右移动空格时,逆序数不变。当上下移动空格时,相当于将一个数字向前(或向后)移动两格,跳过 的这两个数字要么都比它大(小),逆序可能±2;要么一个较大一个较小,逆序不变。所以可得结论:只要是相互可达的两个状态,它们的逆序奇偶性相同。详细的证明请参考:
      http://blog.csdn.net/tiaotiaoyly/article/details/2008233

  • 康拓展开(将每一种排列都康拓展开,得到与其唯一对应的一个数,是对于全排列的一种很好的哈希函数):

    公式: X=an×(n−1)!+an−1×(n−2)!+...+ai×(i−1)!+...+a2×1!+a1×0! 其中,ai表示的是第i-1个数(从0开始数),在这个排列中的大小位置(从0开始计算)。

    代码如下:

int fac[] = { 1,1,2,6,24,120,720,5040,40320 };//打表表示前几个数的阶乘。
int Cantor( int s[])//参数为排列。
{
    int sum = 0;
    for(int i = 0;i<9;i++)
    {
        int cnt = 0;
        for(int j=i+1;j<9;j++)
        {
            if( s[i] > s[j] ) cnt++;
        }
        sum+=(cnt*fac[9-i-1]);
    }
    return sum+1;
}
  • 通过康拓逆展开生成与其对应的全排列:

    如果已知一个排列的康拓展开数,那么可以使用辗转相除的方法得到这个排列中的每一个 ai :
    这里写图片描述

    如果排列中的元素为A,B,C,D,那么a4 = 3表示剩余元素为4的时候它排在第三位(均从第0位开始计数),即D;a3 = 1表示剩余元素为3的时候它排再第一位,即B;a2 = 0表示剩余元素为2的时候它排在第0位,即A,最后一个a1就是C了。

  • BFS + 康拓展开 解决八数码问题:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>

// 正向广度搜索

using namespace std;

const int maxn = 1000000;
int fac[] = { 1,1,2,6,24,120,720,5040,40320 };
bool flag [maxn];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
string cmd = "udlr";
int aim=46234;

int Cantor( int s[])
{
    int sum = 0;
    for(int i = 0;i<9;i++)
    {
        int cnt = 0;
        for(int j=i+1;j<9;j++)
        {
            if( s[i] > s[j] ) cnt++;
        }
        sum+=(cnt*fac[9-i-1]);
    }
    return sum+1;
}

struct node
{
    int s[9];
    int loc;
    int status;
    string path;
};

node origin;
string path;


int BFS()
{
    queue <node> q;
    while ( !q.empty() )
        q.pop();

    node cur,temp;

    q.push(origin);

    int x,y;
    while( !q.empty() )
    {
        cur = q.front();
        q.pop();

        if( cur.status == aim )
        {
            path = cur.path;
            return 1;
        }
        x = cur.loc/3;
        y = cur.loc%3;
        for(int i=0;i<4;i++)
        {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];

            if( tx<0 || ty <0 || tx>2 || ty>2 ) continue;

            temp = cur;
            temp.loc = tx*3 + ty;

            temp.s[cur.loc] = temp.s[temp.loc];
            temp.s[temp.loc] = 0;
            temp.status = Cantor(temp.s);

            if( !flag[temp.status] )
            {
                flag[temp.status] = 1;
                temp.path = temp.path + cmd[i];
                if( temp.status == aim )
                {
                    path = temp.path;
                    return 1;
                }
                q.push(temp);
            }
        }
    }
    return 0;
}



int main()
{
    char ch;
    while(cin>>ch)
    {
        if(ch == 'x')
        {
            origin.s[0] = 0;
            origin.loc = 0;
        }
        else origin.s[0] = ch - '0';
        for(int i=1;i<9;i++)
        {
            cin >> ch;
            if( ch == 'x' )
            {
            origin.s[i] = 0;
            origin.loc = i;
            }
            else origin.s[i] = ch - '0';
        }
        origin.status = Cantor( origin.s );
        memset( flag , 0 ,sizeof(flag) );

        if(BFS())
            cout<<path<<endl;
        else
             printf("unsolvable\n");
    }
    return 0;
}
  • 双向广搜 :

    由于目标态和初始态都已知,可以采用从两个状态同时开始搜,当搜到同一个节点时,搜索结束,将两边的步数加起来输出。这道题里在每个节点,用一个值标记,此节点是由哪个状态访问的,故只需用一个队列交替扩展。双向广搜会少扩展许多节点,时间效率得到大幅提升。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define FUCK puts("fuck!")
#define STOP system("pause")
#define MAXN 388211
using namespace std;
struct state{
    int sta,pos,step;
    int visit;
}st[MAXN],source,target;
int temp[10],tar[10],sou[10];
int d[9][4]={{0,4,0,2},{0,5,1,3},{0,6,2,0},{1,7,0,5},{2,8,4,6},{3,9,5,0},{4,0,0,8},{5,0,7,9},{6,0,8,0}};
int num;
int convert(int a[],state &b)
{
    b.sta=0;
    for(int i = 0; i < 9; i ++) 
    {   
        if(a[i]!=0)
            b.sta |=((a[i]-1)<<(24-i*3));
        else
        {
            b.pos = i;
            b.sta |=(a[i]<<(24-i*3));
        }
    }
    return 1;
}
state exchange(state a,int pos)
{
    int temp = 7<<((9-pos)*3);
    state s;
    s.sta = a.sta;
    temp = temp & a.sta;
    temp = ((temp>>((9-pos)*3))<<((9-a.pos-1)*3));
    s.sta |= temp;
    s.sta &= ~(7<<((9-pos)*3));
    s.pos = pos-1;
    return s;
}
int search(state a)
{
    int index = a.sta%MAXN;
    bool flag = true;
    while(flag)
    {
        if(!st[index].sta)
        {
            st[index].sta = a.sta;
            st[index].pos = a.pos;
            flag = false;
        }
        else if(!(st[index].sta^a.sta)&&!(st[index].pos^a.pos))
            flag = false;
        else
            index = (index+1)%MAXN;
    }
    return index;
}
int main()
{
    freopen("in.txt","r",stdin);
    clock_t start,end;
    for(int j=0;j<8;j++)temp[j] = j+1;
    temp[8]=0;
    convert(temp,target);
    while(1)
    {
        int i = 0;
        memset(st,0,sizeof(st));
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch<='9'&&ch>='0')
                sou[i++] = ch - '0';
            else if(ch=='x')
                sou[i++] =0;
        }
        convert(sou,source);
        start = clock();
        i = search(source);
        queue<int>q;
        q.push(i);
        i = search(target);
        st[i].visit = 1;
        st[i].step = 1;
        q.push(i);
        if(!(source.sta^target.sta)&&!(source.pos^target.pos))
        {
            printf("0\n");
            while(!q.empty())q.pop();
                continue;
        }
        int index;
        int count = 0;
        bool isSolve = false;
        while(!q.empty()&&!isSolve)
        {
            count ++;
            index = q.front();
            for(int j = 0; j < 4; j ++)
            {
                if(d[st[index].pos][j])
                {
                    int flag = search(exchange(st[index],d[st[index].pos][j]));
                    if(!st[flag].step)
                    {
                        st[flag].step = st[index].step + 1; 
                        st[flag].visit = st[index].visit;
                        q.push(flag);
                    }   
                    else
                    {
                        if(st[flag].visit^st[index].visit)
                        {
                            isSolve = true;
                            printf("%d\n",st[index].step+st[flag].step);
                        }
                    }
                }
            }
            q.pop();
        }
        while(!q.empty())q.pop();
        end = clock();
        printf("Time:%dms\nstate number:%d\n",end-start,count);
    }
    system("pause");
        return 0;
}
  • A*搜索:

    有两种可行的启发函数: 不在位数(difference)与曼哈顿距离(manhattan)

    在每一个节点中,加一个int型变量存储此节点的估价。此时用优先队列存储节点。但是若启发函数效率不高,减少的扩展节点的时间可能还不足以抵过小顶堆调整的时间,结果就是时间效率可能比普通的bfs还差。

    基于不在位函数的A*搜索:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define FUCK puts("fuck!")
#define STOP system("pause")
#define MAXN 388211
using namespace std;
struct state{
    int sta,pos,step;
    int f;
}st[MAXN],source,target;
int temp[10],tar[10],sou[10];
int d[9][4]={{0,4,0,2},{0,5,1,3},{0,6,2,0},{1,7,0,5},{2,8,4,6},{3,9,5,0},{4,0,0,8},{5,0,7,9},{6,0,8,0}};
int num;
int h(state a)
{
    int temp = target.sta;
    int cnt=0;
    for(int i = 0;i < 9; i ++)
    {
        if(a.pos==target.pos)
        {
            if(!(((temp>>(3*i))&7)^((a.sta>>(3*i))&7)))
                cnt++;
        }
        else
        {
            if((!(((temp>>(3*i))&7)^((a.sta>>(3*i))&7)))&&((a.sta>>(3*i))&7))
                cnt++;
        }
    }
    return 9-cnt;
}
struct cmp
{
    bool operator () (int u, int v)
    {
        return st[u].f > st[v].f;       
    }
};
int convert(int a[],state &b)
{
    b.sta=0;
    for(int i = 0; i < 9; i ++) 
    {   
        if(a[i]!=0)
            b.sta |=((a[i]-1)<<(24-i*3));
        else
        {
            b.pos = i;
            b.sta |=(a[i]<<(24-i*3));
        }
    }
    return 1;
}
state exchange(state a,int pos)
{
    int temp = 7<<((9-pos)*3);
    state s;
    s.sta = a.sta;
    temp = temp & a.sta;
    temp = ((temp>>((9-pos)*3))<<((9-a.pos-1)*3));
    s.sta |= temp;
    s.sta &= ~(7<<((9-pos)*3));
    s.pos = pos-1;
    return s;
}
int search(state a)
{
    int index = a.sta%MAXN;
    bool flag = true;
    while(flag)
    {
        if(!st[index].sta)
        {
            st[index].sta = a.sta;
            st[index].pos = a.pos;
            flag = false;
        }
        else if(!(st[index].sta^a.sta)&&!(st[index].pos^a.pos))
            flag = false;
        else
            index = (index+1)%MAXN;
    }
    return index;
}
int main()
{
    freopen("in.txt","r",stdin);
    clock_t start,end;
    for(int j=0;j<8;j++)temp[j] = j+1;
    temp[8]=0;
    convert(temp,target);
    while(1)
    {
        int i = 0;
        memset(st,0,sizeof(st));
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch<='9'&&ch>='0')
                sou[i++] = ch - '0';
            else if(ch=='x')
                sou[i++] =0;
        }
        convert(sou,source);
        start = clock();
        i = search(source);
        st[i].f = h(st[i]);
        priority_queue<int,vector<int>,cmp>q;
        q.push(i);
        int index;
        int count = 0;
        while(!q.empty())
        {               
            count++;
            index = q.top();
            q.pop();                //!!!!
            if(!(st[index].sta^target.sta)&&st[index].pos == target.pos)
            {
                printf("%d\n",st[index].step);
                break;
            }
            for(int j = 0; j < 4; j ++)
            {
                if(d[st[index].pos][j])
                {
                    int flag = search(exchange(st[index],d[st[index].pos][j]));
                    if(!st[flag].step||st[flag].step > st[index].step + 1)
                    {
                        st[flag].step = st[index].step + 1; 
                        st[flag].f = st[flag].step + h(st[flag]);
                        q.push(flag);
                    }   
                }
            }
        }
        while(!q.empty())q.pop();
        end = clock();
        printf("Time:%dms\nstate number:%d\n",end-start,count);
    }
    system("pause");
        return 0;
}

基于 manhattan距离函数的A*搜索:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define FUCK puts("fuck!")
#define STOP system("pause")
#define MAXN 388211
using namespace std;
struct state{
    int sta,pos,step;
    int f;
}st[MAXN],source,target;
int temp[10],tar[10],sou[10];
int d[9][4]={{0,4,0,2},{0,5,1,3},{0,6,2,0},{1,7,0,5},{2,8,4,6},{3,9,5,0},{4,0,0,8},{5,0,7,9},{6,0,8,0}};
int num;
int manhattan[10][10] = //第i个数及其所处不同位置的Manhattan路径长度
{
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1, 0, 1, 2, 1, 2, 3, 2, 3, 4},
{-1, 1, 0, 1, 2, 1, 2, 3, 2, 3},
{-1, 2, 1, 0, 3, 2, 1, 4, 3, 2},
{-1, 1, 2, 3, 0, 1, 2, 1, 2, 3},
{-1, 2, 1, 2, 1, 0, 1, 2, 1, 2},
{-1, 3, 2, 1, 2, 1, 0, 3, 2, 1},
{-1, 2, 3, 4, 1, 2, 3, 0, 1, 2},
{-1, 3, 2, 3, 2, 1, 2, 1, 0, 1},
{-1, 4, 3, 2, 3, 2, 1, 2, 1, 0}

};
int h(state a)
{
    int cnt=0;
    for(int i = 0;i < 9; i ++)
    {
        if(a.pos != i)
            cnt += manhattan[((a.sta>>(3*(8-i)))&7)+1][i+1];
    }
    return cnt;
}
class cmp
{
      public:
    bool operator () (int u, int v)
    {
        return st[u].f > st[v].f;       
    }
};
int convert(int a[],state &b)
{
    b.sta=0;
    for(int i = 0; i < 9; i ++) 
    {   
        if(a[i]!=0)
            b.sta |=((a[i]-1)<<(24-i*3));
        else
        {
            b.pos = i;
            b.sta |=(a[i]<<(24-i*3));
        }
    }
    return 1;
}
state exchange(state a,int pos)
{
    int temp = 7<<((9-pos)*3);
    state s;
    s.sta = a.sta;
    temp = temp & a.sta;
    temp = ((temp>>((9-pos)*3))<<((9-a.pos-1)*3));
    s.sta |= temp;
    s.sta &= ~(7<<((9-pos)*3));
    s.pos = pos-1;
    return s;
}
int search(state a)
{
    int index = a.sta%MAXN;
    bool flag = true;
    while(flag)
    {
        if(!st[index].sta)
        {
            st[index].sta = a.sta;
            st[index].pos = a.pos;
            flag = false;
        }
        else if(!(st[index].sta^a.sta)&&!(st[index].pos^a.pos))
            flag = false;
        else
            index = (index+1)%MAXN;
    }
    return index;
}
int main()
{
    freopen("in.txt","r",stdin);
    clock_t start,end;
    for(int j=0;j<8;j++)temp[j] = j+1;
    temp[8]=0;
    convert(temp,target);
    while(1)
    {
        int i = 0;
        memset(st,0,sizeof(st));
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch<='9'&&ch>='0')
                sou[i++] = ch - '0';
            else if(ch=='x')
                sou[i++] =0;
        }
        convert(sou,source);
        start = clock();
        i = search(source);
        st[i].f = h(st[i]);
        priority_queue<int,vector<int>,cmp>q;
        q.push(i);
        int index;
        int count = 0;
        while(!q.empty())
        {               
            count++;
            index = q.top();
            q.pop();                //!!!!
            if(!(st[index].sta^target.sta)&&st[index].pos == target.pos)
            {
                printf("%d\n",st[index].step);
                break;
            }
            for(int j = 0; j < 4; j ++)
            {
                if(d[st[index].pos][j])
                {
                    int flag = search(exchange(st[index],d[st[index].pos][j]));
                    if(!st[flag].step||st[flag].step > st[index].step + 1)
                    {
                        st[flag].step = st[index].step + 1; 
                        st[flag].f = st[flag].step + h(st[flag]);
                        q.push(flag);
                    }   
                }
            }
        }
        while(!q.empty())q.pop();
        end = clock();
        printf("Time:%dms\nstate number:%d\n",end-start,count);
    }
    system("pause");
        return 0;
}
  • IDA*搜索:

    由于普通的深搜在此问题上,要么搜索到错误的结果,要么需要搜索所有的状态,才能确定是否是最优,故在这里使用IDA*。IDA*是一种迭代加深的深度搜索,若在此深度下没有搜到目标点,则将深度加一重新搜索。无须状态判重,无需估价排序,用不到哈希表,堆上也不必应用,空间需求变的超级少,实现也最简单。在深搜过程中,根据启发函数做剪枝,可以使效率达到很高。另外在求路径的时候,IDA*也是最方便的。

    基于不在为函数的IDA*搜索:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define FUCK puts("fuck!")
#define STOP system("pause")
#define MAXN 388211
using namespace std;
struct state{
    int sta,pos;
}source,target;
int temp[10],tar[10],sou[10];
int pathLimit;
int cnt;
int d[9][4]={{0,4,0,2},{0,5,1,3},{0,6,2,0},{1,7,0,5},{2,8,4,6},{3,9,5,0},{4,0,0,8},{5,0,7,9},{6,0,8,0}};
int h(state a)
{
    int temp = target.sta;
    int cnt=0;
    for(int i = 0;i < 9; i ++)
    {
        if(a.pos==target.pos)
        {
            if(!(((temp>>(3*i))&7)^((a.sta>>(3*i))&7)))
                cnt++;
        }
        else
        {
            if(!(((temp>>(3*i))&7)^((a.sta>>(3*i))&7))&&((a.sta>>(3*i))&7))
                cnt++;
        }
    }
    return 9-cnt;
}
int convert(int a[],state &b)
{
    b.sta=0;
    for(int i = 0; i < 9; i ++) 
    {   
        if(a[i]!=0)
            b.sta |=((a[i]-1)<<(24-i*3));
        else
        {
            b.pos = i;
            b.sta |=(a[i]<<(24-i*3));
        }
    }
    return 1;
}
state exchange(state a,int pos)
{
    int temp = 7<<((9-pos)*3);
    state s;
    s.sta = a.sta;
    temp = temp & a.sta;
    temp = ((temp>>((9-pos)*3))<<((9-a.pos-1)*3));
    s.sta |= temp;
    s.sta &= ~(7<<((9-pos)*3));
    s.pos = pos-1;
    return s;
}
bool IDAStar(state &a,int depth,int diff,int prepos)
{
    cnt++;
    if(!(a.sta^target.sta)&&a.pos == target.pos)
    {
        printf("%d\n",depth);
        return true;
    }
    if(depth >= pathLimit) return false;
    if( depth + diff > pathLimit ) return false;  
    for(int j = 0; j < 4; j ++)
    {
        if(d[a.pos][j] == prepos+1) continue;
        if(d[a.pos][j])
        {
            state next = exchange(a,d[a.pos][j]);
            if(IDAStar(next,depth+1, h(next),a.pos))
                return true;
        }
    }
    return false;
}
int main()
{
    freopen("in.txt","r",stdin);
    clock_t start,end;
    int diff = 0;
    for(int j=0;j<8;j++)temp[j] = j+1;
    temp[8]=0;
    convert(temp,target);
    while(1)
    {
        int i = 0;
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch<='9'&&ch>='0')
                sou[i++] = ch - '0';
            else if(ch=='x')
                sou[i++] =0;
        }
        start = clock();
        cnt = 0;
        convert(sou,source);
        pathLimit = h(source);
        diff = pathLimit;
        while(!IDAStar(source,0,diff,-1))pathLimit++; 
        end = clock();
        printf("Time:%dms\nstate number:%d\n",end-start,cnt);
    }
    system("pause");
        return 0;
}

基于manhattan距离函数的搜索:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define FUCK puts("fuck!")
#define STOP system("pause")
#define MAXN 388211
using namespace std;
struct state{
    int sta,pos;
}source,target;
int temp[10],tar[10],sou[10];
int pathLimit;
int d[9][4]={{0,4,0,2},{0,5,1,3},{0,6,2,0},{1,7,0,5},{2,8,4,6},{3,9,5,0},{4,0,0,8},{5,0,7,9},{6,0,8,0}};
int manhattan[10][10] = //第i个数及其所处不同位置的Manhattan路径长度
{
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1, 0, 1, 2, 1, 2, 3, 2, 3, 4},
{-1, 1, 0, 1, 2, 1, 2, 3, 2, 3},
{-1, 2, 1, 0, 3, 2, 1, 4, 3, 2},
{-1, 1, 2, 3, 0, 1, 2, 1, 2, 3},
{-1, 2, 1, 2, 1, 0, 1, 2, 1, 2},
{-1, 3, 2, 1, 2, 1, 0, 3, 2, 1},
{-1, 2, 3, 4, 1, 2, 3, 0, 1, 2},
{-1, 3, 2, 3, 2, 1, 2, 1, 0, 1},
{-1, 4, 3, 2, 3, 2, 1, 2, 1, 0}

};
int h(state a)
{
    int cnt=0;
    for(int i = 0;i < 9; i ++)
    {
        if(a.pos != i)
            cnt += manhattan[((a.sta>>(3*(8-i)))&7)+1][i+1];
    }
    return cnt;
}
int convert(int a[],state &b)
{
    b.sta=0;
    for(int i = 0; i < 9; i ++) 
    {   
        if(a[i]!=0)
            b.sta |=((a[i]-1)<<(24-i*3));
        else
        {
            b.pos = i;
            b.sta |=(a[i]<<(24-i*3));
        }
    }
    return 1;
}
state exchange(state a,int pos)
{
    int temp = 7<<((9-pos)*3);
    state s;
    s.sta = a.sta;
    temp = temp & a.sta;
    temp = ((temp>>((9-pos)*3))<<((9-a.pos-1)*3));
    s.sta |= temp;
    s.sta &= ~(7<<((9-pos)*3));
    s.pos = pos-1;
    return s;
}
bool IDAStar(state &a,int depth,int diff,int prepos)
{
    if(!(a.sta^target.sta)&&a.pos == target.pos)
    {
        printf("%d\n",depth);
        return true;
    }
    if(depth > pathLimit) return false;
    if( depth + diff > pathLimit ) return false;  
    for(int j = 0; j < 4; j ++)
    {
        if(d[a.pos][j] == prepos+1) continue;
        if(d[a.pos][j])
        {
            state next = exchange(a,d[a.pos][j]);
            if(IDAStar(next,depth+1, h(next),a.pos))
                return true;
        }
    }
    return false;
}
int main()
{
    freopen("in.txt","r",stdin);
    clock_t start,end;
    int diff = 0;
    for(int j=0;j<8;j++)temp[j] = j+1;
    temp[8]=0;
    convert(temp,target);
    while(1)
    {
        int i = 0;
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch<='9'&&ch>='0')
                sou[i++] = ch - '0';
            else if(ch=='x')
                sou[i++] =0;
        }
        start = clock();
        convert(sou,source);
        pathLimit = h(source);
        diff = pathLimit;
        while(!IDAStar(source,0,diff,-1))pathLimit++; 
        end = clock();
        printf("Time:%dms\ndepthlimit:%d\n",end-start,pathLimit);
    }
    system("pause");
        return 0;
}
  • 总结:
    从效率上来看 IDA* > A* > DBFS > BFS+Canton >BFS ,其中,manhattan估价函数优余difference估价函数。
eNSP网络构建—建立小型局域网 企业私有网络构建运维 实战案例——使用模拟器构建局域网络 案例分析 1.架构分析 (1)需求分析 本实验的目的在于建立小型局域网。由于公司由不同部门组成,并分布在不同地点,因此需要划分不同网络实现互联互通。设计以下网络:两个部门各使用一台交换机连接,然后连接到总交换机,再通过路由器与外网以及其他部门网络相连。为了控制网络上的广播风暴,增加网络的安全性,在交换机上需要设置VLAN,在路由器与交换机之... 阅读详情

相关推荐

从碰撞电离到雪崩击穿:Sentaurus仿真中的模型选择与验证艺术

本文深入探讨了Sentaurus TCAD中雪崩击穿模型的选取与验证方法,特别关注平行平面结的仿真实践。通过对比Chynoweth、van Overstraeten等主流模型的适用场景与精度差异,结合具体案例展示了从几何建模到边界条件设定的完整验证流程,并针对非理想因素提供了有效的补偿技术,助力工程师提升器件仿真的准确性与可靠性。

weixin_29074295的博客 602

[人工智能]八数码问题

问题描述:       有一个3×3的棋盘,其中有0~8九个数字,0表示空格,其他的数字可以和0交换位置。求由初始状态到达目标状态步数最少的解。       解决八数码问题的常用方法为图搜索法,可用广度优先、深度优先和A*算法实现,其中A*算法又因估价函数的不同而有着不同的搜索时间。程序说明:       在本程序中,用广度优先、深度优先和A*算法分别实现了八数码问题,其中A*

cbtlef的专栏 1万+

Ubuntu离线情况下安装gcc编译器

Ubuntu离线情况下安装gcc编译器1.系统版本2.安装说明3.异常处理 1.系统版本 (1)Ubuntu系统版本:20.04.1LTS; (2)gcc编译器版本:gcc-9.3.0(下载链接为https://download.csdn.net/download/qq_18350351/13762214); 当前可找到的gcc最新版本为gcc-10,但是笔者在安装gcc-10版本时发现一些依赖项无法安装,因此选取了gcc-9版本。如果是在联网情况下安装,直接使用apt相关的安装指令即可。 2.安装说明 安

GreenHand的博客 3942

人工智能实验——八数码难题

人工智能实验 八数码难题 利用了BFS算法DFS算法A*算法

weixin_51735061的博客 2万+

AutoSAR实战:手把手教你配置PNC(Partial Network Cluster)实现精准休眠与唤醒

本文详细介绍了如何在AutoSAR中配置PNC(Partial Network Cluster)实现精准休眠与唤醒,通过DaVinci Configurator工具逐步指导,结合ComM模块的协同调试,优化车载电子系统的能耗管理。文章包含实战步骤、常见错误排查及功耗优化数据,适合汽车电子开发工程师参考。

weixin_33725239的博客 478

单细胞转录组数据质控实战:如何用Seurat精准过滤低质量细胞(附完整代码)

本文详细介绍了使用Seurat进行单细胞转录组数据质控的实战方法,包括基因检出数、分子计数和线粒体基因比例等关键指标的解读与可视化技巧。通过动态阈值策略和高级质控技巧(如双细胞检测和红细胞过滤),帮助研究者精准过滤低质量细胞,提升数据分析可靠性。附完整代码示例,适合生物信息学研究人员参考实践。

ii567的博客 829

八数码问题

如下图所示,在节点1完成展开后,open表弹出最后一个节点4,节点4展开形成节点5、6,并将其加入到open表的尾部,接下来将会弹出最后一个节点6,节点6的子节点又会加入到open表的尾部,接下来继续弹出最后一个节点,这个节点则是节点6的子节点。我在代码中做了许多鲁棒性的操作,本来的想法是,将八数码问题很容易的扩展到十五数码问题,但是后来才想到,代码处理节点的方式是将其拉成一条字符串,这样以来,10以上的数字就没有办法表示了,因为他们占了两位字符,只能放弃了这个想法。但把代码中的鲁棒性操作都保留了下来。

qq_63708623的博客 5038

【传感器模块项目实战】机器人/无人驾驶常用传感器模型、选型与安装

认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!无人车&无人机导航合集本文先对机器人/无人驾驶常用传感器模型、选型与安装做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章提示:以下是本篇文章正文内容分为位置式绝对型编码器增量式编码器打比赛的时候用过欧姆龙的增量式编码器,也用过待编码器的步进电机。【防盗标记–盒子君hzj】增量式编码器正交编码输出信号,用于识别正转和反转,通过微控制器(单片机)的片内外设硬件/软件解码(1)惯性器件-机械陀螺。

qq_35635374的博客 455

c++八数码难题全家桶(A*算法、双向BFS、BFS、DFS)

本文用C++实现了八数码问题,详尽四种经典算法:A*算法、双向BFS、BFS、DFS,有被八数码问题困扰的uu们,抓紧时间,进入正文,一定会对你有所帮助!

qq_54893805的博客 5188

dify upload存在SSRF漏洞

Dify 是一个用于构建 AI 应用程序的开源平台。

nnn2188185的博客 1490

【思考模型框架】采用双钻模型:发散(桌面研究/用户访谈)→收敛(需求归类/优先级排序),输出需求规格书、追踪矩阵及产品待办清单。

设计思维双钻模型是一种以用户为中心的需求管理方法,通过"发散-收敛"的双循环确保问题定位准确、方案切实可行。第一钻石(探索→定义)聚焦"找对问题",运用用户访谈、田野调查等方法挖掘真实需求;第二钻石(开发→交付)解决"做对方案",通过原型测试、A/B测试验证可行性。该模型强调低保真验证、多团队协作和真实用户测试,避免资源浪费与伪需求开发。典型应用场景包括产品功能优化、用户体验提升等需要精准落地的需求场景。核心价值在于建立从用户痛点到解决方案的可控闭

奶爸的编程之路,也就一周冷个三天 335

八数码实验报告

八数码实验报告问题简介:        所谓八数码问题是指这样一种游戏:将分别标有数字1,2,3,…,8的八块正方形数码牌任意地放在一块3×3的数码盘上。放牌时要求不能重叠。于是,在3×3的数码盘上出现了一个空格。现在要求按照每次只能将与空格相邻的数码牌与空格交换的原则,将任意摆放的数码盘逐步摆成某种特殊的排列。如下图表示了一个具体的八数码问题求解。问题分析:        首先,八数码问题包

1万+

SI案例分享--FPC(柔性电路板)信号仿真

设计高性能的柔性PCB时,由于采用了网格地的参考面构造,因此,需要按照特定的结构进行布局,才可以保证所需目标阻抗的稳定性,该结构的配置过程称为“Hatch Configuration”,合理的参数配置将有助于得到满意的设计效果。

一只豌豆象的博客 2842

【POJ1077】Eight 八数码问题,解题报告+思路+代码

#include #include #include #define INPUT using namespace std; /** Problem : poj1077,hdu1043,经典的八数码问题。 知识点: BFS + HASH + 打表 + 父亲节点记录 境界:3 - BFS+HASH+打表 A了两天!!! 记得拿启发式,双向BFS重新写

c0de4fun的完美算(nei)法(he)教室-我们仍不知道,那天调用的驱动的名字。 5779

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

POJ 1077 Eight A*算法 IDA*算法 康拓展开

这道题是一道经典的搜索题 第一次做A*算法的题目 这道题需要知道的,第一是怎样把全排列转换为数字,第二是h函数的设计 全排列转化为数字用到的是康托展开,跟逆序数有关,这是一个比较经典的转换。转换成数字的目的就是为了状态的判重,所以A*算法的空间需求总是指数级的 h函数用的是曼哈顿距离的总和,然后f=g+h,g函数就是从初始状态到现在状态花费的步骤数了。 然后使用优先队列,进行BFS。

水 2927

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

POJ1077 Eight —— IDA*算法

主页面: #include #include #include #include #include #include #include #include #include #include #include #define ms(a,b) memset((a),(b),sizeof((a))) using namespace std; typedef lon

A man can be destroyed, but not defeated. 802

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

poj 1077 & hdu 1043 Eight ( 多种解法:预处理、bfs、dbfs、IDA*、A*)

Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9114    Accepted Submission(s): 2450 Special Judge Problem Description The

To be what you what to be! 1266

POJ 1077 Eight【八数码问题】

http://poj.org/problem?id=1077八数码问题:由1,2,3,4,5,6,7,8,x组成一个3*3的矩阵,如1 2 34 x 5 6 7 8其中x可与其上,下,左,右相邻的元素互换,现问从给出状态出发到达以下状态:1 2 3 4 5 67 8 x需要对x进行怎样的位移操作,输出x的最少位移信息,若状态不可达,输出unsolvable 分析:1.一个很容易的想...

a48634331的博客 295

POJ 1077 Eight

题目链接:http://poj.org/problem?id=1077 经典八数码问题,作为A*与IDA*的入门题来说是很不错的。 第一次学习A*与IDA*,代码参考了:http://www.cnblogs.com/liyongmou/archive/2010/07/19/1780861.html 推荐一篇介绍A*算法的文章:http://hi.baidu.com/catro/it...

aegt43853的博客 107

POJ 1077 Eight 八数码 A*搜索算法

EightTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12974 Accepted: 5788 Special JudgeDescriptionThe 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, e

魔神翼 2366

Poj 1077 Eight 八数码问题 (搜索)

文章还没有写完,明天搞清楚IDA*算法再继续写。 搜索会用的方法太少了,于是最近被搜索虐爆了。。。 认识启发式搜索A*和迭代加深的A*算法:IDA*算法可以看这个: [搜索]A*和IDA*_c++吧_百度贴吧 这个题学到了一个处理排列的小技巧: 康托展开_百度百科 双向BFS解法:http://blog.csdn.net/dizem/article/details/44

whyorwhnt的专栏 1858

POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)

题意:八数码。 思路:IDA*+曼哈顿距离+判断是否有解。因为要用到IDA*搜索,所以我们搜索之前先判断一下是否有解。 判断的方法是学习一个大神的: 判断八数码问题是否有解  IDA*比起BFS的好处是空间复杂度极低,同时因为有剪枝,比起BFS降低了盲目性;比起A*的好处是不用去维护一个堆。 #include #include #include #include #include #i

GODSPEED 1689

POJ-1077 Eight (三种解法:IDA*、A*、bfs()+康托展开)

题目链接:https://cn.vjudge.net/problem/POJ-1077(根据我自己的理解写的,有错误的地方希望大家可以指正一下,非常感谢!)IDA*解法:什么是IDA*算法?    IDA*是基于深搜的基础上的一种优化算法。    对于这个题来说,由于深搜算出最佳方案需要耗费很多的时间,所以我们就从假定最优解开始算起,若搜索不出来这个结果,就将假定最优解加一再搜一次,直到搜到与当前...

mjj1024的博客 1038

POJ 1077 Eight(据说此题不做人生不完整~~~)

分析: 八数码问题,典型的搜索题。本题有3个点。一个是搜索方式,一个是判重,一个是状态表示。搜索方法有多种,速度从低到高是bfs,双向bfs,A*,IDA*。POJ的discuss说BFS一般在500MS左右。但是本人只会BFS,就先用了BFS的做法。网上最通俗的做法是用康托展开和逆展开来判重和表示状态,再慢一点的就是用一个数组表示状态。 我是不想用康托展开,又觉得数组太慢。所以想了很

行走世间,皆为妖怪 1145

POJ - 1077 Eight : 八数码 -- 哈唏 康托展开 双向bfs A* IDA*

前言 做完这道题,感觉整个人都完整了。 这道题所能涉及到的知识点特别多,列举如下: STL判重 Hash判重 康托展开 双向bfs A* IDA* 每一种知识点分别对应不同的版本,下面的题解按照以上顺序给出各个版本。 另外有个比较好的博客,可以根据这个博客给出的顺序一步一步实现代码,再此感谢此大牛博主。 点此转到 -> 八数码的八境界http:/

duaduac.com 1195

poj 1077 Eight (BFS)

八数码问题,各种解法。 /* // BFS #include #include #include using namespace std; // 把1..n的排列映射为数字 0..(n!-1) int fac[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };//... int order(const char *s, int

王老大 755
上一篇: 搜索 ( BFS )——Maximum Cut Order ( UVA 7460 )
下一篇: 搜索 ( 第K短路, A*搜索)——Remmarguts' Date ( POJ 2449 )
FeBr2
博客等级 码龄11年 11粉丝 243原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值