back to the menu

Flag Counter TopCoder SRM 574 - 1050: Tunnels

SRM 574 1 1050 Tunnels


Problem Statement

有一种神奇的地道,将它看成二维平面后,
所有的房屋都在一条地平线上,每条地道都是从房屋底部向下伸出的,
整个地下可以被分割成若干1*1的小方格,每条地道需要满足一些条件:

1. 每一对连续的方格共有一条边(连续的意思是向下延伸出来的顺序上是连续的);
2. 不连续的任意一对方格都不共边;
3. 地道的第一个方格必须刚好在地平线下方;
4. 地道可以往下,往左,往右,不能往上走,不能走重复方格。

同时,任意一对地道都不相交(也就是说没有一个方格同时属于两条地道),
而且不存在两条地道相邻(也就是说没有相邻的一对方格属于不同的地道)。

整个地图可以看成一个无限网格,现在给你截出中间的一个矩形部分,问你整个地图中最少的地道条数。

矩形的长宽<=50,给定的图案一定是合法的。

Preparing for the End of The World on 21.12.2012, Manao and his neighbours decided to dig several tunnels under their neighbourhood. All of the houses in Manao's neighbourhood are standing in a line and all of the tunnels are dug directly under them, so we can consider the neighbourhood's underground in two-dimensional space.



The underground is divided into squares 1 meter long. Each tunnel is a sequence of squares obeying the following rules:
  • Each pair of consecutive squares shares an edge.
  • No pair of non-consecutive squares shares an edge.
  • The first square in the sequence is right under the ground level.
  • If the squares are traversed from the first one to the last one, the direction in each pair will be either down, left or right. That is, a tunnel cannot contain an ascending fragment.

Note that each tunnel may have multiple squares directly below the ground level. It is also known that no two tunnels in the neighbourhood share a square and there are no two neighbouring square which belong to different tunnels. See the following examples of incorrectly built tunnel systems: in the first one, the tunnel does not begin right under the ground level; in the second one, a tunnel has an ascending fragment, in the third one, there are non-consecutive squares sharing an edge and in the fourth one, two tunnels have neighbouring squares.

**********GROUND**********
...   X...   X..   X....X
.X.   X...   XXX   XXXXXX
...   X.X.   .XX   ..XX..
...   XXX.   ...   ..X...

Suppose we have encoded the whole underground and have an infinite grid where each cell is either 'X', denoting a dug square, or '.', denoting an undug square. You are given some rectangular fragment of this infinite grid as String[] frame. It is guaranteed that the tunnels were built according to the given rules. Return the minimum possible number of tunnels in this underground.

Definition

(be sure your method is public)

Limits

Constraints

Test cases

  1. input
    • frame {"XXX.XXXX.....X",
       "..X....XXX...X",
       "XXX......X...."}
    output
    Returns 3
    note
    We see three tunnels in frame. Note that this fragment could either be right below the ground level or elsewhere in the deep.
  2. input
    • frame {".......X.....",
       ".............",
       "XXX.XXXXXXXXX"}
    output
    Returns 3
    note
    A fuller picture of the system could be the following:
    X.......X.....X
    X.............X
    XXXX.XXXXXXXXXX
    
  3. input
    • frame {".............",
       "XXXXXXXXXXXXX",
       ".............",
       "XXX.......XXX",
       "..........X..",
       "..........XXX"}
    output
    Returns 2
    note
    The given fragment could correspond to a system with only two tunnels. A possible picture is:
    2.1..............
    2.111111111111111
    2...............1
    222222.......1111
    .............1...
    .............111.
    

    Another possible picture is:
    ..............1.2
    111111111111111.2
    1...............2
    1111.......222222
    ...........2.....
    ...........222...
    
  4. input
    • frame {"XXXX...X..",
       "....XXXX.X",
       "XX.......X",
       "..........",
       "....XXXXXX"}
    output
    Returns 4
    note
    A possible corresponding picture is:
    ....1...2..3.4
    11111...2..3.4
    1....2222.33.4
    111.......3..4
    .............4
    .....444444444
    
  5. input
    • frame {"X........X..",
       ".........XXX",
       "............",
       "XXXXXXXXXXXX",
       "............",
       "XXXXXXXXXXXX",
       "............",
       ".........XXX",
       "..XXXXXXXX.."}
    output
    Returns 2
  6. input
    • frame {"...............X.X....X",
       "XXXXX..........X.......",
       "....X..................",
       "XXX.X.........XXXXXXXXX",
       "..X.X.........X........",
       "XXX.X.........XXXXXXXXX",
       "....X..................",
       "XXXXX......XXXXXXXXXXXX"}
    output
    Returns 5
  7. input
    • frame {".",
       "X",
       "X",
       ".",
       "X"}
    output
    Returns 1
  8. input
    • frame {"X.XX",
       "X...",
       "...X",
       "X...",
       "X..X"}
    output
    Returns 3
  9. input
    • frame {"...",
       "..."}
    output
    Returns 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.