Medu Vada Maze

All submissions for this problem are available.
A maze, for our purposes, consists of a rectangular grid of cells, where some of the cells are blocked and the remaining cells are empty. A mouse is placed in one of the empty cells and a dosa is placed at a different empty cell. From a cell, the mouse can move to any empty neighbouring cell according to the rules given below. Your aim is to determine whether the mouse can walk through this maze and reach the dosa. A maze with $R$ rows and $C$ columns will be presented as a sequence of $R$ lines, each with $C$ characters. The character # denotes a blocked cell and the character . denotes an empty cell). The mouse and the dosa sit in distinct empty cells and those cells are marked by M and D instead of . Here is a maze with $6$ rows and $12$ columns. $ $ ####.#..#### .M.#..#..D.# #.#...#....# ...#..#..#.. ...#.#.###.# .........### $ $ We will refer to a cell by its row and column position. The rows are numbered from top to bottom and the columns from left to right. For example, in the maze above, the mouse is initially placed at position ($2$,$2$) indicating second row and second column and the dosa is at position ($2$,$10$) indicating second row and tenth column. The mouse can move from a cell to the one above it, the one below it, the one to its left or the one to its right. Further, from a cell in the leftmost column the mouse can move to the cell in the same row in the rightmost column and from a cell in the rightmost column it can move to the cell in the same row in the leftmost column. Similarly, from a cell in the top row the mouse can move to the cell in the same column in the bottom row and from a cell in the bottom row it can move to the cell in the same column in the top row. Thus, in the maze above, the mouse can move from ($4$,$1$) to ($4$,$12$) and from ($6$,$5$) to ($1$,$5$) and so on. You should think of the maze as having the shape of a solid ring, like a Medu Vada. It is as if the paper on which the Maze is drawn is rolled back and stuck at the edge so that the bottom row touches the top row. The resulting cylinder is rolled around so that the right and left columns, which now form circles, touch each other. Thus, the left most column and right most column touch each other, as do the top row and the bottom row. In the example above, the mouse can reach the dosa by walking through the following sequence of cells: ($2$,$2$), ($3$,$2$), ($4$,$2$), ($4$,$1$), ($4$,$12$), ($4$,$11$), ($3$,$11$), ($3$,$10$) and ($2$,$10$). This path is marked with with `x`'s below: $ $ ####.#..#### .M.#..#..D.# #x#...#..xx# xx.#..#..#xx ...#.#.###.# .........### $ $ Alternatively it can also take the following path: $ $ ####.#.x#### .M.#..#xxD.# #x#...#....# .x.#..#..#.. .x.#.#.###.# .xxxxxxx.### $ $ Your task is to determine whether it possible for the mouse to reach the dosa. ###Input: The first line of the input will contain two numbers $R$ and $C$ denoting the number and rows and columns respectively. This is followed by $R$ lines, each with $C$ characters, each of which is # or . or M or D. There is exactly one M and one D in the input. ###Output: If there is no path from the mouse to the dosa print a single line containing the word NO. If there are paths from the mouse to the dosa, then the first line of the output should consist of the word YES. Following this must be R lines of C characters each, where each character is # or . or x or M or D, describing a path from M to D using the x's as illustrated above. There may be many paths and it suffices to describe one path. ###Constraints:  $1 \leq R \leq 1000$.  $1 \leq C \leq 1000$.  In $80 \%$ of the inputs, $3 \leq R \leq 60$ and $3 \leq C \leq 60$. ###Sample input 1: 6 12 ####.#..#### .M.#..#..D.# #.#...#....# ...#..#..#.. ...#.#.###.# .........### ###Sample output 1: YES ####.#..#### .M.#..#..D.# #x#...#..xx# xx.#..#..#xx ...#.#.###.# .........### ###Sample input 2: 4 6 ##.#.. .M.#.# #.#.D. ...#.# ###Sample output 2: NOAuthor:  admin2 
Date Added:  17092018 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions