BGC
TRUST IUPC 2012
Problem
E
Ant’s Shopping Mall
In the world of ant there is a popular shopping mall named
Ant’s Shopping Mall. The shopping mall is a grid with R rows and C
columns. Any cell of the grid can be occupied
by a shop or empty. The shopping mall is dynamic in a sense that if there is a
shop at cell (r, c) (where 1 <= r <= R and 1 <= c <= C, here r
defines the row and c defines the column of the cell) then in a single
move the shop can be moved to (r, c - 1)
or (r, c + 1) if the position is empty. But it cannot be moved outside the grid.
The ant queen now wants to visit the shopping mall. The queen can move vertically that is if the queen is at cell (r, c) then in the next step she can be at (r + 1, c). She has a special way of visit, she always starts from first row that is from any cell (1, c) and continues until the last row is reached that is cell (R, c). The owner of the shopping mall wants to impress queen so he wants to find a path of the form (1, c), (2, c), …, (R, c) for the queen in advance such that there is no shop in any cell of the path. For this the owner of the mall may need to move zero or more shops but he wants to do it in minimum number of shop movement. Now the owner hired you to solve the task for him. If it is not possible to find a path then you have to report it also.
Input
First line of the input contains a positive number T, number of test
cases. There will be at most 50 test cases. For each test case the first
line contains R and C separated by spaces (2 <= R <= 50 and 1
<= C <= 50). Each of the next R lines contains C characters, each of them is either 0 or 1. If the j-th
character in the i-th line contains 1 then cell (i, j) contains a shop otherwise cell (i, j) is empty.
Output
For each test you have to output minimum number of moves needed if it is
possible to generate a path for the queen. Otherwise output -1. See
output format for clarification.
Sample Input |
Output for Sample Input |
3 111 01111 |
Case 1: 1 Case 2: -1 Case 3: 4 |
Problemsetter: Kazi Rakibul Hasan
Special Thanks: Md. Towhidul Islam Talukder