Ce中的迷宫回溯

时间:2016-12-13 09:07:17

标签: c maze

我正试图用C回溯迷宫。

我需要找到从0 | 0n | n的最短路径。

我无法获得正确的输出。我没有看到我的错误。

我认为这是因为char:int类型,但我不确定。

输入:Labyrinth.def

7 6
0 1 0 0 0 1 1
0 0 0 1 0 1 1
0 1 0 1 0 1 1
0 0 0 0 0 0 1
1 1 1 0 1 0 0
1 1 1 0 1 1 0

C代码

#include <stdio.h>

#define MAXROWS 100
#define MAXCOLS 100
#define WEG '0'
#define MAUER '1'
#define MARKIERT 'x'
#define NOTFOUND 0
#define FOUND !NOTFOUND

typedef char Labyrinth[MAXROWS][MAXCOLS];

void search(Labyrinth l, int *done, int x, int y, int zx, int zy) {
    //printf("position => %d | %d\n", x, y);
    if (x == zx && y == zy) {
        *done == FOUND;
    }
    else { //Weg suchen
        if (!*done && y + 1 <= zy && l[y + 1][x] == WEG) { // runter
            search(l, done, x, y + 1, zx, zy);
            //printf("runter\n");
        }
        if (!*done && x + 1 <= zx && l[y][x + 1] == WEG) { // rechts
            search(l, done, x + 1, y, zx, zy);
            //printf("rechts\n");
        }
        if (!*done && y - 1 <= zy && l[y - 1][x] == WEG) {// hoch
            search(l, done, x, y - 1, zx, zy);
            //printf("hoch\n");
        }
        if (!*done && x - 1 <= zx && l[y][x - 1] == WEG) {// links
            search(l, done, x - 1, y, zx, zy);
            //printf("links\n");
        }
        if (!*done) {
            l[y][x] = WEG;
            //printf("log\n");
        }
    }
}

void loadLabyrinth(Labyrinth l, int *currRows, int *currCols) {
    FILE *fp = NULL;
    int i = 0;
    int tmp = 0;

    int x = 0;
    int y = 0;

    bool s = false;

    //werte aus datei lesen
    fp = fopen("labyrinth.def", "r"); //datei öffnen

    if (fp != NULL) {
        //printf("Datei geoeffnet\n");
        while (!feof(fp)) {
            fscanf(fp, "%d", &tmp); //datei int für int einlesen

            if (s == false) {
                if (i == 0) {
                    *currRows = tmp;
                }
                else if (i == 1) {
                    *currCols = tmp;
                    s = true;
                    i = 0;
                }
            }
            else if (s == true) {
                l[x][y] = (char)tmp;
                x++;
                if (i % *currRows == 0) {
                    x = 0;
                    y += 1;
                }
            }

            i++;

            //printf("%d", tmp);
        }
    }
    else {
        printf("Datei nicht gefunden ... :(\n");
    }

    fclose(fp); // datei schließen
}

void displayLabyrinth(Labyrinth l, int rows, int cols) {
    for (int i = 0; i < cols; i++) {
        for (int k = 0; k < rows; k++) {
            printf("%i ", l[k][i]);
            if (k + 1 == rows) printf("\n");
        }
    }
}

int main()
{
    Labyrinth laby;

    int currRows = 0;
    int currCols = 0;
    int geschafft = NOTFOUND;

    int array[MAXROWS][MAXCOLS];

    loadLabyrinth(laby, &currRows, &currCols); // Labyrinth laden

    printf("%d %d\n", currRows, currCols);

    displayLabyrinth(laby, currRows, currCols); // Labyrinth ausgeben

    printf("\nLabyrinth geladen ! \n");
    printf("Loesung wird gesucht ... \n\n");

    //do {
        search(laby, &geschafft, 0, 0, currCols - 1, currRows - 1);
    //}while (geschafft != FOUND);

    displayLabyrinth(laby, currRows, currCols); // Labyrinth ausgeben

    getchar();
}

输出Consol:

48 1 0 0 0 1 1
0 0 0 1 0 1 1
0 1 0 1 0 1 1
0 0 0 0 0 0 1
1 1 1 0 1 0 0
1 1 1 0 1 1 0

1 个答案:

答案 0 :(得分:1)

迷宫中的元素是char,而不是int,所以你需要替换

printf("%i ", l[k][i]);

printf("%c ", l[k][i]);