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 0 1 1 1 1 1 1 1 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
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;
}
  • 写回答

1条回答 默认 最新

  • 知常曰明 2015-06-10 22:52
    关注

    search函数里,

     if(V=1)
    

    应该是双等号吧

     if(V==1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题