1 条题解
-
0
方法思路
- 记忆化DFS:使用深度优先搜索(DFS)遍历每个点,计算从该点出发的最长滑道长度。
- 动态规划优化:使用记忆化存储(
memo
数组)避免重复计算,提高效率。 - 方向处理:对于每个点,检查四个方向(上、下、左、右)是否可以滑行(即高度更低)。
代码实现
Java
import java.io.*; import java.util.*; public class Main { private static int[][] heights; private static int[][] memo; private static int R, C; private static final int[][] DIRS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // Up, Down, Left, Right public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] rc = br.readLine().trim().split(" "); R = Integer.parseInt(rc[0]); C = Integer.parseInt(rc[1]); heights = new int[R][C]; memo = new int[R][C]; for (int i = 0; i < R; i++) { String[] line = br.readLine().trim().split(" "); for (int j = 0; j < C; j++) { heights[i][j] = Integer.parseInt(line[j]); memo[i][j] = -1; } } int maxLength = 0; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { maxLength = Math.max(maxLength, dfs(i, j)); } } System.out.println(maxLength); } private static int dfs(int row, int col) { if (memo[row][col] != -1) { return memo[row][col]; } int maxPath = 1; // Start with length 1 (current cell) int currentHeight = heights[row][col]; for (int[] dir : DIRS) { int newRow = row + dir[0]; int newCol = col + dir[1]; if (newRow >= 0 && newRow < R && newCol >= 0 && newCol < C && heights[newRow][newCol] < currentHeight) { maxPath = Math.max(maxPath, 1 + dfs(newRow, newCol)); } } memo[row][col] = maxPath; return maxPath; } }
Python
def main(): R, C = map(int, input().split()) heights = [] for _ in range(R): heights.append(list(map(int, input().split()))) memo = [[-1 for _ in range(C)] for _ in range(R)] dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right def dfs(row, col): if memo[row][col] != -1: return memo[row][col] max_path = 1 # Start with length 1 (current cell) for dr, dc in dirs: new_row, new_col = row + dr, col + dc if (0 <= new_row < R and 0 <= new_col < C and heights[new_row][new_col] < heights[row][col]): max_path = max(max_path, 1 + dfs(new_row, new_col)) memo[row][col] = max_path return max_path max_length = 0 for i in range(R): for j in range(C): max_length = max(max_length, dfs(i, j)) print(max_length) if __name__ == "__main__": main()
C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; int R, C; vector<vector<int>> heights; vector<vector<int>> memo; const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // Up, Down, Left, Right int dfs(int row, int col) { if (memo[row][col] != -1) { return memo[row][col]; } int maxPath = 1; // Start with length 1 (current cell) for (int i = 0; i < 4; i++) { int newRow = row + dirs[i][0]; int newCol = col + dirs[i][1]; if (newRow >= 0 && newRow < R && newCol >= 0 && newCol < C && heights[newRow][newCol] < heights[row][col]) { maxPath = max(maxPath, 1 + dfs(newRow, newCol)); } } memo[row][col] = maxPath; return maxPath; } int main() { cin >> R >> C; heights.resize(R, vector<int>(C)); memo.resize(R, vector<int>(C, -1)); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { cin >> heights[i][j]; } } int maxLength = 0; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { maxLength = max(maxLength, dfs(i, j)); } } cout << maxLength << endl; return 0; }
- 1
信息
- ID
- 34
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者