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

Magic Labyrinth

Magic Labyrinth

There is a magic labyrinth. Very scary monsters live in this labyrinth. The labyrinth is divided on squares. Each square either is free or contains an obstacle. Free squares can be either dangerous or safe. Starting at some free square and moving only through free squares, your goal is to reach some safe square as soon as possible. Each move you make is "\textbf{U}", "\textbf{D}", "\textbf{L}", "\textbf{R}", denoting up, down, left, and right respectively. Nothing happens (your position does not change) if you move into an obstacle, or try to leave the labyrinth. Similarly, nothing happens if you try to move and you have already reached safe square (you never leave safe position). You know labyrinth structure well, but unfortunately you do not know which position you start at. Find the shortest sequence of moves, leading you to safe square, regardless of your initial position. \InputFile The first line of input contains two integers \textbf{M} and \textbf{N}, giving the number of rows and columns in the labyrinth respectively (\textbf{1} ≤ \textbf{M}, \textbf{N} ≤ \textbf{5}). Each of following \textbf{M} lines contains a string of length \textbf{N}, consisting of characters "\textbf{.}" (free danger square), "\textbf{*}" (free safe square) or "\textbf{#}" (obstacle). These strings specify the map of the labyrinth. \OutputFile Output a single line containing the shortest sequence of moves, leading to safe square. If there are multiple shortest sequences, output the one that is lexicographically smallest. If there is no valid solution, output "\textbf{NO SOLUTION}" instead.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
2 3
*..
*..
Çıxış verilənləri #1
LL
Mənbə ACM-ICPC Ukraine 2012, 2nd Stage Ukraine, September 6, 2012