Problem: monsters

Problem IDmonsters
Difficulty level (0..10)4
Maximum runtime1 minute
Original problemgreece96_problem1.html
Seen atFirst Hellenic Internet Programming Contest

Monsters

Two monsters are placed in a NxN maze. The good monster is placed at the top left corner, whereas the evil one is placed at the bottom right corner of the maze. Each maze cell contains either a 1 or a 0. The monsters walk only up, down, left and right (NOT diagonally), in cells which contain a 1. The monsters are met, if they stand on the same cell. You must find the minimum number of steps each monster should perform in order to meet. Note, that at each turn, both monsters should perform a step (i.e. it is not allowed for a monster to stay at the same position, while the other has performed a step). Also, depending on the maze content, the two monsters may never meet.

Input
Your program should read the input data from standard input as follows:

Output

Your program should write the output to the standard output as follows:

Sample input:

5
1 1 1 1 0
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1

Sample output:

4
DDDD
LLLL

Sample input:

5
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0

Sample output:

NOPATH