学生方阵
标题:学生方阵 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
学校组织活动,将学生排成一个矩形方阵。请在矩形方阵中找到最大的位置相连的男生数量。这个相连位置在一个直线上,方向可以是水平的、垂直的、呈对角线的或者反对角线的。
注:学生个数不会超过10000.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
//矩阵行数和列数
String rowAndColumnStr = scanner.next();
String[] rowAndColumnStrArray = rowAndColumnStr.split(",");
int row = Integer.parseInt(rowAndColumnStrArray[0]);
int column = Integer.parseInt(rowAndColumnStrArray[1]);
//矩阵元素
String[][] matrix = new String[row][column];
for (int rowIndex = 0; rowIndex < row; ++rowIndex) {
String matrixLineStr = scanner.next();
String[] sexStrArray = matrixLineStr.split(",");
System.arraycopy(sexStrArray, 0, matrix[rowIndex], 0, sexStrArray.length);
}
//寻找最长的连线
int result = 0;
int currentResult;
for (int rowIndex = 0; rowIndex < row; ++rowIndex) {
for (int columnIndex = 0; columnIndex < column; ++columnIndex) {
if ("F".equals(matrix[rowIndex][columnIndex])) {
continue;
}
currentResult = findMaleLengthInMatrixHorizontal(matrix, rowIndex, columnIndex);
if (currentResult > result) {
result = currentResult;
}
currentResult = findMaleLengthInMatrixVertical(matrix, rowIndex, columnIndex);
if (currentResult > result) {
result = currentResult;
}
currentResult = findMaleLengthInMatrixLeftTop(matrix, rowIndex, columnIndex);
if (currentResult > result) {
result = currentResult;
}
currentResult = findMaleLengthInMatrixRightTop(matrix, rowIndex, columnIndex);
if (currentResult > result) {
result = currentResult;
}
}
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 寻找水平方向的最长连线
*/
private static int findMaleLengthInMatrixHorizontal(String[][] matrix, int currentRow,
int currentColumn) {
int result = 1;
int columnIndex = currentColumn - 1;
while (columnIndex > -1 && "M".equals(matrix[currentRow][columnIndex])) {
++result;
--columnIndex;
}
columnIndex = currentColumn + 1;
while (columnIndex < matrix[0].length && "M".equals(matrix[currentRow][columnIndex])) {
++result;
++columnIndex;
}
return result;
}
/**
* 寻找垂直方向的最长连线
*/
private static int findMaleLengthInMatrixVertical(String[][] matrix, int currentRow,
int currentColumn) {
int result = 1;
int rowIndex = currentRow - 1;
while (rowIndex > -1 && "M".equals(matrix[rowIndex][currentColumn])) {
++result;
--rowIndex;
}
rowIndex = currentRow + 1;
while (rowIndex < matrix.length && "M".equals(matrix[rowIndex][currentColumn])) {
++result;
++rowIndex;
}
return result;
}
/**
* 寻找左对角线方向的最长连线
*/
private static int findMaleLengthInMatrixLeftTop(String[][] matrix, int currentRow,
int currentColumn) {
int result = 1;
int rowIndex = currentRow - 1;
int columnIndex = currentColumn - 1;
while (rowIndex > -1 && columnIndex > -1 && "M".equals(matrix[rowIndex][columnIndex])) {
++result;
--rowIndex;
--columnIndex;
}
rowIndex = currentRow + 1;
columnIndex = currentColumn + 1;
while (rowIndex < matrix.length && columnIndex < matrix[0].length &&
"M".equals(matrix[rowIndex][columnIndex])) {
++result;
++rowIndex;
++columnIndex;
}
return result;
}
/**
* 寻找右对角线方向的最长连线
*/
private static int findMaleLengthInMatrixRightTop(String[][] matrix, int currentRow,
int currentColumn) {
int result = 1;
int rowIndex = currentRow - 1;
int columnIndex = currentColumn + 1;
while (rowIndex > -1 && columnIndex < matrix[0].length && "M".equals(matrix[rowIndex][columnIndex])) {
++result;
--rowIndex;
++columnIndex;
}
rowIndex = currentRow + 1;
columnIndex = currentColumn - 1;
while (rowIndex < matrix.length && columnIndex > -1 &&
"M".equals(matrix[rowIndex][columnIndex])) {
++result;
++rowIndex;
--columnIndex;
}
return result;
}
}