Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

compilation error main.c:

+14 votes
asked Sep 5, 2023 by Biplab Maity (220 points)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define N 3

typedef struct {
    int puzzle[N][N];
    int g, h;
    int f;
} Node;

Node* createNode(int puzzle[N][N]) {
    Node* node = (Node*)malloc(sizeof(Node));
    memcpy(node->puzzle, puzzle, sizeof(node->puzzle));
    node->g = 0;
    node->h = 0;
    node->f = 0;
    return node;
}

int isGoal(Node* node) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (node->puzzle[i][j] != (i * N + j + 1)) {
                return 0;
            }
        }
    }
    return 1;
}

int getEmptyIndex(Node* node) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (node->puzzle[i][j] == 0) {
                return i * N + j;
            }
        }
    }
    return -1;
}

int getMoves(int index, int* moves, int goal[N][N]) {
    int i, j;
    int count = 0;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (i != index / N && j != index % N) {
                moves[count++] = i * N + j;
            }
        }
    }
    return count;
}

int heuristic(Node* node, int goal[N][N]) {
    int manhattanDistance = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int val = node->puzzle[i][j];
            int goalX = (val - 1) / N;
            int goalY = (val - 1) % N;
            manhattanDistance += abs(i - goalX) + abs(j - goalY);
        }
    }
    return manhattanDistance;
}
Node* getNextNode(Node* node, int moves[N], int goal[N][N]) {
    int i, j;
    int minF = INT_MAX;
    Node* nextNode = NULL;

    for (i = 0; i < N; i++) {
        int newIndex = moves[i];

        // Initialize the variable `newNode`
        Node* newNode = createNode(node->puzzle);

        newNode->puzzle[newIndex][i / N][i % N] = 0;
        newNode->puzzle[newIndex][i / N][i % N] = node->puzzle[i / N][i % N];
        newNode->g = node->g + 1;
        newNode->h = heuristic(newNode, goal);

        if (newNode->f < minF) {
            minF = newNode->f;
            nextNode = newNode;
        }
    }
    return nextNode;
}
void printPuzzle(Node* node) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", node->puzzle[i][j]);
        }
        printf("\n");
    }
}
int main() {
    int puzzle[N][N] = {
        {2, 1, 8},
        {6, 4, 3},
        {7, 0, 5}
    };

    int goal[N][N] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 0}
    };

    Node* startNode = createNode(puzzle);

    // TODO: Implement A* search algorithm here
}

2 Answers

0 votes
answered Sep 6, 2023 by Peter Minarik (86,240 points)

Look at your structure definition.

typedef struct {
    int puzzle[N][N];
    int g, h;
    int f;
} Node;

You defined puzzle as a 2-dimensional int array, yet in your code, you're trying to address its 3rd dimension.

newNode->puzzle[newIndex][i / N][i % N] = 0;
newNode->puzzle[newIndex][i / N][i % N] = node->puzzle[i / N][i % N];

I'd recommend reviewing your getNextNode() function. Keep in mind what you're trying to do here and what your variables mean.

0 votes
answered Oct 21, 2023 by Gulshan Negi (1,580 points)

Well, there are some issues with your code, can you try below code to fix this error. 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define N 3

typedef struct {
    int puzzle[N][N];
    int g, h;
    int f;
} Node;

Node* createNode(int puzzle[N][N]) {
    Node* node = (Node*)malloc(sizeof(Node));
    memcpy(node->puzzle, puzzle, sizeof(node->puzzle[0][0]) * N * N);
    node->g = 0;
    node->h = 0;
    node->f = 0;
    return node;
}

int isGoal(Node* node, int goal[N][N]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (node->puzzle[i][j] != goal[i][j]) {
                return 0;
            }
        }
    }
    return 1;
}

int getEmptyIndex(Node* node) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (node->puzzle[i][j] == 0) {
                return i * N + j;
            }
        }
    }
    return -1;
}

int heuristic(Node* node, int goal[N][N]) {
    int manhattanDistance = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int val = node->puzzle[i][j];
            if (val != 0) {
                int goalX = (val - 1) / N;
                int goalY = (val - 1) % N;
                manhattanDistance += abs(i - goalX) + abs(j - goalY);
            }
        }
    }
    return manhattanDistance;
}

Node* getNextNode(Node* node, int goal[N][N]) {
    int moves[4] = {0, 0, 0, 0}; // Adjust this for your puzzle.

    int emptyIndex = getEmptyIndex(node);
    int i, j;
    int minF = INT_MAX;
    Node* nextNode = NULL;

    for (i = 0; i < 4; i++) {
        int newIndex = moves[i];
        if (newIndex != -1) {
            Node* newNode = createNode(node->puzzle);
            // Implement the puzzle move logic here.

            newNode->g = node->g + 1;
            newNode->h = heuristic(newNode, goal);
            newNode->f = newNode->g + newNode->h;

            if (newNode->f < minF) {
                minF = newNode->f;
                nextNode = newNode;
            }
        }
    }
    return nextNode;
}

void printPuzzle(Node* node) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", node->puzzle[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int puzzle[N][N] = {
        {2, 1, 8},
        {6, 4, 3},
        {7, 0, 5}
    };

    int goal[N][N] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 0}
    };

    Node* startNode = createNode(puzzle);
    Node* currentNode = startNode;

    while (!isGoal(currentNode, goal)) {
        currentNode = getNextNode(currentNode, goal);
        if (currentNode == NULL) {
            printf("No solution found.\n");
            break;
        }
        printPuzzle(currentNode);
    }

    // Clean up and free memory as needed.
}

I hope you can fix your error.

Thanks

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...