456

来源:互联网 发布:网络虚拟手机号申请 编辑:程序博客网 时间:2024/06/08 13:15
#include <stdio.h>
#include <math.h>
  
#define MaxLength 100    //�������ȶ��У�Open��������
#define Height     15    //��ͼ�߶�
#define Width      20    //��ͼ���
  
#define Reachable   0    //���Ե���Ľ��
#define Bar         1    //�ϰ���
#define Pass        2    //��Ҫ�ߵIJ���
#define Source      3    //���
#define Destination 4    //�յ�
  
#define Sequential  0    //˳�����
#define NoSolution  2    //�޽���
#define Infinity    0xfffffff
  
#define East       (1 << 0)
#define South_East (1 << 1)
#define South      (1 << 2)
#define South_West (1 << 3)
#define West       (1 << 4)
#define North_West (1 << 5)
#define North      (1 << 6)
#define North_East (1 << 7)
  
typedef struct
{
    signed char x, y;
} Point;
  
const Point dir[8] =
{
    {0, 1},   // East
    {1, 1},   // South_East
    {1, 0},   // South
    {1, -1},  // South_West
    {0, -1},  // West
    {-1, -1}, // North_West
    {-1, 0},  // North
    {-1, 1}   // North_East
};
  
unsigned char within(int x, int y)
{
    return (x >= 0 && y >= 0
        && x < Height && y < Width);
}
  
typedef struct
{
    int x, y;
    unsigned char reachable, sur, value;
} MapNode;
  
typedef struct Close
{
    MapNode *cur;
    char vis;
    struct Close *from;
    float F, G;
    int H;
} Close;
  
typedef struct //���ȶ��У�Open��
{
    int length;        //��ǰ���еij���
    Close* Array[MaxLength];    //��۽���ָ��
} Open;
  
static MapNode graph[Height][Width];
static int srcX, srcY, dstX, dstY;    //��ʼ�㡢�յ�
static Close close[Height][Width];
  
// ���ȶ��л����
void initOpen(Open *q)    //���ȶ��г�ʼ��
{
    q->length = 0;        // ����Ԫ�����ʼΪ0
}
  
void push(Open *q, Close cls[Height][Width], int x, int y, float g)
{    //�����ȶ��У�Open�������Ԫ��
    Close *t;
    int i, mintag;
    cls[x][y].G = g;    //����ӽڵ�����
    cls[x][y].F = cls[x][y].G + cls[x][y].H;
    q->Array[q->length++] = &(cls[x][y]);
    mintag = q->length - 1;
    for (i = 0; i < q->length - 1; i++)
    {
        if (q->Array[i]->F < q->Array[mintag]->F)
        {
            mintag = i;
        }
    }
    t = q->Array[q->length - 1];
    q->Array[q->length - 1] = q->Array[mintag];
    q->Array[mintag] = t;    //����ۺ���ֵ��С�ڵ����ڶ�ͷ
}
  
Close* shift(Open *q)
{
    return q->Array[--q->length];
}
  
// ��ͼ��ʼ������
void initClose(Close cls[Height][Width], int sx, int sy, int dx, int dy)
{    // ��ͼClose���ʼ������
    int i, j;
    for (i = 0; i < Height; i++)
    {
        for (j = 0; j < Width; j++)
        {
            cls[i][j].cur = &graph[i][j];        // Close����ָ�ڵ�
            cls[i][j].vis = !graph[i][j].reachable;        // �Ƿ
0 0