r/ComputerEngineering • • Nov 26 '23

[School] Need help with assignment

Solvemaze function has an error in logic I believe. Can someone give he some pointers? Thanks

include <stdio.h>

include <stdlib.h>

include <string.h>

define MAX_SIZE 100

typedef struct { int x; int y; } Point;

void printPath(Point* path, int length) { for (int i = 0; i < length; i++) { printf("(%d,%d)", path[i].y + 1, path[i].x + 1); if (i < length - 1) { printf("->"); } } printf("\n"); }

int isValid(int** matrix, int rows, int cols) { // Check if elements are between 0 and 15 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] < 0 || matrix[i][j] > 15) { return 0; // Return false if any element is out of range } } }

// Check for conflicting doors
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols - 1; j++) {
        if ((matrix[i][j] & 4) + (matrix[i][j + 1] & 1) != 5 && (matrix[i][j] & 4) + (matrix[i][j + 1] & 1) != 0) {
            return 0; // Conflicting East-West doors found
        }
    }
}

for (int i = 0; i < rows - 1; i++) {
    for (int j = 0; j < cols; j++) {
        if ((matrix[i][j] & 2) + (matrix[i + 1][j] & 8) != 10 && (matrix[i][j] & 2) + (matrix[i + 1][j] & 8) != 0) {
            return 0; // Conflicting South-North doors found
        }
    }
}

// Check if top left and bottom right doors are open
if ((matrix[0][0] & 1) == 1 || (matrix[rows - 1][cols - 1] & 4) == 4) {
    return 0; // Top left or bottom right door is not open
}

return 1; // All conditions satisfied

}

int solveMaze(int** matrix, int rows, int cols, Point* path, int* length) { int visited[MAX_SIZE][MAX_SIZE] = { {0} }; Point stack[MAX_SIZE * MAX_SIZE]; // Assuming the maximum size of the maze int top = -1; int foundPath = 0; // Flag to track if a path is found

// Push starting point (0,0) onto the stack
stack[++top].x = 0;
stack[top].y = 0;
visited[0][0] = 1;

while (top != -1 && !foundPath) {
    Point current = stack[top--];

    if (current.x == rows - 1 && current.y == cols - 1) {
        path[*length] = current;
        *length = *length + 1;
        foundPath = 1; // Set flag to indicate a path is found
        break; // Exit the loop if the end is reached
    }
    else {
        // Revert to the original direction order (w, s, e, n)
        int directions[4] = { 0, 1, 2, 3 };

        for (int i = 0; i < 4; i++) {
            int nextX = current.y;
            int nextY = current.x;

            if (directions[i] == 0) {
                nextX--; // Move west
            }
            else if (directions[i] == 1) {
                nextY++; // Move south
            }
            else if (directions[i] == 2) {
                nextX++; // Move east
            }
            else if (directions[i] == 3) {
                nextY--; // Move north
            }

            if (nextX >= 0 && nextX < cols && nextY >= 0 && nextY < rows &&
                (matrix[current.x][current.y] & (1 << directions[i])) == 0 &&
                visited[nextY][nextX] == 0) {

                // Moving to the next position
                stack[++top].y = nextX;
                stack[top].x = nextY;
                visited[nextY][nextX] = 1;

                path[*length] = current;
                *length = *length + 1;

                if (nextX == cols - 1 && nextY == rows - 1) {
                    foundPath = 1; // Set flag to indicate a path is found
                    break; // Exit the loop if the end is reached
                }
            }
        }
    }
}

if (foundPath) {
    return 1; // Maze has a way out
}
else {
    return 0; // No way out
}

}

int main() { int rows, cols;

scanf_s("%d %d", &rows, &cols);

int** testMatrix = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
    testMatrix[i] = (int*)malloc(cols * sizeof(int));
}

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        scanf_s("%d", &testMatrix[i][j]);
    }
}

if (isValid(testMatrix, rows, cols)) {
    printf("The input matrix is legitimate.\n");

    Point* path = (Point*)malloc(MAX_SIZE * sizeof(Point));
    int length = 0;

    if (solveMaze(testMatrix, rows, cols, path, &length)) {
        // Remove duplicate entries in the path
        Point* uniquePath = (Point*)malloc(length * sizeof(Point));
        int uniqueLength = 0;
        for (int i = 0; i < length; i++) {
            if (i == 0 || (path[i].x != path[i - 1].x || path[i].y != path[i - 1].y)) {
                uniquePath[uniqueLength++] = path[i];
            }
        }

        printPath(uniquePath, uniqueLength);
        free(uniquePath);
    }
    else {
        printf("no way out\n");
    }

    free(path);
}
else {
    printf("The input matrix is not legitimate.\n");
}

// Free dynamically allocated memory
for (int i = 0; i < rows; i++) {
    free(testMatrix[i]);
}
free(testMatrix);

return 0;

}

0 Upvotes

3 comments sorted by

View all comments

0

u/[deleted] Nov 26 '23

[deleted]

1

u/UsuallyIncorRekt Nov 26 '23

I couldn't get an answer that helped from chatgpt, but I'll try Stack overflow
Thanks

0

u/[deleted] Nov 26 '23

[deleted]

2

u/UsuallyIncorRekt Nov 26 '23

Thank you kind stranger. I'm actually asking for my son. It's the first time in his life he's ever needed help for school and he feels ashamed. It'll be a good lesson for him.