学生方阵

标题:学生方阵 | 时间限制: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;
    }
}



全部评论

相关推荐

我的人生算是废了,23届裸辞空档一年,存款只能坚持几个月了,找不到像样的工作了,人生何去何从。
梦想是成为七海千秋:这大环境下为什么要裸辞呀,风险真的挺大的,而且社招的话23届没有太多的竞争力,不过既然已经裸辞了就不要焦虑慢慢找。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 18:18
点赞 评论 收藏
分享
能干的三文鱼刷了100道题:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务