【问题标题】:How to make an interface to Rat in a maze problem?如何在迷宫问题中制作与老鼠的接口?
【发布时间】: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() or fgets等函数用于读取用户响应。

标签: c matrix queue breadth-first-search


【解决方案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");
  }
}

【讨论】:

  • 请多多解释与您的解决方案相关的内容。然后机器人不会将其标记为低质量答案。用户也不 ;-)
  • 感谢您的建议。我是这个平台的新手。这是我第一次回答任何问题。下次我在回答问题时会牢记您的建议。
  • 我尝试使用此代码,但无论何时使用它都找不到路径。
  • @PRANJALSINGH 只需通过编辑来更新您的答案。现在它只是一个转储,迟早会被标记。请解决 OP 问题。
  • edit出语法错误。
猜你喜欢
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2017-09-09
相关资源
最近更新 更多