Submit | All submissions | Best solutions | Back to list |
Problem hidden on 2013-07-09 22:56:01 by Mitch Schwartz
TOPLEFT - GAME7 |
Bob is playing with an M x N matrix. He can perform four kind of shift operations , top shift , bottom shift , left shift , right shift.
In left shift, he can move each of the column leftwards once, the first column becomes the last column.
In Right shift, he can move each of the column rightwards once, the last column becomes the first column.
In top shift, he can move each of the row upwards once, the first row becomes the last row.
In bottom shift, he can move each of the row downwards once, the last row becomes the first row.
He wants to perform some of the shifts to obtain a ‘value’ at the top left cell of the matrix. Compute and print the minimum number of shifts required to place the required value in the top left cell of the matrix.
Input
First line consits of T , no. of Test cases. First line of every test cases consists two integers , M and N. Next M lines consists of N integers , representing a M X N matrix. Last line of each test case consists of the required value that is to be obtained at the top left cell of the matrix.
Output
Print the minimum number of shifts required to place the required value in the top left cell of the matrix. Print -1 if it is not possible to obtain the required value in the top left cell.
Example
Input: 1
3 4
0 0 0 0
0 0 0 0
0 0 9 9
9 Output: 2
Added by: | [[soup_boy]] |
Date: | 2013-04-10 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | ASM32-GCC MAWK BC C-CLANG C NCSHARP CSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JAVA JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET |