Here is a simple implementation for maze issue.

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->#include<stdio.h>
#include
<stdlib.h>
#include
<stdext.h>
#include
<stdbool.h>
#defineROW_NUM8
#defineCOL_NUM8

typedef
struct{
intx;
inty;
structposition*next;
}position;

staticintmaze[ROW_NUM][COL_NUM]={{0,0,1,0,0,0,0,1},
{
1,1,0,1,1,0,1,0},
{
0,1,0,1,1,0,1,0},
{
0,1,0,1,0,1,0,1},
{
0,1,1,0,1,1,1,1},
{
0,0,0,1,1,0,0,1},
{
1,1,0,1,0,1,1,0},
{
0,0,1,0,1,1,0,0}};

boolisvalid(intx,inty)
{
if(x>=0&&x<ROW_NUM&&y>=0&&y<COL_NUM&&maze[x][y]==0)returntrue;
returnfalse;
}

boolinpath(intx,inty,position*pos)
{
while(pos!=NULL){
if(pos->x==x&&pos->y==y)returntrue;
pos
=(position*)(pos->next);
}
returnfalse;
}


staticboolpathfound=false;

voidwalkmaze(intx,inty,position*pos)
{
if(pathfound||!isvalid(x,y)||inpath(x,y,pos))return;
if(x==ROW_NUM-1&&y==COL_NUM-1){
printf(
"(%d,%d)",x,y);
while(pos!=NULL){
printf(
"<-(%d,%d)",pos->x,pos->y);
pos
=(position*)pos->next;
}
puts(
"");pathfound=true;return;
}
positionpos6;pos6.x
=x,pos6.y=y;pos6.next=pos;walkmaze(x+1,y+1,&pos6);
positionpos8;pos8.x
=x,pos8.y=y;pos8.next=pos;walkmaze(x,y+1,&pos8);
positionpos5;pos5.x
=x,pos5.y=y;pos5.next=pos;walkmaze(x+1,y,&pos5);
positionpos1;pos1.x
=x,pos1.y=y;pos1.next=pos;walkmaze(x-1,y+1,&pos1);
positionpos2;pos2.x
=x,pos2.y=y;pos2.next=pos;walkmaze(x-1,y,&pos2);
positionpos4;pos4.x
=x,pos4.y=y;pos4.next=pos;walkmaze(x+1,y-1,&pos4);
positionpos7;pos7.x
=x,pos7.y=y;pos7.next=pos;walkmaze(x,y-1,&pos7);
positionpos3;pos3.x
=x,pos3.y=y;pos3.next=pos;walkmaze(x-1,y-1,&pos3);
}

intmain()
{
walkmaze(
0,0,NULL);
return0;
}

迷宫算法

相关文章:

  • 2022-01-19
  • 2022-12-23
  • 2023-03-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-29
  • 2021-06-12
猜你喜欢
  • 2021-04-05
  • 2022-01-11
  • 2021-07-10
  • 2022-12-23
  • 2021-09-17
  • 2022-02-01
相关资源
相似解决方案