eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Help the Museum

Help the Museum

The National Association of Museum Curators came to you with an interesting problem. The President of the country, in order to improve his public image, has decided to visit the various Art Museums in the country, to convey the impression that he is a refined man. Being a very busy person, however, and knowing nothing about art, he imposed two restrictions for the visits: \begin{enumerate} \item In each museum, he wants to see the works of one and only one artist, so that he can easily prepare himself for the visit and pose as an art connoisseur. However he does not necessarily have to see all of the works of that artist. \item He does not want to waste time, and demands to walk through the exposition following the shortest possible path. \end{enumerate} The curators are willing to follow the President’s demands, but they do not want to redistribute the masterpieces in the exposition only to obtain a straight path. Their only concession is to exchange temporarily the place of two masterpieces, if this helps to obtain a shorter path. You should write a program that receives as input the layout of an exposition and finds the shortest path, according to the above constraints. To make your task easier, the curators have already defined a standard layout. \textit{\textbf{Figure 1}} shows one such layout. \includegraphics{https://static.e-olymp.com/content/d0/d067fc858ee447719300ebc4281d3bf03ec48cb7.jpg} \textit{\textbf{Figure 1}}: Layout of the Museum The President’s walk begins always at the left wall (\textbf{X = 1}, any \textbf{Y}) and ends at the right wall (\textbf{X = X_max}, any \textbf{Y}). The walk can be done horizontally or vertically; diagonal movements are not allowed. The works of a given artist are all labeled with the same capital letter (\textbf{A}, \textbf{B}, \textbf{C}, etc). From \textit{\textbf{Figure 1}}, several cases can be illustrated: \begin{enumerate} \item If the president wants to see the works of artist \textbf{A}, there is not a path from left to right. Such a path can be obtained if the work by artist \textbf{B} at \textbf{(6, 6)} is exchanged with one by artist \textbf{A} from \textbf{(1, 8)}, \textbf{(7, 8)}, \textbf{(8, 8)}, \textbf{(10, 6)},\textbf{(11, 6)} or \textbf{(11, 3)}. \item If the president wants to see the works of artist \textbf{B}, there is already a path, beginning at \textbf{(1, 10)} and ending at \textbf{(11, 5)}. A shorter path can be obtained, if the work of \textbf{D} at \textbf{(11, 7)} is exchanged with one work by artist \textbf{B}, for example from \textbf{(10, 6)}. \item If the president wants to see the works of artist \textbf{C}, there is already a straight path from \textbf{(1, 1)} to \textbf{(1, 11)}, and a shorter path cannot be obtained. \item If the president wants to see the works of \textbf{D}, \textbf{E} or \textbf{F}, there is no possibility to obtain a path from left to right. \end{enumerate} \InputFile The input file may contain several instances of the problem. Each instance has the following format (all numbers are positive integers): \begin{enumerate} \item The first line contains the integers \textbf{X_max} and \textbf{Y_max}, the layout dimensions. You may assume that \textbf{1} ≤ \textbf{X_max},\textbf{Y_max} ≤ \textbf{100}. \item The second line contains the artist (upper-case) letter that will have his/her works visited. \item \textbf{Y_max} lines, each with \textbf{X_max} letters (without spaces between them). The first input line corresponds to index \textbf{Y_max}, the second line to index \textbf{Y_max-1}, and so on, until the last line, that corresponds to index \textbf{1}. \end{enumerate} A line containing two zeros terminates the input file. Numbers are separated by spaces. \OutputFile For each instance of the problem, your program should produce output as follows. If a path exists, your program should first print one line with the message "\textbf{Exchange (x,y) and (u,v)}" if an exchange will occur, or "\textbf{No exchange}" otherwise. Following that, the shortest path should be printed, one coordinate per line. In case more than one path is the shortest, any one of them may be printed, except that a path without an exchange should be preferred to those with exchanges. If a path does not exist, your program should print only one line with the message "\textbf{No path}". The output of each instance is terminated with a blank line.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
11 10
A
BBBBBBFFFFF
AAAAABDCCFF
AFFFABAACFC
BFEFABBBBBD
FFDEABAAABA
EEDEEEEEABB
DDDEEEEEAAB
DCCFFFCCABA
DCCFFFCCAAA
CCCCCCCCCCC
11 10
C
BBBBBBFFFFF
AAAAABDCCFF
AFFFABAACFC
BFEFABBBBBD
FFDEABAAABA
EEDEEEEEABB
DDDEEEEEAAB
DCCFFFCCABA
DCCFFFCCAAA
CCCCCCCCCCC
0 0
Выходные данные #1
Exchange (6,6) and (1,8)
1 9
2 9
3 9
4 9
5 9
5 8
5 7
5 6
6 6
7 6
8 6
9 6
9 5
9 4
9 3
9 2
10 2
11 2

No exchange
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
Источник ACM International Collegiate Programming Contest, Summer Camp (2nd Day), Tokyo, 2005–9–24