Demo entry 6760389

Code

   

Submitted by anonymous on Sep 13, 2018 at 19:49
Language: Java. Code size: 737 Bytes.

public int[][] bomb(int h, int w, int count) {
        int[] nums = new int[h * w];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i;
        }
        int[] locs = new int[count];
        for (int i = 0; i < count; i++) {
            locs[i] = nums[i];
        }
        Random rand = new Random();
        for (int i = count; i < nums.length; i++) { 
            int j = rand.nextInt(i + 1);
            if (j < count) {
                locs[j] = nums[i];
            }
        }
        int[][] res = new int[h][w];
        for (int i = 0; i < count; i++) {
            int x = locs[i] / w;
            int y = locs[i] % w;
            res[x][y] = 1;
        }
        return res;
    }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).