Under construction

Still being built. Things may change

Vision Program

Time limit1sMemory limit1024 MB

Summary
Build a Boolean circuit of NOT, AND, OR, and XOR gates that outputs 1 when the two black pixels in an H by W image are exactly K apart.
Level

Medium7 of 10

Topics
Implementation, Brute force, Math
Solved
No attempts yet

Problem

Each time the robot camera takes a picture, the image is stored as a black and white image in the robot's memory. Each image is an H×WH \times W grid, with rows numbered 00 through H−1H-1 and columns numbered 00 through W−1W-1. There are exactly two black pixels in each image, and all other pixels are white.

The robot can process each image with a program made of simple instructions. You are given HH, WW, and a positive integer KK. Write a procedure that produces a program which, for any image, determines whether the distance between the two black pixels is exactly KK. The distance between the pixel in row r1r_1, column c1c_1 and the pixel in row r2r_2, column c2c_2 is ∣r1−r2∣+∣c1−c2∣|r_1-r_2|+|c_1-c_2|, where ∣x∣|x| equals xx if x≥0x \geq 0 and −x-x if x<0x < 0.

The robot's memory is a sufficiently large array of cells indexed from 00. Each cell stores either 00 or 11, and once set, its value does not change. The image is stored row by row in cells 00 through H⋅W−1H \cdot W - 1. The first row is stored in cells 00 through W−1W-1, and the last row is stored in cells (H−1)⋅W(H-1) \cdot W through H⋅W−1H \cdot W - 1. If the pixel in row ii and column jj is black, cell i⋅W+ji \cdot W + j holds 11; otherwise it holds 00.

A robot program is a sequence of instructions numbered consecutively from 00. The instructions run one by one. Each instruction reads one or more cells (its inputs) and produces a single output, 00 or 11. The output of instruction ii is stored in cell H⋅W+iH \cdot W + i. The inputs of instruction ii can only be cells 00 through H⋅W+i−1H \cdot W + i - 1, which hold pixels or outputs of earlier instructions.

There are four instruction types:

  • NOT: exactly one input. The output is 11 if the input is 00, otherwise 00.
  • AND: one or more inputs. The output is 11 if and only if all inputs are 11.
  • OR: one or more inputs. The output is 11 if and only if at least one input is 11.
  • XOR: one or more inputs. The output is 11 if and only if an odd number of inputs are 11.

The output of the last instruction must be 11 if the distance between the two black pixels is exactly KK, and 00 otherwise.

Constraints

  • 1≤H≤2001 \leq H \leq 200
  • 1≤W≤2001 \leq W \leq 200
  • 2≤H⋅W2 \leq H \cdot W
  • 1≤K≤H+W−21 \leq K \leq H+W-2

Examples1

  1. Example 1

    Input
    1 2 1
    
    Expected output
    2
    AND 0 1
    OR 2