IOI '19 P5 - Vision Program
View as PDFYou are implementing a vision program for a robot. Each time the robot camera takes a
picture, it is stored as a black and white image in the robot's memory. Each image is an 
grid of pixels, with rows numbered 
 through 
 and columns numbered 
through 
. There are exactly two black pixels in each image, and all other pixels
are white.
The robot can process each image with a program consisting of simple instructions.
You are given the values of , 
, and a positive integer 
. Your goal is to write a
procedure to produce a program for the robot that, for any image, determines whether
the distance between the two black pixels is exactly 
. Here, the distance between a
pixel in row 
 and column 
 and a pixel in row 
 and column 
 is 
.
In this formula 
 denotes the absolute value of 
, which equals 
 if 
 and equals
 if 
.
We now describe how the robot works.
The robot's memory is a sufficiently large array of cells, indexed from . Each cell can
store either 
 or 
 and its value, once set, will not be changed. The image is stored row
by row in cells indexed 
 through 
. The first row is stored in cells 
 through 
,
and the last row is stored in cells 
 through 
. In particular,
if the pixel in row 
 and column 
 is black, the value of cell 
 is 
, otherwise it is
.
A robot's program is a sequence of instructions, which are numbered with
consecutive integers starting from . When the program is run, the instructions are
executed one by one. Each instruction reads the values of one or more cells (we call
these values the instruction's inputs) and produces a single value equal to 
 or 
 (we
call this value the instruction's output). The output of instruction 
 is stored in cell 
.
The inputs of instruction 
 can only be cells that store either pixels or
outputs of previous instructions, i.e. cells 
 to 
.
There are four types of instructions:
: has exactly one input. Its output is
if the input is
, otherwise its output is
.
: has one or more inputs. Its output is
if and only if all of the inputs are
.
: has one or more inputs. Its output is
if and only if at least one of the inputs is
.
: has one or more inputs. Its output is
if and only if an odd number of the inputs are
.
The output of the last instruction of the program should be  if the distance between
the two black pixels is exactly 
, and 
 otherwise.
Implementation details
You should implement the following procedure:
void construct_network(int H, int W, int K)
: dimensions of each image taken by the robot's camera
: a positive integer
- This procedure should produce a robot's program. For any image taken by the
robot's camera, this program should determine whether the distance between the
two black pixels in the image is exactly 
.
 
This procedure should call one or more of the following procedures to append instructions to the robot's program (which is initially empty):
int add_not(int N)
int add_and(std::vector<int> Ns)
int add_or(std::vector<int> Ns)
int add_xor(std::vector<int> Ns)
- Append a 
,
,
, or
instruction, respectively.
 (for
add_not): the index of the cell from which the appendedinstruction reads its input
(for
add_and,add_or,add_xor): array containing the indices of the cells from which the appended,
, or
instruction reads its inputs
- Each procedure returns the index of the cell that stores the output of the
instruction. The consecutive calls to these procedures return consecutive integers
starting from 
.
 
The robot's program can consist of at most  instructions. The instructions can
read at most 
 values in total. In other words, the total length of 
 arrays in
all calls to 
add_and, add_or and add_xor plus the number of calls to add_not cannot
exceed .
After appending the last instruction, procedure construct_network should return. The
robot's program will then be evaluated on some number of images. Your solution
passes a given test case if for each of these images, the output of the last instruction is 
if and only if the distance between the two black pixels in the image is equal to 
.
The grading of your solution may result in one of the following error messages:
Instruction with no inputs: an empty array was given as the input toadd_and,add_or, oradd_xor.Invalid index: an incorrect (possibly negative) cell index was provided as the input toadd_and,add_or,add_xor, oradd_not.Too many instructions: your procedure attempted to add more thaninstructions.
Too many inputs: the instructions read more thanvalues in total.
Example
Assume . There are only two possible images where the distance
between the black pixels is 
.
- Case 
: black pixels are
and
 - Case 
: black pixels are
and
 
A possible solution is to build a robot's program by making the following calls:
add_and({0, 5}), which adds an instruction that outputsif and only if the first case holds. The output is stored in cell
.
add_and({2, 3}), which adds an instruction that outputsif and only if the second case holds. The output is stored in cell
.
add_or({6, 7}), which adds an instruction that outputsif and only if one of the cases above holds.
Constraints
Subtasks
- (
points)
 - (
points)
 - (
points)
 - (
points)
 - (
points)
 - (
points) Pixel in row
and column
is black in each image.
 - (
points)
 - (
points) No additional constraints.
 
Sample grader
The sample grader reads the input in the following format:
- line 
:
 - line 
:
 - last line: 
 
Each line excepting the first and the last line represents an image with two black
pixels. We denote the image described in line  by image 
. One black pixel is in
row 
 and column 
 and the other one in row 
 and column 
.
The sample grader first calls construct_network(H, W, K). If construct_network
violates some constraint described in the problem statement, the sample grader prints
one of the error messages listed at the end of Implementation details section and exits.
Otherwise, the sample grader produces two outputs.
First, the sample grader prints the output of the robot's program in the following format:
- line 
: output of the last instruction in the robot's program for image
(
or
).
 
Second, the sample grader writes a file log.txt in the current directory in the
following format:
- line 
:
 
The sequence on line  describes the values stored in the robot's memory cells
after the robot's program is run, given image 
 as the input. Specifically, 
 gives
the value of cell 
. Note that the value of 
 (the length of the sequence) is equal to 
plus the number of instructions in the robot's program.
Comments