#P6751. [IOI 2019] 视觉程序
[IOI 2019] 视觉程序
Description
You are writing a vision program for a robot. Each time the robot’s camera takes a photo, the image is stored in the robot’s memory as a black-and-white image. Each image is a grid of pixels of size . The rows are numbered from to , and the columns are numbered from to . Each image contains exactly two black pixels, and all other pixels are white.
The robot can process images using a program made of simple instructions. Given , , and a positive integer , your goal is to write a function that generates a program for the robot. This program must determine whether the distance between the two black pixels is exactly . Here, the distance between the pixel at row and column and the pixel at row and column is defined as . In this expression, denotes the absolute value of : it equals when , and equals when .
The robot works as follows.
The robot’s memory has enough cells, numbered starting from . Each cell can store or , and once its value is set, it cannot be changed. The image is stored in memory row by row, in cells numbered from to . The first row is stored in cells to , and the last row is stored in cells to . In particular, if the pixel at row and column is black, then the value stored in cell is ; otherwise it is .
The robot’s program is a sequence of instructions, numbered by consecutive integers starting from . While the program runs, instructions are executed one by one. Each instruction reads the values of one or more memory cells (we call these values the instruction’s inputs) and produces a value or (we call it the instruction’s output). The output of instruction is stored in cell . The inputs of instruction can only be memory cells that store the image, or memory cells that store outputs of previous instructions, i.e., cells numbered from to .
The robot has four types of instructions:
NOT: has exactly one input. If the input is , the output is ; otherwise the output is .AND: has one or more inputs. Its output is if and only if all inputs are .OR: has one or more inputs. Its output is if and only if at least one input is .XOR: has one or more inputs. Its output is if and only if the number of inputs equal to is odd.
If the distance between the two black pixels is exactly , then the output of the last instruction should be ; otherwise it should be .
Implementation Details
You need to implement the following function:
void construct_network(int H, int W, int K)
- , : the size of the image captured by the robot’s camera.
- : a positive integer.
- This function must generate a robot program. For every image captured by the robot’s camera, the program should determine whether the distance between the two black pixels is exactly .
The program should be generated by appending instructions to the robot’s program (initially, the robot’s program is empty) by calling the following functions:
int add_not(int N)
int add_and(int[] Ns)
int add_or(int[] Ns)
int add_xor(int[] Ns)
- These append one
NOT,AND,OR, orXORinstruction, respectively. - (for
add_not): the index of the input memory cell for theNOTinstruction to be appended. - (for
add_and,add_or,add_xor): an array of indices of input memory cells for theAND,OR, orXORinstruction to be appended. - Each call returns the index of the memory cell where the appended instruction’s output is stored. Consecutive calls will return consecutive integers starting from .
The robot’s program may contain at most instructions. In total, these instructions may read at most values. In other words, the sum of the lengths of the arrays over all add_and, add_or, and add_xor calls, plus the number of add_not calls, must not exceed .
After appending the last instruction, the function construct_network must return. The generated robot program will be evaluated on some images. For an image, the output of the last instruction is if and only if the distance between the two black pixels is exactly . If, for every image in a test point, the program generated by your solution outputs the correct result, then you pass that test point.
While evaluating your program, the grader may produce the following error messages:
Instruction with no inputs: an empty array was provided as input toadd_and,add_or, oradd_xor.Invalid index: an incorrect (possibly negative) memory cell index was provided as input toadd_not,add_or, oradd_xor.Too many instructions: your function attempted to add more than instructions.Too many inputs: the instructions in the program read more than values in total.
Example of a Grader Program
The example grader program reads input in the following format:
- Line : , , .
- Line (): , , , .
- The last line: .
Except for the first and the last line, each line represents an image containing two black pixels. Let the image described on line be image . In this image, one black pixel is at row and column , and the other black pixel is at row and column .
The example grader program first calls construct_network(H, W, K). If construct_network violates the constraints described in this statement, the example grader will output one of the error messages listed at the end of the Implementation Details section and terminate.
Otherwise, the example grader program outputs two parts.
First, it outputs the robot program’s results in the following format:
- Line (): the output ( or ) of the last instruction of the robot program for image .
Second, it writes the following format to a file named log.txt in the current directory:
- Line (): , , ,.
The sequence on line describes the data stored in memory after the robot program finishes running with image as input. Specifically, is the value stored in memory cell . Note that (the sequence length) equals plus the number of instructions in the robot program.
Hint
Sample
Suppose , , and . In this case, there are only two possible images where the distance between the two black pixels is .

- Case 1: the black pixels are and .
- Case 2: the black pixels are and .
One feasible solution is to construct the robot program using the following calls:
add_and([0, 5]): adds an instruction whose output is if and only if the image matches case 1. The output will be stored in memory cell .add_and([2, 3]): adds an instruction whose output is if and only if the image matches case 2. The output will be stored in memory cell .add_or([6, 7]): adds an instruction whose output is if and only if at least one of the above two cases holds.
Constraints
For all data:
- .
- .
- .
The additional constraints and scores for each subtask are as follows: | Subtask ID | Additional Constraints | Score | | :--------: | :--------------------: | :---: | | | | | | | | | | | | | | | | | | | | | | | In every image, the pixel in row and column is black | | | | | | | | No additional constraints | |
Translated by ChatGPT 5
京公网安备 11011102002149号