#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<windows.h>
int light(int dir,int ban) //指路函数
{
if(dir==ban)
{
return dir++;
}
return dir;
}
struct way
{
int dot[2]; //点的坐标
int dir; //1234分别为右下左上
int num; //寻址
struct way *next;
};
int search(int step)
{
int x,num,ban; //dot中坐标
char map[50][50]={1,1}; //定义地图
struct way *head,*current,*next,*p;
head=(struct way*)malloc(sizeof(struct way));
current->dir=light(current->dir);
if(step==0)
{
printf("请输入起点坐标");
for(x=0;x<2;x++)
{
scanf("%d",head->dot[x]);
}
head->dir=1; //起始方向为右(按顺时针变化)
current=head;
}
next=(struct way*)malloc(sizeof(struct way));
switch(current->dir)
{
case 1:
{
next->dot[0]++;current->dir++;break;
}
case 2:
{
next->dot[1]++;current->dir++;break;
}
case 3:
{
next->dot[0]--;current->dir++;break;
}
case 4:
{
next->dot[1]--;current->dir++;break;
}
}
if(map[next->dot[0]][next->dot[1]] =='1') //假如下一步能走得通
{
/*next->dir = light(current->dir,next);*/ //下一步的方向
current->next =next;
current=next;
current->dir=1;
step++;
step=search(step);
}
else
{
if((current->dir)<=4)
{
step=search(step);
}
if((current->dir)>4) //如果current的四个方向都不能走
{
for(num=0;num<=step;num++) //从栈中寻找current的上一个结点
{
if(num==0)
{
p=head;
}
else
{
p=p->next;
}
}
}
}
return step;
}
void main()
{
int step=0;
step=search(step); //从起点处开始寻找路径
}
#include<string.h>
#include<malloc.h>
#include<windows.h>
int light(int dir,int ban) //指路函数
{
if(dir==ban)
{
return dir++;
}
return dir;
}
struct way
{
int dot[2]; //点的坐标
int dir; //1234分别为右下左上
int num; //寻址
struct way *next;
};
int search(int step)
{
int x,num,ban; //dot中坐标
char map[50][50]={1,1}; //定义地图
struct way *head,*current,*next,*p;
head=(struct way*)malloc(sizeof(struct way));
current->dir=light(current->dir);
if(step==0)
{
printf("请输入起点坐标");
for(x=0;x<2;x++)
{
scanf("%d",head->dot[x]);
}
head->dir=1; //起始方向为右(按顺时针变化)
current=head;
}
next=(struct way*)malloc(sizeof(struct way));
switch(current->dir)
{
case 1:
{
next->dot[0]++;current->dir++;break;
}
case 2:
{
next->dot[1]++;current->dir++;break;
}
case 3:
{
next->dot[0]--;current->dir++;break;
}
case 4:
{
next->dot[1]--;current->dir++;break;
}
}
if(map[next->dot[0]][next->dot[1]] =='1') //假如下一步能走得通
{
/*next->dir = light(current->dir,next);*/ //下一步的方向
current->next =next;
current=next;
current->dir=1;
step++;
step=search(step);
}
else
{
if((current->dir)<=4)
{
step=search(step);
}
if((current->dir)>4) //如果current的四个方向都不能走
{
for(num=0;num<=step;num++) //从栈中寻找current的上一个结点
{
if(num==0)
{
p=head;
}
else
{
p=p->next;
}
}
}
}
return step;
}
void main()
{
int step=0;
step=search(step); //从起点处开始寻找路径
}