下面程序能找到解。
TC中可以,VC中要出错
#include <stdio.h>
#include <conio.h>
struct point
{
int x;
int y;
};
struct point way[25];
int map[5][5];
int len;
void wtw(struct point,int);
struct point findnext(struct point);
int jud_p(struct point p);
int main ()
{
int k,y,z;
FILE *fp;
struct point p_cur,p_next;
p_cur.x=0;
p_cur.y=0;
wtw(p_cur,0);
clrscr();
fp=fopen("C:\\File\\12.txt","w");
for(k=0;k<=4;k++)
{
scanf("%d %d %d %d %d",&map[k][0],&map[k][1],&map[k][2],&map[k][3],&map[k][4]);
}
if(fp==NULL)
{
printf("Open File Failed !\n");
}
else
{
for(y=0;y<=4;y++)
{
for(z=0;z<=4;z++)
{
fprintf(fp,"%d ",map[y][z]);
}
fprintf(fp,"\n");
}
}
do
{
p_next=findnext(p_cur);
if(((p_next.x)==0)&&((p_next.y)==0))
{
if(len<=0)
{
printf("No Way Been Found !\n");
system("Pause");
return 0;
}
map[p_cur.y][p_cur.x]=1;
p_cur.x=way[len-1].x;
p_cur.y=way[len-1].y;
way[len].x=0;
way[len].y=0;
len=len-1;
}
else
{
wtw(p_next,len);
p_cur.x=p_next.x;
p_cur.y=p_next.y;
printf("(%d,%d)\n",way[len].x+1,way[len].y+1);
fprintf(fp,"(%d,%d)\n",way[len].x+1,way[len].y+1);
getch();
}
}
while(((way[len].x)!=4)||((way[len].y)!=4));
printf("(1,1)->");
for(k=1;k<=24;k++)
{
if(way[k].x!=0||way[k].y!=0)
if(way[k].x!=4||way[k].y!=4)
printf("(%d,%d)->",way[k].x+1,way[k].y+1);
else
printf("(%d,%d)",way[k].x+1,way[k].y+1);
}
printf("\n");
fclose(fp);
system("Pause");
return 0;
}
struct point findnext(struct point poi)
{
struct point p;
p.x=poi.x;
p.y=poi.y;
p.x=p.x+1;
if((jud_p(p))||(p.x>4))
{
p.x=poi.x;
p.y=poi.y;
p.y=p.y+1;
if((jud_p(p))||(p.y>4))
{
p.x=poi.x;
p.y=poi.y;
p.x=poi.x-1;
if((jud_p(p))||(p.x<0))
{
p.x=poi.x;
p.y=poi.y;
p.y=p.y-1;
if((jud_p(p))||(p.y<0))
{
p.x=0;
p.y=0;
return p;
}
else
{
len=len+1;
return p;
}
}
else
{
len=len+1;
return p;
}
}
else
{
len=len+1;
return p;
}
}
else
{
len=len+1;
return p;
}
}
void wtw(struct point _p,int len)
{
way[len].x=_p.x;
way[len].y=_p.y;
}
int jud_p(struct point poin)
{
int i,m=0;
if(map[poin.y][poin.x]==1)
{
m=1;
}
for(i=0;i<=24;i++)
{
if((way[i].x==poin.x)&&(way[i].y==poin.y))
{
m=2;
}
}
return m;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[
本帖最后由 Agdmeg 于 2013-3-25 08:53 编辑 ]