8.12 京东笔试第三题

简单说来就是固定一个点,枚举找第二个点,可以固定所有点

感谢牛客的大佬们的安慰和帮助

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int m = in.nextInt();
        int n = in.nextInt();
        char[][] chess = new char[m][n];
        for(int i = 0;i<m;i++){
            String s = in.next();
            chess[i] = s.toCharArray();
        }
        int res = 0;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(chess[i][j] == 'X'){
                    res += cal(chess,i,j);
                }
            }
        }
        System.out.println(res);
    }

    private static int cal(char[][] chess, int i, int j) {
        int m = chess.length;
        int n = chess[0].length;
        int res = 0;
        for(int x = 1;i + x < m && j + x < n;x++){
            if (chess[i + x][j + x] == 'X' && chess[i + x][j] == 'X' && chess[i][j + x] == 'X')res++;
        }
        for(int ni = 1;ni+i<m;ni++){
            if(j - ni < 0)break;
            for(int nj = 1;nj+j<n;nj++){
                if(i+ni+nj >= m || j + nj-ni >= n)break;
                if (chess[i + ni][j + nj] == 'X' && chess[i + nj][j - ni] == 'X' && chess[i+ni+nj][j + nj-ni] == 'X')res++;
            }
        }
        return res;
    }
}

全部评论
可以看看第二题代码吗,大佬,dfs超时,dp没思路😭
1 回复 分享
发布于 2023-08-12 22:52 湖北
两种情况,一种传统一种菱形,每个点要么是左上要么是最上,接下来去遍历剩下三个点,我不知道这思路哪错了
1 回复 分享
发布于 2023-08-12 21:35 浙江

相关推荐

点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
3
5
分享

创作者周榜

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