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 浙江

相关推荐

哞客37422655...:csol:来了他们来了
点赞 评论 收藏
分享
01-14 16:23
广州商学院 Java
双非后端失败第N人:如果准备好了可以直接投字节,字节是最不看学历的,只要想面,大概率都能给你约面。
双非有机会进大厂吗
点赞 评论 收藏
分享
评论
3
5
分享

创作者周榜

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