Biggest square in a grid

10

1

Challenge

Given a grid like this,

  1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . # . . . # . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . # . . . . .
7 . . . . . . . .
8 . . . . . . . .

write a piece of code that can determine the size of the largest square that does not include a '#'. (The answer to this input is 5x5 as the bottom right 5x5 grid is the biggest square possible).

The square must have sides parallel to the x and y axes.

As some small details: the original grid is always a square and its side length is given to you. The coordinates of the '#' symbols are also given to you.

Input Details

First line: N (1 <= N <= 1000), the side length of the square grid, and T (1 <= T <= 10,000) the number of '#' signs.

Next T Lines: The coordinates of each of the T #'s

Test Cases

Input #1:

8 3
2 2
2 6
6 3

Result #1: 5

================

Input #2:

8 4
1 1
1 8
8 1
8 8

Result #2: 6

================

Input #3:

5 1
3 3

Result #3: 2

This is a problem, so the fastest code tested on the rextester compiler wins.

Have fun!

NL628

Posted 2018-01-02T03:38:16.403

Reputation: 673

Question was closed 2018-02-20T10:06:06.867

1Related. – user202729 – 2018-01-02T03:57:26.007

3For fastest-code 1000x1000 is too small, though – l4m2 – 2018-01-02T04:07:23.383

1But rextester doesn't support Jelly or Hexagony. – user202729 – 2018-01-02T04:08:48.320

@user202729 I guess you meet memory lack first – l4m2 – 2018-01-02T04:09:20.067

6

As capable as rextester is, may I recommend using try it online instead? It contains many more langauges, and is community run.

– ATaco – 2018-01-02T04:10:17.450

maybe TIO as pretest? – l4m2 – 2018-01-02T04:12:37.077

1"the fastest code tested on the rextester compiler wins" - fastest on what input? – Nathaniel – 2018-02-12T05:45:28.907

@user202729 rextester should have exactly the same problems. – Dennis – 2018-02-20T10:05:58.897

Answers

1

Node.js

Takes input as (w, l), where w is the width and l is an array of coordinates [x, y]. (This may be changed if the input format really is as strict as described.) Works in O(w²).

f = (w, l) => {
  var x, y,
      W = w * w,
      a = new Uint16Array(W),
      best = 0;

  l.forEach(([x, y]) => a[(y - 1) * w + x - 1] = 1);

  for(y = w; y < W; y += w) {
    for(x = y + 1; x < y + w; x++) {
      if(a[x]) {
        a[x] = 0;
      }
      else {
        best = Math.max(
          best,
          a[x] = Math.min(a[x - 1], a[x - w], a[x - w - 1]) + 1
        );
      }
    }
  }

  return best;
}

Try it online!

Arnauld

Posted 2018-01-02T03:38:16.403

Reputation: 111 334

e> 1000, [...Array(10000)].map(_=>[Math.random()*1000+1|0,Math.random()*1000+1|0]) )); ``` cost 114ms, though it might be the language's low efficiency – l4m2 – 2018-01-03T06:29:07.980

It's more like 8ms inside f() after JIT compilation. (But yeah... answers are not going to be scored that way.)

– Arnauld – 2018-01-03T11:54:04.857

I doubt the OP will add a more objective winning criteria... – user202729 – 2018-01-04T00:27:36.257

1

C (gcc)

No fancy algorithm here, just almost-bruteforce... but hey, C is fast.

Input: Takes input from stdin.

Output: Writes output to stdout.

#include <stdio.h>
#include <stdint.h>

int main(void) {
    uint_fast16_t n, t, i, j, h, x, y, flag, biggest = 0;
    scanf("%hi %hi", &n, &t);
    uint_fast8_t m[n][n];
    for (uint_fast16_t c = 0; c < t; ++c) {
        scanf("%hi %hi", &i, &j);
        m[i-1][j-1] = '#';
    }
    for (i = 0; i < n - 1; ++i) {
        for (j = 0; j < n - 1; ++j) {
            flag = 1;
            for (h = 1; flag && i + h < n + 1 && j + h < n + 1; ++h) {
                for (y = i; flag && y < i + h; ++y) {
                    for (x = j; flag && x < j + h; ++x) {
                        if (m[y][x] == '#') flag = 0;
                    }
                }
                if (flag && h > biggest) biggest = h;
            }
        }
    }
    printf("%d", biggest);
}

Try it online!

Marco

Posted 2018-01-02T03:38:16.403

Reputation: 251