burn this image into the retina of my eye

Exclusive Easy+ 2 BasicsMachine Learning Meta

Implement ImgClassifier, which labels images based on training data using a simple heuristic.

You are given a small training set of labeled grayscale images. Each image is represented as a 2D array of integers. Your task is to classify new query images by finding the closest training image using squared Euclidean distance.

This problem is designed to be simple and deterministic—no machine learning libraries or advanced techniques are required.


Distance Definition

Given two images A and B of size H x W, their distance is defined as:

d(A,B)=r=0H1c=0W1(A[r][c]B[r][c])2d(A, B) = \sum_{r=0}^{H-1} \sum_{c=0}^{W-1} (A[r][c] - B[r][c])^2


Classification Rule

For each query image:

  1. Compute its distance to every training image.
  2. Find the training image with the minimum distance.
  3. Assign the query image the label of that training image.

Tie-breaking rules

If multiple training images have the same minimum distance:

  1. Choose the one with the smallest label (lexicographically or numerically).
  2. If still tied, choose the one with the smallest training index.

Input Format

H W
N
label_0
<image_0 row 0>
<image_0 row 1>
...
label_1
<image_1 row 0>
...
...
label_(N-1)
<image_(N-1) row H-1>
Q
<query_0 row 0>
...
<query_0 row H-1>
...
<query_(Q-1) row H-1>

  • H, W — image height and width
  • N — number of training images
  • Each training image has:
    • a label (string or integer)
    • H rows of W integers in [0, 255]
  • Q — number of query images
  • Each query image has H rows of W integers

Output Format

For each query image, output one line containing the predicted label.


Notes

  • Do not normalize pixel values.
  • Do not use feature extraction or learning.
  • Simply compute distances and apply the rules above.

Constraints

  • 1H,W201 ≤ H, W ≤ 20
  • 1N2001 ≤ N ≤ 200
  • 1Q2001 ≤ Q ≤ 200
  • Pixel values are integers in [0, 255]
Accepted 2/4
Acceptance 50%
Loading editor...
Sample Input:
2 3 3 cat 0 0 0 0 255 0 dog 0 0 0 255 0 255 owl 10 10 10 10 10 10 2 0 0 0 0 250 0 0 0 0 250 0 250
Expected Output:
cat dog