2025 A卷 200分 题型
本专栏内全部题目均提供Java、python、JavaScript、C、C++、GO六种语言的最佳实现方式;
并且每种语言均涵盖详细的问题分析、解题思路、代码实现、代码详解、3个测试用例以及综合分析;
本文收录于专栏:《2025华为OD真题目录+全流程解析+备考攻略+经验分享》
学校组织活动,将学生排成一个矩形方阵。请在矩形方阵中找到最大的位置相连的男生数量。这个相连位置在一个直线上,方向可以是水平的、垂直的、成对角线的或者呈反对角线的。
注:学生个数不会超过10000。
输入的第一行为矩阵的行数和列数,接下来的n行为矩阵元素,元素间用“,”分隔。
输出一个整数,表示矩阵中最长的位置相连的男生个数。
输入
3,4
F,M,M,F
F,M,M,F
F,F,F,M
输出
3
说明:对角线方向存在连续3个男生(M),为最长连续线段。
我们需要在给定的学生矩阵中找到最长的连续男生(‘M’)线段,方向可以是水平、垂直、主对角线(左上到右下)或反对角线(右上到左下)。我们需要遍历所有可能的方向,计算每个方向上的最长连续’M’的长度,取最大值。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取行数和列数
String[] firstLine = scanner.nextLine().split(",");
int rows = Integer.parseInt(firstLine[0]);
int cols = Integer.parseInt(firstLine[1]);
// 读取矩阵数据
char[][] matrix = new char[rows][cols];
for (int i = 0; i < rows; i++) {
String line = scanner.nextLine();
String[] elements = line.split(",");
for (int j = 0; j < cols; j++) {
matrix[i][j] = elements[j].charAt(0);
}
}
int max = 0; // 记录最大连续长度
// 处理水平方向
for (int i = 0; i < rows; i++) {
int current = 0;
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
}
}
// 处理垂直方向
for (int j = 0; j < cols; j++) {
int current = 0;
for (int i = 0; i < rows; i++) {
if (matrix[i][j] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
}
}
// 处理主对角线方向(右下)
// 第一行的每个列作为起点
for (int j = 0; j < cols; j++) {
int x = 0, y = j;
int current = 0;
while (x < rows && y < cols) {
if (matrix[x][y] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
x++;
y++;
}
}
// 第一列的每个行(除第一个)作为起点
for (int i = 1; i < rows; i++) {
int x = i, y = 0;
int current = 0;
while (x < rows && y < cols) {
if (matrix[x][y] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
x++;
y++;
}
}
// 处理反对角线方向(左下)
// 第一行的每个列作为起点
for (int j = 0; j < cols; j++) {
int x = 0, y = j;
int current = 0;
while (x < rows && y >= 0) {
if (matrix[x][y] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
x++;
y--;
}
}
// 最后一列的每个行(除第一个)作为起点
for (int i = 1; i < rows; i++) {
int x = i, y = cols - 1;
int current = 0;
while (x < rows && y >= 0) {
if (matrix[x][y] == 'M') {
current++;
max = Math.max(max, current);
} else {
current = 0;
}
x++;
y--;
}
}
System.out.println(max);
}
}
matrix
中。示例1:
输入:
3,4
F,M,M,F
F,M,M,F
F,F,F,M
输出:3
解析:对角线方向存在三个连续的’M’。
示例2:
输入:
4,4
M,M,M,M
F,F,F,F
M,M,M,M
F,F,F,F
输出:4
解析:水平方向每行有四个连续的’M’。
示例3:
输入:
2,2
M,M
M,M
输出:2
解析:四个方向上的连续’M’长度均为2。
我们需要在给定的学生矩阵中找到最长的连续男生(‘M’)线段,方向可以是水平、垂直、主对角线(左上到右下)或反对角线(右上到左下)。目标是遍历所有方向,找到最长连续’M’的长度。
def main():
import sys
# 读取输入
lines = [line.strip() for line in sys.stdin if line.strip()]
rows, cols = map(int, lines[0].split(','))
matrix = []
for line in lines[1:rows+1]:
matrix.append(line.split(','))
max_len = 0 # 记录最大连续长度
# 处理水平方向
for i in range(rows):
current = 0
for j in range(cols):
if matrix[i][j] == 'M':
current += 1
max_len = max(max_len, current)
else:
current = 0
# 处理垂直方向
for j in range(cols):
current = 0
for i in