eolymp
bolt
Try our new interface for solving problems
Məsələlər

Mad Veterinarian

Mad Veterinarian

Mad Veterinarian puzzles have a mad veterinarian, who has developed several machines that can transform an animal into one or more animals and back again. The puzzle is then to determine if it is possible to change one collection of animals into another by applying the machines in some order (forward or reverse). For example: \textit{Machine }\textit{\textbf{A}}\textit{ turns one ant into one beaver.} \textit{Machine }\textit{\textbf{B}}\textit{ turns one beaver into one ant, one beaver and one cougar.} \textit{Machine }\textit{\textbf{C}}\textit{ turns one cougar into one ant and one beaver.} \textit{Can we convert a beaver and a cougar into }\textit{\textbf{3}}\textit{ ants?} \textit{Yes. \{b,c\} }\textit{\textbf{C}}\textit{ → \{a,2b\} }\textit{\textbf{A}}\textit{ reversed → \{2a,b\} }\textit{\textbf{A}}\textit{ reversed → 3a} \textit{Can we convert one ant into }\textit{\textbf{2}}\textit{ ants? }\textit{\textbf{NO}} These puzzles have the properties that: \begin{enumerate} \item In forward mode, each machine converts one animal of a given species into a finite, non-empty collection of animals from the species in the puzzle. \item Each machine can operate in reverse. \item There is one machine for each species in the puzzle and that machine (in forward mode) takes as input one animal of that species. \end{enumerate} Write a program to find the shortest solution (if any) to Mad Veterinarian puzzles. For this problem we will restrict to Mad Veterinarian puzzles with exactly three machines, \textbf{A}, \textbf{B}, \textbf{C}. \InputFile The first line of input contains a single integer \textbf{P}, (\textbf{1} ≤ \textbf{P} ≤ \textbf{1000}), which is the number of data sets that follow. Each data set consists of several lines of input. Each data set should be processed identically and independently. The first line of each data set consists of two decimal integers separated by a single space. The first integer is the data set number. The second integer is the number, \textbf{N}, of puzzle questions. The next three input lines contain the descriptions of machines \textbf{A}, \textbf{B} and \textbf{C} in that order. Each machine description line consists of three decimal integers separated by spaces giving the number of animals of type \textbf{a}, \textbf{b} and \textbf{c} output for one input animal. The following \textbf{N} lines give the puzzle questions for the Mad Veterinarian puzzle. Each contains seven decimal digits separated by single spaces: the puzzle number, the three starting animal counts for animals \textbf{a}, \textbf{b} and \textbf{c} followed by the three desired ending animal counts for animals \textbf{a}, \textbf{b} and \textbf{c}. \OutputFile For each input data set there are multiple lines of output. The first line of output for each data set contains the data set number, a space and the number of puzzle questions (\textbf{N}). For each puzzle question, there is one line of output which consists of the puzzle question number followed by a space, followed by "\textbf{NO SOLUTION}" (without the quotes) if there is no solution \textbf{OR} the puzzle question number followed by the shortest number of machine steps used, a space and a sequence of letters \textbf{\[A B C a b c\]} with capital letters indicating applying the machine in the forward direction and lower case tetters indicating applying the machine in the reverse direction.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 32 MiB
Giriş verilənləri #1
2
1 2
0 1 0
1 1 1
1 1 0
1 0 1 1 3 0 0
2 1 0 0 2 0 0
2 2
0 3 4
0 0 5
0 0 3
1 2 0 0 0 0 5
2 2 0 0 0 0 4
Çıxış verilənləri #1
1 2
1 3 aCa
2 NO SOLUTION
2 2
1 NO SOLUTION
2 25 AcBcccBccBcccAccBccBcccBc