主题:[讨论]探索迷宫的所有路径有问题,希望高手指点一下
#include <stdio.h>
struct path{
int x,y,c;
}pa[400];
int m,n;
int k,l;
void print(int a[20][20],struct path pa[400]);
void way(int a[20][20],struct path pa[400],int x,int y,int i);
main()
{
int a[20][20]={0},b[20][20]={0};
int i,j,u,o;
scanf("%d,%d",&k,&l);
for(i=0;i<l+2;i++){
a[0][i]=a[k+1][i]=1;
}
for(i=0;i<k+2;i++){
a[i][0]=a[i][l+1]=1;
}
for(i=1;i<k+1;i++){
for(j=1;j<l+1;j++){
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}}
for(i=0;i<=k+1;i++){
for(j=0;j<=l+1;j++){
printf(" %d",a[i][j]);
}
printf("\n");
}
scanf("%d,%d %d,%d",&u,&o,&m,&n);
way(a,pa,o,u,0);
}
void print(int a[20][20],struct path pa[400])
{
int i,j;
int count=0;
for(i=1;i<k+1;i++){
for(j=1;j<l+1;j++){
if(!a[i][j] && pa[count].x==i && pa[count].y==j)
{printf(" 2");count++;}
else if(a[i][j]) printf(" 1");
else printf(" ");
}
printf("\n");
}
}
void way(int a[20][20],struct path pa[400],int x,int y,int i)
{
pa[i].x=x;
pa[i].y=y;
pa[i].c=0; printf("%d %d\n",x,y);
if(x==m && y==n){
print(a,pa);
return;
}getchar();
Try: pa[i].c++;
if(pa[i].c<5){
switch(pa[i].c)
{
case 1:
if(!a[x+1][y]) way(a,pa,x+1,y,i+1);
goto Try;
case 2:
if(!a[x-1][y]) way(a,pa,x-1,y,i+1);
goto Try;
case 3:
if(!a[x][y+1]) way(a,pa,x,y+1,i+1);
goto Try;
case 4:
if(!a[x][y-1]) way(a,pa,x,y-1,i+1);
goto Try;
}
}
pa[i].x=0;
pa[i].y=0;
return ;
}
struct path{
int x,y,c;
}pa[400];
int m,n;
int k,l;
void print(int a[20][20],struct path pa[400]);
void way(int a[20][20],struct path pa[400],int x,int y,int i);
main()
{
int a[20][20]={0},b[20][20]={0};
int i,j,u,o;
scanf("%d,%d",&k,&l);
for(i=0;i<l+2;i++){
a[0][i]=a[k+1][i]=1;
}
for(i=0;i<k+2;i++){
a[i][0]=a[i][l+1]=1;
}
for(i=1;i<k+1;i++){
for(j=1;j<l+1;j++){
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}}
for(i=0;i<=k+1;i++){
for(j=0;j<=l+1;j++){
printf(" %d",a[i][j]);
}
printf("\n");
}
scanf("%d,%d %d,%d",&u,&o,&m,&n);
way(a,pa,o,u,0);
}
void print(int a[20][20],struct path pa[400])
{
int i,j;
int count=0;
for(i=1;i<k+1;i++){
for(j=1;j<l+1;j++){
if(!a[i][j] && pa[count].x==i && pa[count].y==j)
{printf(" 2");count++;}
else if(a[i][j]) printf(" 1");
else printf(" ");
}
printf("\n");
}
}
void way(int a[20][20],struct path pa[400],int x,int y,int i)
{
pa[i].x=x;
pa[i].y=y;
pa[i].c=0; printf("%d %d\n",x,y);
if(x==m && y==n){
print(a,pa);
return;
}getchar();
Try: pa[i].c++;
if(pa[i].c<5){
switch(pa[i].c)
{
case 1:
if(!a[x+1][y]) way(a,pa,x+1,y,i+1);
goto Try;
case 2:
if(!a[x-1][y]) way(a,pa,x-1,y,i+1);
goto Try;
case 3:
if(!a[x][y+1]) way(a,pa,x,y+1,i+1);
goto Try;
case 4:
if(!a[x][y-1]) way(a,pa,x,y-1,i+1);
goto Try;
}
}
pa[i].x=0;
pa[i].y=0;
return ;
}