#include <stdio.h> #include <stdlib.h>
#define MAX_N 101
// 存储路径 struct Path { int row[MAX_N * MAX_N]; int col[MAX_N * MAX_N]; int length; int value; };
// 输入矩阵 void inputMatrix(int n, int matrix[][MAX_N]) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &matrix[i][j]); }
// 输入起点和终点的行和列 void inputStartEnd(int *startRow, int *startCol, int *endRow, int *endCol) { scanf("%d%d%d%d", startRow, startCol, endRow, endCol); }
// 向上搜索最大价值路径 struct Path searchMaxValuePath(int n, int matrix[][MAX_N], int startRow, int startCol, int endRow, int endCol) { struct Path path = {0}; int value = matrix[startRow][startCol]; path.row[0] = startRow; path.col[0] = startCol; path.length = 1; path.value = value;
while (startRow > endRow)
{
// 从当前位置向左上、上、右上三个方向搜索
int maxValue = 0;
int maxRow = startRow, maxCol = startCol;
for (int i = -1; i <= 1; i++)
{
int row = startRow - 1;
int col = startCol + i;
// 注意要判断越界
if (col >= 0 && col < n && matrix[row][col] > maxValue)
{
maxValue = matrix[row][col];
maxRow = row;
maxCol = col;
}
}
// 更新路径信息
value += maxValue;
path.row[path.length] = maxRow;
path.col[path.length] = maxCol;
path.length++;
path.value = value;
startRow = maxRow;
startCol = maxCol;
}
return path;
}
// 输出路径信息 void printPath(struct Path path) { printf("%d ", path.value); for (int i = 0; i < path.length; i++) printf("(%d,%d) ", path.row[i], path.col[i]); putchar('\n'); }
int main(void) { int n, startRow, startCol, endRow