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.