-1

Blockquote

I need to create a program where the user could input the matrix for the maze, i.e. the row and column of the maze. The user could also decide where is the endpoint, but the start point remained (0,0). This is my current code:

#include<stdio.h>

#define MAXSIZE 1000

// The point in the max (with coordinate)
typedef struct
{
    int x;
    int y;
    int pre; //the previous point in the stack
}Point;

//Create the queue for the maze
typedef struct
{
    Point data[MAXSIZE];
    int front;
    int rear;
}Queue;

int Maze[6][6] = {{0,0,0,1,1,1},{1,1,0,0,0,0},{1,1,0,1,1,0},{1,1,0,0,0,0},{1,1,1,0,1,1},{1,1,1,0,0,1}};

//Function to initialize queue
int InitQueue(Queue *q)
{
    q->front = q->rear = -1;
    return 1;
}

//Function to know whether the queue is empty
int IsEmpty(Queue *q)
{
    if(q->front == q->rear)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//Function to know whether the queue is full
int IsFull(Queue *q)
{
    if(q->rear == MAXSIZE-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//Function to get the front value
int Front(Queue *q)
{
    int x;
    if(IsEmpty(q))
    {
        printf("Queue is empty\n");
        return 0;
    }
    else
    {
        x = q->front;
        return x;
    }
}

//Function to input new path
int Enqueue(Queue *q, Point b)
{
    if(IsFull(q))
    {
        printf("The queue is full\n");
        return 0;
    }
    else
    {
        (q->rear)++;
        q->data[q->rear] = b;

    }
    return 1;
}

//Function to delete queue
int Dequeue(Queue *q, Point *b)
{
    if(IsEmpty(q))
    {
        printf("The queue is empty\n");
        return 0;
    }
    else
    {
        (q->front)++;
        *b = q->data[q->front];
    }
    return 1;
}

//Solution for printing the path
void PrintPath(Queue *q, int front)
{
    int p = front, temp;
    while(p != 0)
    {
        temp = q->data[p].pre;
        q->data[p].pre = -1;
        p = temp;
    }

    for(int i = 0; i <= q->rear; i++)
    {
        if(q->data[i].pre == -1)
        {
            printf("(%d, %d)->", q->data[i].x, q->data[i].y);
        }
    }
    printf("\n");
}

//Solution function
// (x1,y1) is the original point
// (xe,ye) is the end point
int Solution(Queue *q, int x1, int y1, int xe, int ye)
{
    Point now;
    int i, j, i0, j0;

    now.x = x1;
    now.y = y1;
    now.pre = -1;

    Enqueue(q, now);
    Maze[x1][y1] = -1; //starting point

    while(!IsEmpty(q))
    {
        Dequeue(q, &now);

        i = now.x;
        j = now.y;

        if(i == xe && j == ye) //ending point
        {
            PrintPath(q, q->front);
            return 1;
        }

        int dir;
        for(dir = 0; dir < 4; dir++)
        {
            switch(dir)
            {
            case 0:
                i0 = j-1;
                j0 = j;
                break;
            case 1:
                i0 = i;
                j0 = j+1;
                break;
            case 2:
                i0 = i+1;
                j0 = j;
                break;
            case 3:
                i0 = i;
                j0 = j-1;
                break;
            }
            //whether the path is passable
            if(i0 >= 0 && j0 >= 0 && i0 <= 5 && j0 <= 5 && Maze[i0][j0] == 0)
            {
                now.x = i0;
                now.y = j0;
                now.pre = q->front;

                Enqueue(q, now);
                Maze[i0][j0] = -1;
            }
        }
    }
    return 0;
}

int main()
{
    Queue queue;
    InitQueue(&queue);
    printf("The shortest path to the maze: \n");
    if(Solution(&queue, 0, 0, 5, 4) == 0)
    {
        printf("No path found!\n");
    }
    else
    {
        return 0;
    }

}

This code is running well, but I don't know how to make an interface for the user. I tried making a function to initialize the maze, but the output is always "No path found". The user should provide the row and column for the maze, make the maze (using 1 as the wall and 0 as the path), provide the endpoint of the maze.

I tried making the prompt:

int main()
{
    int col, row;
    int Maze[col][row];
    printf("How many column in the maze?(Input no bigger than 10 column)\n");
    scanf("%d", &col);
    printf("How many row in the maze?(Input no bigger than 10 column)\n");
    scanf("%d", &row);
    printf("The maze consist of walls and path\nUse number '1' to make the wall\nUse number '0' to make the path\nEnter the maze: \n");

    for(int i = 0; i<row; i++)
    {
        for(int j = 0; i<col; j++)
        {
            scanf("%d", &Maze[i][j]);
        }
        printf("\n");
    }
    int x2, y2;
    printf("Input the ending point of your maze: (x y)\n");
    scanf("%d %d", &x2, &y2);

    Queue queue;
    InitQueue(&queue);

    if(Solution(&queue, 0, 0, x2, y2) == 0)
    {
        printf("No path found!\n");
    }
    return 0;
}

But I always got 'No Path Found'. I think the reason is that the Maze is not initialized, in the Solution function.

Betty Gandhi
  • 453
  • 1
  • 4
  • 7
  • Okay, so the user inputs dimensions for the matrix, and `0,0` is the starting point. Now, describe in a little more detail what you would like the _user interface_ to provide, or to do. i.e. will it include a console menu to prompt the user to select 1 of several numbers, each representing a different option?. Or should it be a set of instructions to press buttons (maybe arrow keys?) to move the player along the matrix paths? Please edit the bottom section of your post to include these and other details that will help someone know how to help. – ryyker Nov 08 '21 at 13:30
  • The user should provide the row and column for the maze, make the maze (using 1 as the wall and 0 as the path), provide the end point of the maze. – Betty Gandhi Nov 08 '21 at 13:33
  • In your code you have `nt Maze[6][6] = {{0,0,0,1,1,1},...`. Does this describe the maze? (If so, why does the user need to do it?) – ryyker Nov 08 '21 at 13:36
  • Yes, the code that I write does not need any user interface, but my teacher asked me to change it so it could give a user interface. I need to change my code and let the user create the maze. – Betty Gandhi Nov 08 '21 at 13:42
  • Typically functions such `printf()` are used to provide a prompt to user, such as _"Enter number of rows: of matrix:"_, then functions such as [`scanf()` or `fgets`](https://stackoverflow.com/questions/9278226/which-is-the-best-way-to-get-input-from-user-in-c) are used to read users response. – ryyker Nov 08 '21 at 13:47

1 Answers1

-1
#include <stdio.h>

main() {
  int wall, path;
  printf("Enter the dimension of matrix in the form wall(two spaces) path\ n ");
  scanf("%d%d", & wall, & path);
  int m[wall][path];
  printf("Enter the matrix\n");
  for (int i = 0; i < wall; i++) {
    for (int j = 0; j < path; j++) {
      printf("%d,%d=", i + 1, j + 1);
      scanf("%d", & m[i][j]);
    }
  }
  for (int i = 0; i < wall; i++) {
    for (int j = 0; j < path; j++) {
      printf("%d", m[i][j]);
      printf("\t");
    }
    printf("\n");
  }
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37