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

Power Grid

Power Grid

After the untimely demise of its previous tenant, you have found yourself promoted to the position of the new "chief architect" of the Death Star by none other than Lord Vader himself. Your have been tasked with designing the wiring system for distributing power throughout each sector of the Death Star. Knowing that your life depends on your success, you have decided to be extremely meticulous and exhaustively enumerate all valid wiring configurations so that you may choose the best one. You are given a map of the Death Star, in the form of an \textbf{m}×\textbf{n} grid, whose cells correspond to sectors. Exactly one of the sectors in the map is a power station; the remaining sectors are all either living quarters or storage units. You have the option of placing a connection between any two non-storage sectors that are either vertically or horizontally adjacent. A valid wiring configuration is one such that: \begin{itemize} \item Every living quarter sector is either directly or indirectly connected to the power station sector, and \item As few connections are used as possible (i.e., removing any connection would result in disconnecting some living quarter from the power station). \end{itemize} For a given map, how many valid wiring configurations exist? \InputFile The input will contain multiple test cases. Each test case begins with a single line containing a pair of integers \textbf{m }and \textbf{n} (where \textbf{1} ≤ \textbf{m} ≤ \textbf{8} and \textbf{1} ≤ \textbf{n} ≤ \textbf{8}). The next \textbf{m} lines each contain \textbf{n} characters, representing the map of the Death Star. The map will contain exactly one sector marked as a power station (\textbf{P}); the remainder of the sections will be either living quarters (\textbf{.}) or storage units (\textbf{#}). You are guaranteed that there exists at least one valid wiring configuration. The end-of-input is denoted by a single line containing "\textbf{0 0}" and should not be processed. \OutputFile For each input test case, print the number of valid wiring configurations \textbf{mod} \textbf{1000000000}. \includegraphics{https://static.e-olymp.com/content/3a/3a2aa95a66d177e38d0bb38b1205246fa4e2f518.jpg}
Лимит времени 3 секунды
Лимит использования памяти 256 MiB
Входные данные #1
2 2
..
.P
4 5
#####
#.P.#
#...#
#####
3 4
....
P#..
...#
6 6
######
##..##
#....#
#..P.#
##..##
######
0 0
Выходные данные #1
3
15
31
768
Источник ACM ICPC Regionals 2009: North America - Pacific Northwest