数据结构[迷宫问题] 急求~~~~~~~~
在迷宫中求从入口到出口的一条简单路径。迷宫可用下图表示,1表示通道0表示墙。11111111111101001110011000001001110111000111100...
在迷宫中求从入口到出口的一条简单路径。迷宫可用下图表示,1表示通道
0表示墙。
11111111111
10100111001
10000010011
10111000111
10001011011
11001011001
11100000001
11111111111 展开
0表示墙。
11111111111
10100111001
10000010011
10111000111
10001011011
11001011001
11100000001
11111111111 展开
1个回答
展开全部
运行时提示输入起始和终止坐标,这个坐标是迷宫按行展开之后的序号,从0开始,比如输入“13,18”表示搜索由第13格到第18格的可达路径,它会把所有可能的路径都以图形式输出,“X”表示墙,“.”表示空,“1”表示足迹,如果不可达或者开始坐标是墙,则输出“目标不可达”提示,参考程序如下,具体迷宫表示形式可修改,只要把对应字符一换就可以了:
#include<stdio.h>
#include<math.h>
#define N 12
int search(int,int);
int canplace(int,int);
int isaim(int,int);
void readdata();
void printfresult();
int a[20][20],s,t,sign=0;
int isaim(int,int);
void main()
{
int row,col;
readdata();
row = s/N;
col = s%N;
search(row,col);
if(sign==0)
printf("目标不可达\n");
}
int search(int row,int col)
{
int r,c;
if(a[row][col]==-2)
return 0;
a[row][col] = 1;
if(isaim(row,col))
printfresult();
r = row; //左
c = col-1;
if(canplace(r,c))
search(r,c);
r = row+1; //下
c = col;
if(canplace(r,c))
search(r,c);
r = row; //右
c = col+1;
if(canplace(r,c))
search(r,c);
r = row-1; //上
c = col;
if(canplace(r,c))
search(r,c);
a[row][col] = 0;
}
void printfresult()
{
int i,j;
printf("\nThe result follow as:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(a[i][j]==0)
printf(" .");
else if(a[i][j]==1)
printf(" 1");
else
printf(" X");
}
printf("\n");
}
printf("------------------");
sign=1;
}
int canplace(int r,int c)
{
if(r>=0&&r<N&&c>=0&&c<N&&a[r][c]==0)
return 1;
else
return 0;
}
int isaim(int r,int c)
{
if(r==t/N&&c==t%N)
return 1;
else
return 0;
}
void readdata()
{
int i,j;
char str[12][12]={{'X','X','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','.','.','.','X','.','X','X','X'},
{'X','.','X','.','X','X','.','.','.','.','.','X'},
{'X','.','X','.','X','X','.','X','X','X','.','X'},
{'X','.','X','.','.','.','.','.','X','.','.','X'},
{'X','.','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','X','.','X','.','.','.','.','X'},
{'X','.','X','X','X','.','.','.','X','X','X','X'},
{'X','.','.','.','.','.','X','X','.','.','.','X'},
{'X','X','X','.','X','X','X','X','.','X','.','X'},
{'X','X','X','X','X','X','X','X','.','X','X','X'},
{'X','X','X','X','X','X','X','X','X','X','X','X'},
};
printf("Please input start and end value:"); //for example:13,128
scanf("%d,%d",&s,&t);
printf("The initial state:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(str[i][j]=='.')
a[i][j] = 0;
else
a[i][j] = -2;
printf(" %c",str[i][j]);
}
printf("\n");
}
printf("-----------------\n");
}
#include<stdio.h>
#include<math.h>
#define N 12
int search(int,int);
int canplace(int,int);
int isaim(int,int);
void readdata();
void printfresult();
int a[20][20],s,t,sign=0;
int isaim(int,int);
void main()
{
int row,col;
readdata();
row = s/N;
col = s%N;
search(row,col);
if(sign==0)
printf("目标不可达\n");
}
int search(int row,int col)
{
int r,c;
if(a[row][col]==-2)
return 0;
a[row][col] = 1;
if(isaim(row,col))
printfresult();
r = row; //左
c = col-1;
if(canplace(r,c))
search(r,c);
r = row+1; //下
c = col;
if(canplace(r,c))
search(r,c);
r = row; //右
c = col+1;
if(canplace(r,c))
search(r,c);
r = row-1; //上
c = col;
if(canplace(r,c))
search(r,c);
a[row][col] = 0;
}
void printfresult()
{
int i,j;
printf("\nThe result follow as:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(a[i][j]==0)
printf(" .");
else if(a[i][j]==1)
printf(" 1");
else
printf(" X");
}
printf("\n");
}
printf("------------------");
sign=1;
}
int canplace(int r,int c)
{
if(r>=0&&r<N&&c>=0&&c<N&&a[r][c]==0)
return 1;
else
return 0;
}
int isaim(int r,int c)
{
if(r==t/N&&c==t%N)
return 1;
else
return 0;
}
void readdata()
{
int i,j;
char str[12][12]={{'X','X','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','.','.','.','X','.','X','X','X'},
{'X','.','X','.','X','X','.','.','.','.','.','X'},
{'X','.','X','.','X','X','.','X','X','X','.','X'},
{'X','.','X','.','.','.','.','.','X','.','.','X'},
{'X','.','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','X','.','X','.','.','.','.','X'},
{'X','.','X','X','X','.','.','.','X','X','X','X'},
{'X','.','.','.','.','.','X','X','.','.','.','X'},
{'X','X','X','.','X','X','X','X','.','X','.','X'},
{'X','X','X','X','X','X','X','X','.','X','X','X'},
{'X','X','X','X','X','X','X','X','X','X','X','X'},
};
printf("Please input start and end value:"); //for example:13,128
scanf("%d,%d",&s,&t);
printf("The initial state:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(str[i][j]=='.')
a[i][j] = 0;
else
a[i][j] = -2;
printf(" %c",str[i][j]);
}
printf("\n");
}
printf("-----------------\n");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询