【发布时间】:2021-11-08 12:56:56
【问题描述】:
块引用
我需要创建一个程序,用户可以在其中输入迷宫的矩阵,即迷宫的行和列。用户还可以决定端点在哪里,但起点仍然是 (0,0)。这是我当前的代码:
#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;
}
}
这段代码运行良好,但我不知道如何为用户制作界面。我尝试制作一个函数来初始化迷宫,但输出始终是“找不到路径”。 用户需要提供迷宫的行列,制作迷宫(以1为墙,0为路径),提供迷宫的终点。
我尝试了提示:
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;
}
但我总是得到“找不到路径”。我认为原因是迷宫没有在解决方案函数中初始化。
【问题讨论】:
-
好的,所以用户输入矩阵的维度,
0,0是起点。现在,更详细地描述您希望 用户界面 提供或执行的操作。即它是否会包含一个控制台菜单来提示用户从几个数字中选择一个,每个数字代表一个不同的选项?还是应该是一组指令来按下按钮(可能是箭头键?)以沿着矩阵路径移动玩家?请编辑您帖子的底部以包含这些和其他详细信息,以帮助某人知道如何提供帮助。 -
用户提供迷宫的行列,制作迷宫(1为墙,0为路径),提供迷宫的终点。
-
在你的代码中你有
nt Maze[6][6] = {{0,0,0,1,1,1},...。这是描述迷宫吗? (如果是,用户为什么需要这样做?) -
是的,我写的代码不需要任何用户界面,但是我的老师让我改变它,以便它可以提供用户界面。我需要更改我的代码并让用户创建迷宫。
-
通常使用
printf()等函数向用户提供提示,例如“输入行数:矩阵:”,然后使用scanf()orfgets等函数用于读取用户响应。
标签: c matrix queue breadth-first-search