|
发表于 2022-6-18 16:08:30
|
显示全部楼层
本帖最后由 蔚蓝水晶 于 2022-6-18 16:12 编辑
- #include <stdio.h>
- #define W 100 //迷宫的宽
- char De[4]={'U','D','L','R'}; //用来输出路径
- int deM(int a[][W],int road[],int count,int h,int w,int choice,int x,int y); //检测函数,用来指明方向
- int mm(int a[][W],int road[],int count,int h,int w,int choice,int x,int y); //deM的过渡函数,主要用来生成完整路径
- int mm(int a[][W],int road[],int count,int h,int w,int choice,int x,int y){
- int n=count+1;
- if(deM(a,road,n,h,w,choice,x,y)){
- road[count]=De[choice];
- return 1;
- }
- else
- return 0;
- }
- int deM(int a[][W],int road[],int count,int h,int w,int choice,int x,int y){
- if(h==y&&w==x)
- return 1;
-
- else{
- if(y!=0&&choice!=1){
- if(*(*(a+y-1)+x)==0){
- if(mm(a,road,count,h,w,0,x,y-1))
- return 1;
- }
- }
- if(y!=h&&choice!=0){
- if(*(*(a+y+1)+x)==0){
- if(mm(a,road,count,h,w,1,x,y+1))
- return 1;
- }
- }
- if(x!=0&&choice!=3){
- if(*(*(a+y)+x-1)==0){
- if(mm(a,road,count,h,w,2,x-1,y))
- return 1;
- }
- }
- if(x!=w&&choice!=2){
- if(*(*(a+y)+x+1)==0){
- if(mm(a,road,count,h,w,3,x+1,y))
- return 1;
- }
- }
- return 0;
- }
- }
- int main(void){
- int i,j,h,w;
- printf("请输入迷宫的规模:");
- scanf("%d %d",&h,&w);
- getchar();
- int M[h][W],road[h*w+1]={0};
- for(i=0;i<h;i++){
- printf("请输入第%d行迷宫:",i+1);
- for(j=0;j<w;j++){
- scanf("%d",&M[i][j]);
- getchar();
- }
- }
- if(deM(M,road,0,h-1,w-1,-1,0,0)){
- for(i=0;i<h*w;i++)
- printf("%c",road[i]);
- }
- else
- printf("No pass!");
- }
复制代码 |
|