数据结构--迷宫问题课程设计

 

 

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<Windows.h>
#include<conio.h>
#define STACKSIZE   100
#define M  100
/*-----用数组定义栈表示经过的路径-----*/
typedef struct _sqmaze
{
    int x;
    int y;
}Node;
struct _sqmaze pre[M][M];
int maze[M][M];  /*-----迷宫表示,1表示墙壁,0表示通路-----*/
int book[M][M],dist[M][M]={0};
int m,  n;       /*------迷宮的入口坐标-----*/
int ROW,COL;    //记录迷宫大小 
int next[][2] = {{0,1},{1,0},{0,-1},{-1,0}};    //存储上下左右方向 

int tx,ty;
Node temp;


/*-----迷宫的入口-----*/
void gotoxy(int x, int y) {
    COORD pos = { x,y };
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}

void enter()
{
    int a, b;
    printf("请输入迷宫的入口:");
    scanf("%d%d", &a, &b);
    m = a;
    n = b;
}
/*-----输出一条通路-----*/
void footprint(Node cur)
{
    Node stack[200];
    int base=1,top=-1;
    int i;
    stack[top++] = cur;
    while(cur.x!=m || cur.y!=n)
    {
        tx = cur.x;
        ty = cur.y;
        temp.x = pre[tx][ty].x;
        temp.y = pre[tx][ty].y;
        stack[top++] = temp; 
        cur.x = pre[tx][ty].x;
        cur.y = pre[tx][ty].y;
    }
    for(i=top-1; i>=base; i--)
    {
        printf("<%d,%d> ",stack[i].x,stack[i].y);
    }
    printf("\n");
}
/*-----寻找一条最短的通路-----*/
void pass()
{
    Node que[500];
    Node cur;
    int i;
    int endX,endY;    //存储终点 
    int front = 1 ,rear = 0;
    //数组置0 
    memset(book,0,sizeof(book));
    //输入起点 
    enter();
    //输入终点 
    printf("请输入迷宫的出口行数m和列数n:");
    scanf("%d%d", &endX, &endY);   //(endX,endY)为迷宫的出口*/
    
    printf("起点<%d,%d>\n",m,n);
    printf("终点<%d,%d>\n",endX,endY);

    cur.x = m;
    cur.y = n;
    que[rear++] = cur;
    book[m][n] = ;
    
    while(front<rear)
    {
        cur = que[front];
        
        if(cur.x == endX && cur.y == endY)
        {
            footprint(cur);    
            return; 
        } 
        
        for(i=0; i<4; i++)
        {
            tx = cur.x + next[i][0];
            ty = cur.y + next[i][1];
            if(tx>=2 && tx<ROW && ty>=2 && ty<COL && !book[tx][ty] && !maze[tx][ty])
            {
                book[tx][ty] = book[cur.x][cur.y] + 1;
                pre[tx][ty] = cur;
                temp.x = tx;
                temp.y = ty;
                que[rear++] = temp;
            }
        }
        front++;
    } 
    printf("暂无路径\n");
    
    
}
/*-----创建迷宫-----*/
void creatmaze(int maze[M][M], int m, int n)
{
    int i, j, t;
    printf("请创建您的迷宫;\n");
    for (i = 1; i <= m + 2; i++)
        for (j = 1; j <= n + 2; j++)
        {
            scanf("%d", &t);
            maze[i][j] = t;
            book[i][j] = 0;
        }
}
/*-----输出迷宫-----*/
void printmaze(int maze[M][M], int m, int n)
{
    int i, j;
    printf("输出您创建的迷宫为:");
    for (i = 1; i <= m + 2; i++)
    {
        printf("\n");
        for (j = 1; j <= n + 2; j++)
            printf("%4d", maze[i][j]);
    }
    printf("\n");
}
/*-----主菜单定义-----*/
int menu_select()
{
    char s[80];
    int c;
    gotoxy(1, 25);  /*将光标定在第25行第1列*/
    printf("请按任意键继续 ......\n");    /*提示按任意键继续*/
    getch();                 /*读入任意字符*/
    system("cls");
  //  clrscr();               /*清屏*/
    gotoxy(1, 1);
    printf("***************MENU***************\n\n");
    printf("    0. 手动创建迷宫\n");
    printf("    1. 使用程序迷宫\n");
    printf("    2. 退出程序\n");
    printf("**********************************\n");
    do {
        printf("\n 输入您的选择 (0~2):\n");   /*提示输入选项*/
        scanf("%s", s);                  /*输入选择项*/
        c = atoi(s);                /*将输入的字符串转化为整型*/
    } while (c < 0 || c>2);
    return c;
}
/*-----主函数-----*/
int main()
{
    int row, col;
    int b[10][10] = { {1,1,1,1,1,1,1,1,1,1},
                    {1,0,0,1,0,0,0,1,0,1},
                    {1,0,1,1,0,0,0,1,0,1},
                    {1,0,0,0,0,1,0,0,0,1},
                    {1,0,1,1,1,1,1,0,0,1},
                    {1,0,0,0,1,0,0,0,0,1},
                    {1,0,1,0,0,0,1,0,0,1},
                    {1,0,1,1,1,0,1,1,0,1},
                    {1,1,0,0,0,0,0,0,0,1},
                    {1,1,1,1,1,1,1,1,1,1}, };
    int i, j;
    //clrscr();  /*清屏*/
    system("cls");
    for (;;) {
        switch (menu_select()) {
        case 0: { //clrscr();
            system("cls");
            printf("请输入迷宫的行数(row<=98):\n");
            scanf("%d", &row);
            printf("请输入迷宫的列数(col<=98):\n");
            scanf("%d", &col);
            ROW = row+2;
            COL = col+2; 
            creatmaze(maze, row, col);
            printmaze(maze, row, col);
            pass();
            break;    }
        case 1: {  //clrscr();
            system("cls");
            printf("您选择了程序自动生成的迷宫\n\n");
            for (i = 1; i <= 10; i++)
                for (j = 1; j <= 10; j++){
                    maze[i][j] = b[i - 1][j - 1];
                    book[i][j] = 0;
                }
            printmaze(maze, 8, 8);
            
            ROW = COL = 10;
            pass();
            
            break;      
            }
        case 2:    exit(0);
        }
    }
    return 0;
}
/*
1 1 1 1 1 1 1
1 0 0 1 0 1 1
1 0 1 1 0 1 1
1 0 0 0 0 0 1
1 1 1 0 1 1 1
1 1 1 0 0 0 1
1 1 1 1 1 1 1

*/
 

具体全套分享见评论区~~~ 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值