简体   繁体   English

让迷宫算法在开放迷宫上工作

[英]Getting maze algorithm to work on open maze

first time using the site.第一次使用该网站。 I am trying to figure out how to solve a 'maze' using the shortest path.我试图弄清楚如何使用最短路径解决“迷宫”。 The code works for traditional mazes but the path I am trying to work on is essentially more open.该代码适用于传统迷宫,但我尝试使用的路径本质上更加开放。 When run the current path goes right, then down, then left and goes up then turns right before finally reaching B. My solution needs to go right then up then left to B. Any help would be appreciated!运行时,当前路径向右,然后向下,然后向左,向上然后向右转,最后到达 B。我的解决方案需要 go 向右然后向上然后向左到 B。任何帮助将不胜感激!

9,11
xxxxxxxxxxx
x......B..x
x...xxxx..x
x...xxxx..x
x....A....x
x..xx.xx..x
x.........x
x.........x
xxxxxxxxxxx
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>

char** maze;
int** checked;
int rows;
int cols;
int start_row;
int start_col;
 
enum area
{
    space,
    wall,
    end,
    trail,
};

void alloc_maze() {
    maze = malloc(rows * sizeof(char*));
    for (int i = 0; i < rows; i++) {
        maze[i] = malloc(cols * sizeof(char*));
    }
}

void alloc_checked() {
    checked = malloc(rows * sizeof(char*));
    for (int i = 0; i < rows; i++) {
        checked[i] = malloc(cols * sizeof(char*));
    }
}

void get_maze(const char* file_name)
{
    char c;
    char rows_t[3] = { '\0' };
    char cols_t[3] = { '\0' };
    int rows_i = 0;
    int cols_i = 0;
    int swap = 0;

    FILE* file = fopen(file_name, "r");
    if (file) {
        while ((c = getc(file)) != EOF) {
            if (c == '\n') {
                break;
            }   else if (c ==',')
                {
                    swap = 1;
            }
            else if (!swap) {
                rows_t[rows_i] = c;
                rows_i++;
                } 
            else {
                cols_t[cols_i] = c;
                cols_i++;
                }

        }

    }
    rows = atoi(rows_t);
    cols = atoi(cols_t);

    alloc_maze();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            c = getc(file);

            if (c == '\n') {
                c = getc(file);
            }
            maze[i][j] = c;
            if (c == 'A') {
                start_row = i;
                start_col = j;
            }
        }
    }
    fclose(file);
}

void get_checked() {
    alloc_checked();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (maze[i][j] == 'x') {
                checked[i][j] = wall;
            }
            else if (maze[i][j] == 'B') {
                checked[i][j] = end;
            }
            else  {
                checked[i][j] = space;
            
            }
        }
    }
}

int search(int row, int col) {
    int* current = &checked[row][col];

    if (*current == end) {
        printf("\n congrats you found the shortest path is");
        return 1;
    }
    if (*current == space) {
        *current = wall;


        if (search(row, col + 1)) {
            *current = trail;
            printf("E");
            return 1;
        }

        if (search(row + 1, col)) {
            *current = trail;
            printf("N");
            return 1;
        }


        if (search(row - 1, col)) {
            *current = trail;
            printf("S");
            return 1;
        }

        if (search(row, col - 1)) {
            *current = trail;
            printf("W");
            
                return 1;
        }

        
    }
    return 0;
}

void add_trail() {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (maze[i][j] != 'A'){
                if (checked[i][j] == trail) {
                    maze[i][j] = 'O';
                }
            }
        }
    }
}
void print_checked() {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", checked[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}


void print_maze() {
    printf("\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%c", maze[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main()
{
    get_maze("quickest_route_4.txt");
    get_checked();

    print_maze();


    search(start_row, start_col);
    add_trail();

    print_maze();

    return 0;
}

You have implemented a DFS - Depth First Search maze searching algorithm.您已经实现了DFS - Depth First Search迷宫搜索算法。 This means that your searching algorithm is going to explores as far as possible along a direction before trying another one.这意味着您的搜索算法将在尝试另一个方向之前尽可能地探索另一个方向。
In your code this means that it will always try all of the options going right and then all of the options that are going down and this causes your code to not find the shortest path but just finding a path.在您的代码中,这意味着它将始终尝试所有正确的选项,然后尝试所有失败的选项,这会导致您的代码找不到最短路径,而只是找到路径。
If you do want to find the shortest path you should implement a BFS - Breadth First Search algorithm, it will find you the shortest path since it is progressing the search in all of the active nodes at the same time.如果您确实想找到最短路径,您应该实施BFS - Breadth First Search算法,它会为您找到最短路径,因为它同时在所有活动节点中进行搜索。 It will be a bit harder to implement though since it uses a queue data structure.由于它使用队列数据结构,因此实现起来会有点困难。
Good luck祝你好运

Also notice that the path you are printing is coming out in reverse order.另请注意,您正在打印的路径以相反的顺序出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM