回 帖 发 新 帖 刷新版面

主题:我也有一个随机迷宫求路径的代码,共赏析吧.C实现

工作之余编写的一个迷宫问题.请大家评价,多提意见,用单连表和堆栈实现
#include <stdio.h>
#include <stdlib.h>
#include "random.h"
#include "random.c"

typedef struct node{
    int            x_pos;
    int            y_pos;
    int            dir;
    int            mark;
    struct node    *next;
}*NODE;
typedef struct stack{
    NODE        head;
    NODE        tail;
}STACK;

int        x_start, y_start, x_end, y_end;

void    init_stack(STACK *stack);
void    push(STACK *stack, NODE node);
NODE    pop(STACK *stack);
void    maze_create(int n);
void    out_way_print(STACK stack, int n);

int main()
{
    int            n_dimension;
    STACK        valid_path_stack;
    init_stack(&valid_path_stack);
    
    printf ("please input the dimensions of the maze:    ");
    scanf ("%d", &n_dimension);
    maze_create (n_dimension);

    printf("\nNow computer will find if there is a path to the exit!\n");
    out_way_print(valid_path_stack, n_dimension);
}

void    init_stack(STACK *stack)
{
    stack->head = stack->tail = NULL;
}

void     push(STACK *stack, NODE node)
{
    if (stack->tail == NULL) {
        stack->head = stack->tail = node;
    }else {
        stack->head->next = node;
        stack->head = node;
    }
}

NODE    pop(STACK *stack)
{
    NODE        temp;
    temp = stack->tail;
    if (stack->tail == stack->head) {
        free(stack->head);
        printf("\nthe stack is empty!\n");
        return NULL;
    } else {
        while (temp->next != stack->head) {
            temp = temp->next;
        }
        printf("%d    %d",stack->head->x_pos,stack->head->y_pos);
        free(stack->head);
        stack->head = temp;
        stack->head->next = NULL;
        return temp;
    }
}

void     maze_create(int n)
{
    int        i, j, matrix[n][n];
    FILE    *fp;
    if ((fp = fopen("1.dat", "w+")) == NULL) {
        printf("Create the file error!\n");
        exit(0);
    }
    Randomize();
    for (i = 0; i < n; i++) {
        matrix[0][i] = 1;                               /*修改成memset库函数*/
    }
    for (i = 1; i < n-1; i++) {
        matrix[i][0] = matrix[i][n-1] = 1;
        for (j = 1; j < n-1; j++) {
            matrix[i][j] = RandomInteger(0,1);
        }
    }
    for (i = 0; i < n; i++) {
        matrix[n-1][i] = 1;                               /*修改成memset库函数*/
    }

    printf("\nPlease input the position of entrance as follow  x,y:    ");
    scanf("%d,%d", &x_start, &y_start);
    matrix[x_start-1][y_start-1] = 0;
    printf("\nPlease input the position of exit as follow  x,y:    ");
    scanf("%d,%d", &x_end, &y_end);
    matrix[x_end-1][y_end-1] = 0;
    
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            printf("     %d", matrix[i][j]);
            fprintf(fp, "     %d",matrix[i][j]);
        }
        putchar('\n');
        fprintf(fp, "\n");
    }
    if(fclose(fp)) {
        printf("File close error!\n");
        exit(0);
    }
}

void    out_way_print(STACK stack, int n)
{
    int     i, j, x, y;
    long    temp;
    NODE    walker,p_temp;
    char    matrix[n][n];
    temp = 5L;   /*用以隔5个字节的去取数*/
    FILE    *fp;
    if((fp = fopen("1.dat", "r+")) == NULL) {
        printf("Open the file error!\n");
        exit(0);
    }
    
    /*从1.dat中读出了迷宫的原貌并且存放到了matrix二维数组中*/
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            fseek(fp, temp, 0);
            printf("     ");
            putchar(matrix[i][j] = fgetc(fp));
            temp += 6L;
        }
        temp -= 5L;
        fseek(fp, temp, 0);
        putchar(fgetc(fp));
        temp += 6L;
    }
    /*改变方向寻找路径*/
    walker = (NODE)malloc(sizeof(struct node));
    walker->x_pos = x_start;
    walker->y_pos = y_start;
    walker->dir = 1;
    walker->mark = 1;
    walker->next = NULL;
    push(&stack, walker);
    
    while (walker->x_pos != x_end || walker->y_pos != y_end) {
        
        switch(walker->dir){
            case    1:
                if (matrix[walker->x_pos-1][walker->y_pos] == '0') {
                    x = walker->x_pos;
                    y = walker->y_pos+1;
                    walker->dir++;
                    p_temp = (NODE)malloc(sizeof(struct node));
                    walker->next = p_temp;
                    walker = p_temp;
                    walker->x_pos = x;
                    walker->y_pos = y;
                    walker->dir = 1;
                    walker->mark = 1;
                    walker->next = NULL;
                    push(&stack, walker);
                    break;
                }else {
                    walker->dir++;
                    break;
                }
                break;
            case    2:
                if (matrix[walker->x_pos][walker->y_pos-1] == '0') {
                    x = walker->x_pos+1;
                    y = walker->y_pos;
                    walker->dir++;
                    p_temp = (NODE)malloc(sizeof(struct node));
                    walker ->next = p_temp;
                    walker = p_temp;
                    walker->x_pos = x;
                    walker->y_pos = y;
                    walker->dir = 1;
                    walker->mark = 1;
                    walker->next = NULL;
                    push(&stack, walker);
                    break;
                }else {
                    walker->dir++;
                    break;
                }
                break;
            case    3:
                if (walker->y_pos-2>=0 &&
                        matrix[walker->x_pos-1][walker->y_pos-2] == '0' &&
                                walker->mark != 1) {
                    x = walker->x_pos;
                    y = walker->y_pos-1;
                    walker->dir++;
                    p_temp = (NODE)malloc(sizeof(struct node));
                    walker ->next = p_temp;
                    walker = p_temp;
                    walker->x_pos = x;
                    walker->y_pos = y;
                    walker->dir = 1;
                    walker->mark = 1;
                    walker->next = NULL;
                    push(&stack, walker);
                    break;
                }else {
                    walker->dir++;
                    break;
                }
                break;
            case    4:
                if (walker->x_pos-2>=0 &&
                        matrix[walker->x_pos-2][walker->y_pos-1] == '0' &&
                                walker->mark != 1) {
                    x = walker->x_pos-1;
                    y = walker->y_pos;
                    walker->dir++;
                    p_temp = (NODE)malloc(sizeof(struct node));
                    walker ->next = p_temp;
                    walker = p_temp;
                    walker->x_pos = x;
                    walker->y_pos = y;
                    walker->dir = 1;
                    walker->mark = 1;
                    walker->next = NULL;
                    push(&stack, walker);
                    break;
                } else if((walker = pop(&stack)) != NULL) {
                    break;
                } else {
                    printf("\nThere is no way to the exit!!!");
                    exit(0);
                }
                break;
            default:
                if((walker = pop(&stack)) != NULL) {
                    break;
                }else {
                    printf("\nThere is no way to the exit!!!");
                    exit(0);
                }
                break;
        }
    }
    /*打印出来,只是缺少添加到.dat文档的代码*/
    if(walker->x_pos == x_end && walker->y_pos == y_end) {
        printf("There is a path exit in the maze!\n");
        walker = stack.tail;
        do{
            printf("(%d , %d)->", walker->x_pos, walker->y_pos);
            walker = stack.tail->next;
            free(stack.tail);
            stack.tail = walker;
        }while (walker != stack.head);
        printf("(%d , %d)", walker->x_pos, walker->y_pos);
        free(walker);
    }
    if(fclose(fp)) {
        printf("\nClose file error!\n");
        exit(0);
    }
}    

回复列表 (共20个回复)

11 楼

一个简单的图的深搜递归就可以解决的

12 楼

你的程序注释太少,
算不上一个好帖子,
也算不上一个好程序……

[em9]

13 楼

界面做的 好一点比较完美了
用图形函数
搂主文件操作掌握的 很好
像你学习~~~~~~~~~~~~

14 楼

谢谢大家的热情,如果需要我可以为大家在奉献我的一个比这个更完美迷宫程序,其封装与效率以及可读性要比这个强好几倍

15 楼

随机历遍 整个地图 似乎也一个方法哟!

16 楼

高手好多啊!

17 楼

大家都好强啊,我是个菜鸟,什么都不懂,看了这么长的程序,唉“”“”

18 楼

高手就是高手 说出的话都那么有专业是的色彩  不知道我以后会不会像你们一样
你的QQ我可以加吗?

19 楼

我运行了一下好象有好多错误

不信你也自己试试试试试试试试试试试试试试

20 楼

怎么随机生成路径呀,不读文件的那种?
P.S.代码不错

我来回复

您尚未登录,请登录后再回复。点此登录或注册