猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

AOJ 2600: Koto Distance

解法

縦、横についていもす法するだけ。縦か横かどちらかカバーできていればOKということになる。

ソースコード

class Main extends MyUtil{
    public static void main(String[] args) throws Exception{
        // 入出力は自作クラスを使って行っている
        int n = readIntMap(0);
        int w = readIntMap(1);
        int h = readIntMap(2);

        int[] ww = new int[w+1];
        int[] hh = new int[h+1];
        
        for (int i = 0; i < n; i++) {
            int x = readIntMap(0);
            int y = readIntMap(1);
            int l = readIntMap(2);
            
            ww[Math.max(0, x-l)]++;
            ww[Math.min(w, x+l)]--;
            hh[Math.max(0, y-l)]++;
            hh[Math.min(h, y+l)]--;
        }
        
        if(check(ww) || check(hh)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
    
    static boolean check(int[] arr){
        int l = arr.length - 1;
        int sum = 0;
        for(int i = 0; i < l; i++){
            sum += arr[i];
            if(sum == 0) return false;
        }
        return true;
    }
}