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0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
(感覺是search的問題 但找不出問題)
#include
#include
int V=0;
int xnum;
int ynum;
int a[100][100];
typedef struct
{
int x;
int y;
}NODE;
typedef struct
{
NODE nownode;
NODE prenode;
int dir;
}GRID;
NODE start;
NODE end;
GRID grid;
void Init()
{
FILE *fp;
fp=fopen("mice.txt","r");
fscanf(fp,"%d%d%d%d%d%d",&xnum,&ynum,&start.x,&start.y,&end.x,&end.y);//24 24 1 1 24 1
int i,j;
for(j=0;j<=ynum+1;j++)
for(i=0;i<=xnum+1;i++)
a[j][i]=1;
for(j=1;j<=ynum;j++)
for(i=1;i<=xnum;i++)
fscanf(fp,"%d",&a[j][i]);
//grid.nownode.x =start.x;
//grid.nownode.y=start.y;
grid.nownode=start;
fclose(fp);
}
void search(int x,int y){
if((x==24)&&(y==1))
V=1;
else{
a[x][y]=1;
if((V==0)&&(a[x][y+1]==0)) //向右查找----------v==0 可避免在V==1時的不必要搜索 第一個if裡的可略
search(x,y+1);
if((V==0)&&(a[x+1][y]==0))//向下查找
search(x+1,y);
if((V==0)&&(a[x][y-1]==0)) //向左查找
search(x,y-1);
if((V==0)&&(a[x-1][y]==0)) //向上查找
search(x-1,y);
}
a[x][y]=0;
if(V=1)
a[x][y]=2;
}
int main()
{
int x,y;
Init();
x=grid.nownode.x;
y=grid.nownode.y;
printf("%d %d\n",start.x,start.y);
printf("%d %d %d \n",a[24][1],a[24][0],a[25][1]);
printf("%d \n",V);
search(x,y);
for(int i=0;i<=xnum+1;i++)
{
for(int j=0;j<=ynum+1;j++)
printf("%d",a[i][j]);
printf("\n");
}
scanf("%d",&x);
return 0;
}
search函數裡,
if(V=1)
應該是雙等號吧
if(V==1)