#P9904. [COCI 2023/2024 #1] Labirint
[COCI 2023/2024 #1] Labirint
Background
Teo and the Croatian EJOI team are inside a maze.
Problem Description
The maze is an grid. The top-left cell is , and the bottom-right cell is . Between every pair of adjacent cells (4-neighborhood, i.e., adjacent up, down, left, right), there is a door. Doors have four colors: blue, red, green, and orange, represented by the characters P, C, Z, N, respectively. You can only move to other cells by passing through doors.
Now Teo gives you queries. Each query provides , asking you to find a path from to that minimizes the number of distinct door colors used along the path. You need to output this minimum number of colors.
Input Format
The first line contains two integers , representing the number of rows and columns of the grid.
Next is a character matrix with rows and columns. The character in row , column represents the color of the door between and .
Next is a character matrix with rows and columns. The character in row , column represents the color of the door between and .
Next is one line with an integer , representing the number of queries.
Then follow lines. The -th line contains four integers , meaning the minimum number of distinct door colors on a path from to .
Output Format
Output lines, each containing one integer, which is the answer to the corresponding query.
1 8
CPZNCCP
4
1 1 1 8
1 3 1 5
1 8 1 4
1 2 1 3
4
2
3
1
3 3
PP
PP
PP
CCC
CCC
3
1 1 3 3
3 3 2 2
1 1 1 3
2
2
1
4 4
CCC
CPC
PPP
CNP
ZZZZ
PPPP
CPZC
4
3 1 2 3
1 1 4 4
2 2 3 3
1 4 4 1
1
2
1
3
Hint
Sample Explanation #3
As shown in the figure.

- For the first query, you only need to pass through blue doors.
- For the second query, you only need to pass through blue and green doors.
- For the third query, you only need to pass through blue doors.
- For the fourth query, following the path in the figure, you only need to pass through red, blue, and green doors.
Constraints
For of the testdata, , , , , , and the character matrices consist only of the characters P, C, Z, N.
This problem uses bundled tests.
| Subtask | Special Property | Points |
|---|---|---|
The first character matrix contains only P, and the second character matrix contains only C |
||
All character matrices contain only C and P |
||
| No special properties |
Notes
The scoring of this problem follows the original COCI problem settings, with a full score of .
Translated from COCI2023-2024 CONTEST #1 T2 Labirint.
Translated by ChatGPT 5