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;
    }
}

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

相关推荐

点赞 评论 收藏
转发
3 5 评论
分享
正在热议
牛客网
牛客企业服务