我正试图找到通向迷宫的最短路径。输入是起点,终点和迷宫本身。我有2个问题:
1)我收到消息“变量”周围的堆栈已损坏,我看不到原因。
2)我尝试计算到达获胜点的最小步骤数,但看起来像是获得堆栈溢出。
代码:
void test()
{
int n;
char board[MIN_GRID][MAX_GRID];
int startX, startY, goalX, goalY, result;
printf("Please enter the number of rows and columns (n):\n");
scanf("%d", &n);
if (n < MIN_GRID || n> MAX_GRID)
{
return;
}
printf("Please enter the X of the starting position:\n");
scanf("%d", &startX);
if (startX < MIN_START_X || startX> MAX_START_X)
{
return;
}
printf("Please enter the Y of the starting position:\n");
scanf("%d", &startY);
if (startY < MIN_START_Y || startY> MAX_START_Y)
{
return;
}
printf("Please enter the X of the goal position:\n");
scanf("%d", &goalX);
if (goalX < MIN_GOAL_X || goalX> MAX_GOAL_X)
{
return;
}
printf("Please enter the Y of the goal position:\n");
scanf("%d", &goalY);
if (goalY < MIN_GOAL_Y || goalY> MAX_GOAL_Y)
{
return;
}
printf("Please enter the grid:\n");
loadCastleBoard(board, n, n); // Reading the Board
result = getMinLength(board, startX, startY, goalX, goalY, n,0); // Getting the Minimum Value
if (result != -1)
{
printf("The maximum number of steps is %d\n", result);
}
else
{
printf("No path was found from (%d,%d) to (%d,%d)\n", startX, startY, goalX, goalY);
}
}
int getMinLength(char mat[][MAX_GRID],int startX,int startY, int goalX, int
goalY, int n,int counter)
{
int newWay,oldWay,fastestSteps=9999;
if (startX > n - 1 || startY > n - 1 || startX < 0 || startY < 0)
{
return -1;
}
if (startY == goalY && startX == goalX)
{
return counter;
}
if (mat[startX][startY] == 88)
{
return -1;
}
mat[startX][startY] = 88;
counter++;
//check right
if (getMinLength(mat, startX + 1, startY,goalX,goalY, n,counter)==counter);
{
return counter;
}
//check left
if (getMinLength(mat, startX -1, startY, goalX, goalY, n,counter)==counter);
{
return counter;
}
//check up
if (getMinLength(mat, startX, startY+1, goalX, goalY, n, counter)==counter);
{
return counter;
}
//check down
if (getMinLength(mat, startX, startY-1, goalX, goalY, n, counter)==counter);
{
return counter;
}
mat[startX][startY] = 45;
return -1;
}
void loadCastleBoard(char mat[][MAX_GRID], int rows, int cols)
{
int i;
for (i = 0;i < cols;i++)
{
scanf("%s", mat[i]);
}
printf("\n");
}
非常感谢