burn this image into the retina of my eye
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:
Classification Rule
For each query image:
- Compute its distance to every training image.
- Find the training image with the minimum distance.
- Assign the query image the label of that training image.
Tie-breaking rules
If multiple training images have the same minimum distance:
- Choose the one with the smallest label (lexicographically or numerically).
- 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 widthN— number of training images- Each training image has:
- a label (string or integer)
Hrows ofWintegers in[0, 255]
Q— number of query images- Each query image has
Hrows ofWintegers
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
- 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 250Expected Output:
cat
dog