//迷宫问题,递推解法
#include<stdio.h>
#define desr 6
#define desc 5
int a[8][7]={{1,1,1,1,1,1,1},
{1,0,0,1,0,0,1},
{1,1,0,1,1,0,1},
{1,0,1,0,1,1,1},
{1,1,1,0,0,1,1},
{1,1,1,0,1,0,1},
{1,1,1,0,1,0,1},
{1,1,1,1,1,1,1}
};
int maze(int r,int c)
{
if(a[6][5]==2)
return 1;
else if(a[r][c]==0)
{
a[r][c]=2;
if(maze(r-1,c)) return 1;
if(maze(r-1,c+1)) return 1;
if(maze(r-1,c-1)) return 1;
if(maze(r,c-1)) return 1;
if(maze(r,c+1)) return 1;
if(maze(r+1,c)) return 1;
if(maze(r+1,c-1)) return 1;
if(maze(r+1,c+1)) return 1;
a[r][c]=3;
return 0;
}
else
return 0;
}
int main()
{
int i,j;
maze(1,1);
for(i=0;i<8;i++)
{
printf("/n");
for(j=0;j<7;j++)
{
printf("-%d-",a[i][j]);
}
}
printf("/n");
return 0;
}
We started from (1,1) .Ended at (6,5);
#include<stdio.h>
#define desr 6
#define desc 5
int a[8][7]={{1,1,1,1,1,1,1},
{1,0,0,1,0,0,1},
{1,1,0,1,1,0,1},
{1,0,1,0,1,1,1},
{1,1,1,0,0,1,1},
{1,1,1,0,1,0,1},
{1,1,1,0,1,0,1},
{1,1,1,1,1,1,1}
};
int maze(int r,int c)
{
if(a[6][5]==2)
return 1;
else if(a[r][c]==0)
{
a[r][c]=2;
if(maze(r-1,c)) return 1;
if(maze(r-1,c+1)) return 1;
if(maze(r-1,c-1)) return 1;
if(maze(r,c-1)) return 1;
if(maze(r,c+1)) return 1;
if(maze(r+1,c)) return 1;
if(maze(r+1,c-1)) return 1;
if(maze(r+1,c+1)) return 1;
a[r][c]=3;
return 0;
}
else
return 0;
}
int main()
{
int i,j;
maze(1,1);
for(i=0;i<8;i++)
{
printf("/n");
for(j=0;j<7;j++)
{
printf("-%d-",a[i][j]);
}
}
printf("/n");
return 0;
}
We started from (1,1) .Ended at (6,5);