簡體   English   中英

我需要用C編寫一個程序來遞歸地解決X和空白的迷宮

[英]I need to write a program in C that will recursively solve a maze of X's and blank spaces

程序必須從索引[0] [1]開始並在到達索引[7] [7]時完成迷宮。 程序到達“ FindPath”功能時崩潰。 我想不出我在做什么錯,任何信息都可以幫助您。

#include "Header.h"

int main(void) {

int x = 0, y = 1;

char maze[8][8] = {
{' ', ' ', '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', ' '},
};

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


FindPath(maze, x, y);

return 0;
}


#include "Header.h"

void FindPath(char maze[8][8], int x, int y) {

if (x == 7 & y == 7) {
    printf("Maze complete");

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            printf("%c ", maze[i][j]);
        }
        printf("\n");
    }
        return;
}
else {

    if (maze[x + 1][y] = ' ') {
        maze[x][y] = 'h';
        FindPath(maze, x + 1, y);
        return;
    }
    else if (maze[x - 1][y] = ' ') {
        maze[x][y] = 'h';
        FindPath(maze, x - 1, y);
        return;
    }
    else if (maze[x][y + 1] = ' ') {
        maze[x][y] = 'h';
        FindPath(maze, x, y + 1);
        return;
    }
    else if (maze[x][y - 1] = ' ') {
        maze[x][y] = 'h';
        FindPath(maze, x, y - 1);
        return;
    }
    else {
        printf("no path found");
        return;
    }
}
}

我相信這個問題比您要解決的要簡單。

方法的主要問題: FindPath()需要在反復試驗的基礎上工作-您沒有任何策略可以使它成功和/或失敗並向調用者發出信號,也不能撤消它的錯誤假設。

您的代碼存在問題:人們已經提到過= vs. ==但是在表示&&時也使用& 您的代碼縮進或SO代碼發布需要工作; 您應該努力避免代碼中出現數字,以便以后可以換掉迷宮; 您的較低級別例程應返回結果,而不是打印-您較高級別的例程應獲取這些結果並決定要打印的內容; 大量冗余代碼-將重復的語句上移一個級別; 正如其他人所提到的,您需要進行極限檢查,因為如果x為零,像maze[x - 1][y]這樣的表達式會占用您沒有分配的內存。

以下是根據上面的代碼對代碼進行的重做,並進行了一些樣式更改。 它解決了您提供的一種迷宮:

#include <stdio.h>
#include <stdbool.h>

#define WIDTH (8)
#define HEIGHT (8)

void printMaze(char maze[WIDTH][HEIGHT]) {

    for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                    printf("%c ", maze[i][j]);
        }
        putchar('\n');
    }
}

bool findPath(char maze[WIDTH][HEIGHT], int x, int y) {

    maze[x][y] = 'h';

    if (x == WIDTH - 1 && y == HEIGHT - 1) {
        return true;
    }

    if (x + 1 < WIDTH && maze[x + 1][y] == ' ') {
        if (findPath(maze, x + 1, y)) {
            return true;
        }
    }

    if (x - 1 >= 0 && maze[x - 1][y] == ' ') {
        if (findPath(maze, x - 1, y)) {
            return true;
        }
    }

    if (y + 1 < HEIGHT && maze[x][y + 1] == ' ') {
        if (findPath(maze, x, y + 1)) {
            return true;
        }
    }

    if (y - 1 >= 0 && maze[x][y - 1] == ' ') {
        if (findPath(maze, x, y - 1)) {
            return true;
        }
    }

    maze[x][y] = ' ';

    return false;
}

int main(void) {

    char maze[WIDTH][HEIGHT] = {
        {' ', ' ', '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', ' '},
    };

    printMaze(maze);

    if (findPath(maze, 0, 1)) {
        printf("Maze completed!\n");
        printMaze(maze);
    } else {
        printf("No path found!");
    }

    return 0;
}

輸出值

> ./a.out
    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   
Maze completed!
h h x x x x x x 
h x x x x x x x 
h x x x x x x x 
h h h h h x x x 
x x x x h x x x 
x x x x h h h h 
x x x x x x x h 
x x x x x x x h 
> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM