C 无法将矩阵用作迷宫解算器中的函数参数
我试图从作为命令行参数给出的.dat文件中解决一个迷宫。打印解决方案时,路径中的单元格应以“.”表示。我知道我将矩阵的大小定义为宏,即使我有两个函数来计算行和列,我这样做是为了能够在函数中使用大小作为参数(我有一个可能的解决方案,但首先我想让它在当前状态下工作)。供参考:C 无法将矩阵用作迷宫解算器中的函数参数,c,C,我试图从作为命令行参数给出的.dat文件中解决一个迷宫。打印解决方案时,路径中的单元格应以“.”表示。我知道我将矩阵的大小定义为宏,即使我有两个函数来计算行和列,我这样做是为了能够在函数中使用大小作为参数(我有一个可能的解决方案,但首先我想让它在当前状态下工作)。供参考: #-墙,S-起点,E-终点,'-自由单元,。-解决方案路径。 .dat文件中的迷宫: ######################################################################
#-墙
,S-起点
,E-终点
,'-自由单元
,。-解决方案路径
。
.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');
}
}
你知道我该怎么解决这个问题吗?提前谢谢你
int-maze[HEIGHT][WIDTH]
和char-maze[WIDTH][HEIGHT]
是两个截然不同的东西。@n.“代词是m。”。是的,那一定是滑倒了。我已经更新了代码和帖子,现在没有任何警告,但是输出是一样的,没有解决方案:/now是学习使用调试器的时候了。如果不这样做,请在代码中“有趣”的位置插入一些printf()
,以跟踪变量。请务必仔细阅读:
################################################################################
#S # #
################################################################### # ######## #
# ####### # ## ## #
# ######################################################### # # ## ## ## #
# # #E # ####### # ## ## ## #
# # ##################################################### # # ## # ## #
# # ######### # ## ## ## #
# ####################################################### # # # ## ## ## #
# # # ##### # ## ## ## #
# # ####################################################### # # ### # ## ## ## #
# # # ######################### # # # ## ## ## #
# # # ###### ######################## ##### # ## ## ## #
# # # ## ### ####################### # ## ### ## ## # # ## ## ## #
# # # # # # ### ### ## #### # ## ## ## #
# # ## ############################# # ############################ # ## ## ## #
# # ## # ### # # # ## ## ## #
# # ## # ########### ############ ## ############################## ## ## ## #
# # # # # #### # ### #
# ################ # ####### ######### ############################## #### # #
# # # # # ########## #
######## ######### # ############################################## # #
# # ### ######
################################################################################
The starting point: (1,1)
The end point: (5,5)
No solution!