POJ1077 Eight —— A*算法

来源:互联网 发布:华腾软件 编辑:程序博客网 时间:2024/05/19 05:05

主页面:http://blog.csdn.net/dolfamingo/article/details/77825569



关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费的估计值(启发值),f(n) = g(n)+h(n)。
A*算法必须满足的条件(能不能满足由所选的h(n)估计方式决定):每次扩展的节点的 f 值 >= 父节点的 f 值小。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 1e6+10;#define AIM  1     //123456789的哈希值为1struct node{    int status;    int s[9];    int loc;    int g,h,f;    bool operator<(const node x)const{        return f>x.f;    }};int vis[MAXN], fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};int dir[4][2] = { -1,0, 1,0, 0,-1, 0,1 };char op[4] = {'u', 'd', 'l', 'r'  };char path[MAXN];int pre[MAXN];int cantor(int s[])  //获得哈希函数值{    int sum = 0;    for(int i = 0; i<9; i++)    {        int num = 0;        for(int j = i+1; j<9; j++)            if(s[j]<s[i])                num++;        sum += num*fac[8-i];    }    return sum+1;}int dis_h(int s[])  //获得曼哈顿距离{    int dis = 0;    for(int i = 0; i<9; i++)    if(s[i]!=9)     //‘x’不能算进去,否则不能满足:“每次扩展的节点的 f 值 >= 父节点的 f 值小”    {        int x = i/3, y = i%3;        int xx = (s[i]-1)/3, yy = (s[i]-1)%3;        dis += abs(x-xx) + abs(y-yy);    }    return dis;}priority_queue<node>que;bool Astar(node now){    ms(vis,0);    while(!que.empty()) que.pop();    now.status = cantor(now.s);    now.g = 0;    now.h = dis_h(now.s);    now.f = now.f + now.h;    pre[now.status] = -1;   //开始状态的上一个状态为-1,用于输出路径时“刹车”    vis[now.status] = 1;    que.push(now);    node tmp;    while(!que.empty())    {        now = que.top();        que.pop();        if(now.status==AIM) //找到了123456789的状态            return true;        int x = now.loc/3;        int y = now.loc%3;        for(int i = 0; i<4; i++)        {            int xx = x + dir[i][0];            int yy = y + dir[i][1];            if(xx>=0 && xx<=2 && yy>=0 && yy<=2)            {                tmp = now;                tmp.s[x*3+y] = tmp.s[xx*3+yy];  //交换位置,下同                tmp.s[xx*3+yy] = 9;                tmp.status = cantor(tmp.s);                if(!vis[tmp.status])                {                    vis[tmp.status] = 1;                    tmp.loc = xx*3+yy;                    tmp.g++;        //g                    tmp.h = dis_h(tmp.s);   //h                    tmp.f = tmp.g + tmp.h;      //f                    pre[tmp.status] = now.status;   //tmp.status的上一个状态为now.status                    path[tmp.status] = op[i];   //保存操作                    que.push(tmp);                }            }        }    }    return 0;}void Print(int status){    if(pre[status]==-1) return;    Print(pre[status]);     //追溯上一个状态    putchar(path[status]);}int main(){    char str[50];    while(gets(str))    {        node now;        int cnt = 0;        for(int i = 0; str[i]; i++)        {            if(str[i]==' ') continue;            if(str[i]=='x') now.s[cnt] = 9, now.loc = cnt++;            else  now.s[cnt++] = str[i]-'0';        }        if(!Astar(now))            puts("unsolvable");        else            Print(AIM), putchar('\n');    }}




原创粉丝点击