Submit | All submissions | Best solutions | Back to list |
MMAHWIRE - Manhattan Wire |
There is a rectangular area containing n × m cells. Two cells are marked with “2”, and another two with “3”. Some cells are occupied by obstacles. You should connect the two “2”s and also the two “3”s with non-intersecting lines. Lines can run only vertically or horizontally connecting centers of cells without obstacles.
Lines cannot run on a cell with an obstacle. Only one line can run on a cell at most once. Hence, a line cannot intersect with the other line, nor with itself. Under these constraints, the total length of the two lines should be minimized. The length of a line is defined as the number of cell borders it passes. In particular, a line connecting cells sharing their border has length 1.
Fig. 1(a) shows an example setting. Fig. 1(b) shows two lines satisfying the constraints above with minimum total length 18.
Figure 1: An example of setting and its solution
Input
The input consists of multiple datasets, each in the following format.
n m row1 … rown
n is the number of rows which satisfies 2 ≤ n ≤ 9. m is the number of columns which satisfies 2 ≤ m ≤ 9. Each rowi is a sequence of m digits separated by a space. The digits mean the following.
0: Empty 1: Occupied by an obstacle 2: Marked with “2” 3: Marked with “3”
The end of the input is indicated with a line containing two zeros separated by a space.
Output
For each dataset, one line containing the minimum total length of the two lines should be output. If there is no pair of lines satisfying the requirement, answer “0” instead.
Sample Input
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 0 0
Sample Output
18 2 17
Added by: | psetter |
Date: | 2009-02-22 |
Time limit: | 0.100s-1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | Yokohama 2006 |
hide comments
2012-04-22 18:16:19 Patrick Klitzke
I agree, my solution is quite fast, on most testcases it is a fraction of a second, but I get TLE. It is really not fun optimizing all day long... |
|
2012-04-21 14:59:32 Kliment Serafimov
this is the worst test case I found: 9 9 2 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 3 0 0 0 0 0 0 0 2 0 0 my program outputs the correct answer in 0.78 sec on the test case above, but when i send the solution it still says TLE. Can anyone tell me how many datasets contains the largest test case? |
|
2012-04-08 21:39:15 Buda IM (retired)
@Marko Stankovic No, for each testcase you can assign different TL |
|
2012-04-07 16:19:55 Marko Stankovic
Why is TL 1s-4s. Shouldn't it be a single value? |
|
2009-12-03 01:01:06 [Trichromatic] XilinX
Time limit too strict! |