#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 H×WH\times W. The rows are numbered from 00 to H1H-1, and the columns are numbered from 00 to W1W-1. 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 HH, WW, and a positive integer KK, 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 KK. Here, the distance between the pixel at row r1r_1 and column c1c_1 and the pixel at row r2r_2 and column c2c_2 is defined as r1r2+c1c2|r_1-r_2|+|c_1-c_2|. In this expression, x|x| denotes the absolute value of xx: it equals xx when x0x\ge0, and equals x-x when x<0x<0.

The robot works as follows.

The robot’s memory has enough cells, numbered starting from 00. Each cell can store 00 or 11, and once its value is set, it cannot be changed. The image is stored in memory row by row, in cells numbered from 00 to HW1H\cdot W-1. The first row is stored in cells 00 to W1W-1, and the last row is stored in cells (H1)W(H-1)W to HW1H\cdot W-1. In particular, if the pixel at row ii and column jj is black, then the value stored in cell iW+ji\cdot W+j is 11; otherwise it is 00.

The robot’s program is a sequence of instructions, numbered by consecutive integers starting from 00. 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 00 or 11 (we call it the instruction’s output). The output of instruction ii is stored in cell HW+iH\cdot W+i. The inputs of instruction ii can only be memory cells that store the image, or memory cells that store outputs of previous instructions, i.e., cells numbered from 00 to HW+i1H\cdot W+i-1.

The robot has four types of instructions:

  • NOT: has exactly one input. If the input is 00, the output is 11; otherwise the output is 00.
  • AND: has one or more inputs. Its output is 11 if and only if all inputs are 11.
  • OR: has one or more inputs. Its output is 11 if and only if at least one input is 11.
  • XOR: has one or more inputs. Its output is 11 if and only if the number of inputs equal to 11 is odd.

If the distance between the two black pixels is exactly KK, then the output of the last instruction should be 11; otherwise it should be 00.

Implementation Details

You need to implement the following function:

void construct_network(int H, int W, int K)
  • HH, WW: the size of the image captured by the robot’s camera.
  • KK: 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 KK.

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, or XOR instruction, respectively.
  • NN (for add_not): the index of the input memory cell for the NOT instruction to be appended.
  • NsNs (for add_and, add_or, add_xor): an array of indices of input memory cells for the AND, OR, or XOR instruction 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 HWH\cdot W.

The robot’s program may contain at most 10410^4 instructions. In total, these instructions may read at most 10610^6 values. In other words, the sum of the lengths of the NsNs arrays over all add_and, add_or, and add_xor calls, plus the number of add_not calls, must not exceed 10610^6.

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 11 if and only if the distance between the two black pixels is exactly KK. 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 to add_and, add_or, or add_xor.
  • Invalid index: an incorrect (possibly negative) memory cell index was provided as input to add_not, add_or, or add_xor.
  • Too many instructions: your function attempted to add more than 10410^4 instructions.
  • Too many inputs: the instructions in the program read more than 10610^6 values in total.

Example of a Grader Program

The example grader program reads input in the following format:

  • Line 11: HH, WW, KK.
  • Line 2+i2+i (i0i\ge0): r1ir1_i, c1ic1_i, r2ir2_i, c2ic2_i.
  • The last line: 1-1.

Except for the first and the last line, each line represents an image containing two black pixels. Let the image described on line 2+i2+i be image ii. In this image, one black pixel is at row r1[i]r_1[i] and column c1[i]c_1[i], and the other black pixel is at row r2[i]r_2[i] and column c2[i]c_2[i].

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 1+i1+i (0i0\le i): the output (11 or 00) of the last instruction of the robot program for image ii.

Second, it writes the following format to a file named log.txt in the current directory:

  • Line 1+i1+i (0i0\le i): mi,0m_{i,0}, mi,1m_{i,1}, \dotsmi,c1m_{i,c-1}.

The sequence on line 1+i1+i describes the data stored in memory after the robot program finishes running with image ii as input. Specifically, mi,jm_{i,j} is the value stored in memory cell jj. Note that cc (the sequence length) equals HWH\cdot W plus the number of instructions in the robot program.

Hint

Sample

Suppose H=2H=2, W=3W=3, and K=3K=3. In this case, there are only two possible images where the distance between the two black pixels is 33.

  • Case 1: the black pixels are 00 and 55.
  • Case 2: the black pixels are 22 and 33.

One feasible solution is to construct the robot program using the following calls:

  1. add_and([0, 5]): adds an instruction whose output is 11 if and only if the image matches case 1. The output will be stored in memory cell 66.
  2. add_and([2, 3]): adds an instruction whose output is 11 if and only if the image matches case 2. The output will be stored in memory cell 77.
  3. add_or([6, 7]): adds an instruction whose output is 11 if and only if at least one of the above two cases holds.

Constraints

For all data:

  • 1H,W2001\le H,W\le200.
  • 2HW2\le H\cdot W.
  • 1KH+W21\le K\le H+W-2.

The additional constraints and scores for each subtask are as follows: | Subtask ID | Additional Constraints | Score | | :--------: | :--------------------: | :---: | | 11 | max(H,W)3\max(H,W) \le 3 | 1010 | | 22 | max(H,W)10\max(H,W) \le 10 | 1111 | | 33 | max(H,W)30\max(H,W) \le 30 | 1111 | | 44 | max(H,W)100\max(H,W) \le 100 | 1515 | | 55 | min(H,W)=1\min(H,W) = 1 | 1212 | | 66 | In every image, the pixel in row 00 and column 00 is black | 88 | | 77 | K=1K = 1 | 1414 | | 88 | No additional constraints | 1919 |

Translated by ChatGPT 5