Under construction

Still being built. Things may change

Manhattan Wiring

Time limit1sMemory limit128 MB

Summary
Find two vertex-disjoint shortest simple paths on a small grid connecting two pairs of marked cells while avoiding obstacles, minimizing total path length or returning 0 if impossible.
Level

Hard8 of 10

Topics
Graph, BFS, Brute force
Solved
No attempts yet

Problem

You are given a rectangular grid of n×mn \times m cells. Exactly two cells are marked "2" and exactly two other cells are marked "3"; some cells are obstacles.

Connect the two "2" cells with one line, and the two "3" cells with another line. A line runs through the centers of non-obstacle cells, moving only horizontally or vertically between orthogonally adjacent cells.

The rules are:

  • A line may not pass through an obstacle cell.
  • Each cell may be used by at most one line. Hence the two lines never share a cell and never cross.
  • The length of a line is the number of cell borders it crosses; a line joining two adjacent cells has length 11.

Make the sum of the two line lengths as small as possible and output that minimum. If the two pairs cannot both be connected under these rules, the answer is 00.

For example, in the first grid of the first sample below, both pairs can be connected with a minimum total line length of 1818.

Input

The first line contains the number of data sets TT.

The first line of each data set contains the number of rows nn and the number of columns mm. (2≤n≤92 \le n \le 9, 2≤m≤92 \le m \le 9)

The next nn lines describe the grid from the top row to the bottom row; each line contains mm integers separated by spaces. The meaning of each integer is:

  • 00: empty cell
  • 11: obstacle
  • 22: a cell marked "2"
  • 33: a cell marked "3"

Every grid contains exactly two cells marked "2" and exactly two cells marked "3".

Output

For each data set, print on its own line the minimum possible sum of the two line lengths. Print 00 if the two pairs cannot both be connected.

Examples1

  1. Example 1

    Input
    7
    5 5
    0 0 0 0 0
    0 0 0 3 0
    2 0 2 0 0
    1 0 1 1 1
    0 0 0 0 3
    2 3
    2 2 0
    0 3 3
    6 5
    2 0 0 0 0
    0 3 0 0 0
    0 0 0 0 0
    1 1 1 0 0
    0 0 0 0 0
    0 0 2 3 0
    5 9
    0 0 0 0 0 0 0 0 0
    0 0 0 0 3 0 0 0 0
    0 2 0 0 0 0 0 2 0
    0 0 0 0 3 0 0 0 0
    0 0 0 0 0 0 0 0 0
    9 9
    3 0 0 0 0 0 0 0 2
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0 3
    9 9
    0 0 0 1 0 0 0 0 0
    0 2 0 1 0 0 0 0 3
    0 0 0 1 0 0 0 0 2
    0 0 0 1 0 0 0 0 3
    0 0 0 1 1 1 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    9 9
    0 0 0 0 0 0 0 0 0
    0 3 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 2 3 2
    
    Expected output
    18
    2
    17
    12
    0
    52
    43