我的九宫格算法

来源:互联网 发布:mac版的网络游戏 编辑:程序博客网 时间:2024/04/29 07:17


//GUOKE000 21:56:43
#include <stdio.h>
#include <string.h>
#include <malloc.h>


/*九宫格的数据结构*/
typedef struct nodetype
{
 char elements[9];
 struct nodetype *mother;/*指向母亲节点*/
 struct nodetype *nchild;/*东西南北四个方向上的孩子节点*/
 struct nodetype *schild;
 struct nodetype *wchild;
 struct nodetype *echild;
}gridnode;

typedef struct pointtype
{
 int x;
 int y;
}gpoint;

typedef struct roadtype
{
 int north;
 int south;
 int west;
 int east;
}road;

typedef struct nodequeuetype
{
 gridnode *nodes[362900];
 int head;
 int tail;
}nodequeue;

char heap[100000000];

void inqueue(gridnode *node,nodequeue *queue)
{
 int temp;
 queue->tail++;
 temp=queue->tail%362900;
 queue->nodes[temp]=node;
 return;
}

gridnode *outqueue(nodequeue *queue)
{
 int temp;
 queue->head++;
 temp=queue->head%362900;
 return queue->nodes[temp];
}
int isempty(nodequeue *queue)
{
 if(queue->head==queue->tail)return 1;
 return 0;
}

gpoint locatezero(char *grid)/*判断0元素在九宫的哪个位置上,返回一个POINT*/
{
 int i=0;
 int j=0;
 int k=0;
 gpoint p;
 while(*grid!=0)
 {
  i++;
  grid++;
 }
 j=i/3+1;
 k=i%3+1;
 p.x=k;
 p.y=j;
 return p;
}

road findway(char *grid)/*通过填充一个ROAD结构来找路*/
{
 gpoint p;
 road s={0,0,0,0};
 p=locatezero(grid);
 if(p.y>1)s.north=1;
 if(p.y<3)s.south=1;
 if(p.x>1)s.west=1;
 if(p.x<3)s.east=1;
 return s;
}
void gridcopy(char *source,char *des)/*复制格格*/
{
 int i=0;
 while(i<9)
 {
  *des=*source;
  des++;
  source++;
  i++;
 }
 return;
}
/*点转化为物理位置*/
int getpos(gpoint g)
{
 return (g.y-1)*3+g.x-1;
}

int hash(char *node)

 int pos;
 pos=*(node)*10000000+*(node+1)*1000000+*(node+2)*100000+*(node+3)*10000+*(node+4)*1000+*(node+5)*100+*(node+6)*10+*(node+7);
 return pos;
}

void inheap(char *node)
{
 int pos;
 pos=hash(node);
 heap[pos]=1;
}

int isallmatch(char *node)
{
 int pos;
 pos=hash(node);
 if(heap[pos]==0)return 0;
 return 1;
}

 

gridnode *buildgrid(char *start,char *finish)
{
 gridnode *head,*temp;
 nodequeue *queue;
 char k=0;
 char elements[10];
 int des=0,j=0;
 int count=0;
 int zeropos=0;
 gpoint point;
 road way={0,0,0,0};

 des=hash(finish);
 queue=(nodequeue *)malloc(sizeof(nodequeue));
 queue->head=queue->tail=-1;
 head=(gridnode *)malloc(sizeof(gridnode));
 head->wchild=head->echild=head->nchild=head->schild=head->mother=NULL;
 gridcopy(start,head->elements);
 inheap(head->elements);
 count++;
 inqueue(head,queue);
 while(!isempty(queue))
 {
  temp=outqueue(queue);
  way=findway(temp->elements);
  if(way.east==1)
  {
   gridcopy(temp->elements,elements);
   point=locatezero(elements);
   zeropos=getpos(point);
   k=elements[zeropos+1];
   elements[zeropos+1]=0;
   elements[zeropos]=k;
   j=isallmatch(elements);
   if(j)goto ende;
   else
   {
    temp->echild=(gridnode *)malloc(sizeof(gridnode));
    gridcopy(elements,temp->echild->elements);
    temp->echild->mother=temp;
   }
   j=hash(temp->echild->elements);
   if(j==des)
   {
    printf("OK");
    return temp->echild;
   }
   inqueue(temp->echild,queue);
   inheap(temp->echild->elements);
   count++;
   printf("%d/n",count);
ende:  ;
  }
  if(way.west==1)
  {
   gridcopy(temp->elements,elements);
   point=locatezero(elements);
   zeropos=getpos(point);
   k=elements[zeropos-1];
   elements[zeropos-1]=0;
   elements[zeropos]=k;
   j=isallmatch(elements);
   if(j)goto endw;
   else
   {
    temp->wchild=(gridnode *)malloc(sizeof(gridnode));
    gridcopy(elements,temp->wchild->elements);
    temp->wchild->mother=temp;
   }
   j=hash(temp->wchild->elements);
   if(j==des)
   {
    printf("OK");
    return temp->wchild;
   }
   inqueue(temp->wchild,queue);
   inheap(temp->wchild->elements);
   count++;
   printf("%d/n",count);
endw:  ;
  }
  if(way.south==1)
  {
   gridcopy(temp->elements,elements);
   point=locatezero(elements);
   zeropos=getpos(point);
   k=elements[zeropos+3];
   elements[zeropos+3]=0;
   elements[zeropos]=k;
   j=isallmatch(elements);
   if(j)goto ends;
   else
   {
    temp->schild=(gridnode *)malloc(sizeof(gridnode));
    gridcopy(elements,temp->schild->elements);
    temp->schild->mother=temp;
   }
   j=hash(temp->schild->elements);
   if(j==des)
   {
    printf("OK");
    return temp->schild;
   }
   inqueue(temp->schild,queue);
   inheap(temp->schild->elements);
   count++;
   printf("%d/n",count);
ends:  ;
  }
  if(way.north==1)
  {
   gridcopy(temp->elements,elements);
   point=locatezero(elements);
   zeropos=getpos(point);
   k=elements[zeropos-3];
   elements[zeropos-3]=0;
   elements[zeropos]=k;
   j=isallmatch(elements);
   if(j)goto endn;
   else
   {
    temp->nchild=(gridnode *)malloc(sizeof(gridnode));
    gridcopy(elements,temp->nchild->elements);
    temp->nchild->mother=temp;
   }
   j=hash(temp->nchild->elements);
   if(j==des)
   {
    printf("OK");
    return temp->nchild;
   }
   inqueue(temp->nchild,queue);
   inheap(temp->nchild->elements);
   count++;
   printf("%d/n",count);
endn:  ;
  } 
 }
 return head;
}

main()
{
 char finish[9]={1,2,3,4,5,6,7,8,0};
 char start[9]={8,0,2,1,4,3,7,6,5};
 int i=0,step=0;
 gridnode *end=NULL;
 end=buildgrid(start,finish);
 printf("/n");
 while(end!=NULL)
 {
  for(i=0;i<=8;i++)
  {
   printf("%d",end->elements[i]);
   if(!((i+1)%3))printf("/n");
  }
  printf("/n");
  end=end->mother;
  step++;
 }
 printf("/ntotal steps: %d/n",step);
 getchar();
}  

原创粉丝点击