c - 不能在迷宫求解器中使用矩阵作为函数参数
问题描述
我正在尝试从作为命令行参数给出的 .dat 文件中解决迷宫问题。打印解决方案时,路径中的单元格应由“.”表示。我知道我将矩阵的大小定义为宏,即使我有 2 个函数来计算行和列,我这样做是为了能够将大小用作函数中的参数(我有一个可能的解决方案,但首先我想让它在当前状态下工作)。供参考:
# - wall
, S - starting point
, E - end point
, ' ' - free cell
, '.' - solution path
. .dat 文件中的迷宫:
################################################################################
#S # #
################################################################### # ######## #
# ####### # ## ## #
# ######################################################### # # ## ## ## #
# # #E # ####### # ## ## ## #
# # ##################################################### # # ## # ## #
# # ######### # ## ## ## #
# ####################################################### # # # ## ## ## #
# # # ##### # ## ## ## #
# # ####################################################### # # ### # ## ## ## #
# # # ######################### # # # ## ## ## #
# # # ###### ######################## ##### # ## ## ## #
# # # ## ### ####################### # ## ### ## ## # # ## ## ## #
# # # # # # ### ### ## #### # ## ## ## #
# # ## ############################# # ############################ # ## ## ## #
# # ## # ### # # # ## ## ## #
# # ## # ########### ############ ## ############################## ## ## ## #
# # # # # #### # ### #
# ################ # ####### ######### ############################## #### # #
# # # # # ########## #
######## ######### # ############################################## # #
# # ### ######
################################################################################
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000
#define HEIGHT 24
#define WIDTH 81
void storeMaze(int N, int M, int matrix[N][M], FILE *fp);
int countLines(FILE *fp);
int countColumns(FILE *fp);
int find_path(int x, int y, int maze[HEIGHT][WIDTH]);
void printMaze(int maze[HEIGHT][WIDTH]);
int start_x, start_y, end_x, end_y;
int main(int argc, char **argv)
{
if(argc != 2)
{
fprintf(stderr, "Invalid format.\nCorrect format: %s maze_file.dat\n", argv[0]);
exit(EXIT_FAILURE);
}
FILE *fin = fopen(argv[1], "rb");
if(!fin)
{
fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
exit(EXIT_FAILURE);
}
int N = countLines(fin); // lines in the matrix
rewind(fin);
int M = countColumns(fin); // columns in the matrix
rewind(fin);
// printf("Lines: %d | Columns: %d\n", N, M);
int maze[N][M];
// int sol[N][M];
storeMaze(N, M, maze, fin);
if(find_path(start_x, start_y, maze) == true)
{
printMaze(maze);
}
else
printf("No solution!\n");
// printf("Lines: %d | Columns: %d\n", N, M);//24 81
fclose(fin);
return 0;
}
void storeMaze(int N, int M, int matrix[N][M], FILE *fp)
{
int z=0, i;
char buf[1000];
while (fgets(buf, sizeof buf, fp))
{
for(i = 0; buf[i] != '\n' && z < N; i++)
{
if(buf[i] == '#')
{
matrix[z][i] = 0;
putchar('#');
}
else if(buf[i] == ' ')
{
matrix[z][i] = 1;
putchar(' ');
}
else if(buf[i] == 'S')
{
start_x = z;
start_y = i;
matrix[z][i] = 2;
putchar('S');
}
else if(buf[i] == 'E')
{
end_x = z;
end_y = i;
matrix[z][i] = 3;
putchar('E');
}
//printf("%d ", matrix[z][i]);
}
putchar('\n');
z++;
}
putchar('\n');
printf("The starting point: (%d,%d)\n", start_x, start_y);
printf("The end point: (%d,%d)\n", end_x, end_y);
}
int countLines(FILE *fp)
{
char c;
int count = 0;
while((c = fgetc(fp)) != EOF)
{
if (c == '\n') // Increment count if this character is newline
{
count++;
}
}
return count;
}
int countColumns(FILE *fp)
{
char c;
int count = 0;
while((c = fgetc(fp)) != EOF)
{
if (c == '\n') // Increment count if this character is newline
{
break;
}
count++;
}
return count;
}
int find_path(int x, int y, int maze[HEIGHT][WIDTH])
{
// If x,y is outside maze, return false.
if ( x < 0 || x > HEIGHT - 1 || y < 0 || y > WIDTH - 1 )
return false;
// If x,y is the goal, return true.
if ( maze[y][x] == 3 ) // 'E' - end point
return true;
// If x,y is not open, return false.
if ( maze[y][x] != 1 && maze[y][x] != 2 )
return false;
// Mark x,y part of solution path.
maze[y][x] = '.';
// If find_path North of x,y is true, return true.
if ( find_path(x, y - 1, maze) == true )
return true;
// If find_path East of x,y is true, return true.
if ( find_path(x + 1, y, maze) == true )
return true;
// If find_path South of x,y is true, return true.
if ( find_path(x, y + 1, maze) == true )
return true;
// If find_path West of x,y is true, return true.
if ( find_path(x - 1, y, maze) == true )
return true;
// Unmark x,y as part of solution path.
maze[y][x] = 0; // mark this as a wall
return false;
}
void printMaze(int maze[HEIGHT][WIDTH]) {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
printf("%d ", maze[i][j]);
}
putchar('\n');
}
}
这是将文件中的迷宫正确存储到矩阵中,它为开始和结束获取正确的坐标。我通常不使用矩阵,所以我有点困惑为什么会这样。如果我运行代码,没有段错误或任何东西,只是:
################################################################################
#S # #
################################################################### # ######## #
# ####### # ## ## #
# ######################################################### # # ## ## ## #
# # #E # ####### # ## ## ## #
# # ##################################################### # # ## # ## #
# # ######### # ## ## ## #
# ####################################################### # # # ## ## ## #
# # # ##### # ## ## ## #
# # ####################################################### # # ### # ## ## ## #
# # # ######################### # # # ## ## ## #
# # # ###### ######################## ##### # ## ## ## #
# # # ## ### ####################### # ## ### ## ## # # ## ## ## #
# # # # # # ### ### ## #### # ## ## ## #
# # ## ############################# # ############################ # ## ## ## #
# # ## # ### # # # ## ## ## #
# # ## # ########### ############ ## ############################## ## ## ## #
# # # # # #### # ### #
# ################ # ####### ######### ############################## #### # #
# # # # # ########## #
######## ######### # ############################################## # #
# # ### ######
################################################################################
The starting point: (1,1)
The end point: (5,5)
No solution!
知道我该如何解决这个问题吗?先感谢您!
解决方案
推荐阅读
- php - 安装 phppgadmin 并使用 localhost (php 7) 运行
- python - 对时间字符串范围列表进行排序
- java - 如何使用 Spark java 从 mariadb 读取数据
- javascript - 图例点击事件在迭代后不起作用
- javascript - 我在 MERN Stack 上处理我的项目时遇到了问题
- asp.net - 在 ajax 请求之前调用的表单操作
- android - Android上的RSA解密中的IllegalBlockSizeException“null”
- ssl - 一个 istio-ingressgateway 和多个 TLS 网关
- regex - 如何使用 pprof 优化 CSV 加载器?
- c# - 为什么 Insert() 方法会删除零