Pipes

All submissions for this problem are available.
In pipe games, your objective is to arrange pipes such that there is water flowing from a source to destination. In our variation of the game, there is an unlimited supply of water. Consider a grid of size N x M. In each cell of the grid, there is a pipe shape(the details are given below). You are able to rotate the pipe shapes 90 degrees clockwise or anticlockwise any number of times. You cannot move the pipe shapes from the cell. The source is from the left side of the top left cell and the destination is the right side of the bottom right cell. You have to make a minimum number of rotations such that a path exists from the source to the destination. Since there is an unlimited supply of water don’t worry about open ends. Note that water cannot flow from bottom to top since it defies gravity. Water can flow in all other directions. Print "1" if there is no way
The pipe shapes are: 1. Horizontal pipe(connects left and right) 2. Vertical pipe(connects top and bottom) 3. L shape1(connects top and left) 4. L shape2(connects top and right) 5. L shape3(connects bottom and left) 6. L shape4(connects bottom and right) 7. T shape1(connects all sides except top) 8. T shape2(connects all sides except right) 9. T shape3(connects all sides except down) 10. T shape4(connects all sided except left) 11. ‘+’ shape (connects all 4 sides) Pipes 1 and 2 can be obtained by rotating each other. Pipes 3, 4, 5 and 6 can be obtained by rotating each other. Pipes 7, 8, 9 and 10 can be obtained by rotating each other.
Input
T the number of test cases For each test case, 1st line contains two integers N and M. Then N lines follow each containing M integers. The integer A, at jth position of ith line represents a pipe shape as described above.
Output
Print an integer that is the correct answer followed by a new line.
Constraints
1 <= T <= 100 1 <= N, M <= 100 1 <= A <= 11 where A is the grid element.
Example
Input: 3 1 1 1 1 1 3 3 3 4 1 3 7 8 6 11 10 3 Output: 0 1 6
Explanation
For Case 1 and Case 2: Since the grid has only one cell, the source is the left side of the cell and destination is its right side.
Case 1: Horizontal pipe connecting left and right side is given, thus the water flows left to right and no rotation is needed.
Case 2: L shaped pipe is given which can never be used to connect left and right sides irrespective of rotations, hence output is 1.
Case 3: Look at image above. The pipe at the top left (0,0) is rotated twice [ 4 > 5 ] , the pipe at (1,0) is rotated once [ 7 > 10 ] , the pipe at (1,1) is rotated once [ 8 > 7 ] , the pipe at (1,2) is rotated once [ 6 > 5] and the pipe at the right bottom (2,2) is rotated once [ 3 > 4 ].
The minimum number of rotations is 2+1+1+1+1 = 6.
Author:  kopc_admin 
Tags  kopc_admin 
Date Added:  24012015 
Time Limit:  0.5 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, 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, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 