The Islands
题号:NC207598
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

III. Reading Comprehension (In this section, you are going to read a short passage. Afterwards, you should write a program to answer the sole and only question. Remember that every word in the short passage matters!)

In this problem, you are given an n × m map consisting of '.' and '#', where '.' and '#' represent water and land, respectively. The first and the last rows, and the first and the last columns are called the border, and the border is guaranteed to be water. Water can flow in up, down, left and right, four directions, and water cannot flow into the lands. Similarly, two lands are connected if they share a common edge, i.e., one land appears above, below, on the left side, or on the right side of another land. If land A is connected with land B, and land B is connected with land C, then land A is connected with land C. Furthermore, we can say land A, land B, and land C are all connected. Connected lands form an island.

It is possible that some water cannot flow to the border, i.e., the water is trapped by the island(s). The islands that trap any water form the surroundings. The following map shows an example [Example 1]. The water in the center is trapped by the surroundings.

[Example 1]
........
..####..
.#....#.
.#.##.#.
.#....#.
..####..
........

There are people living in the islands. These people can only travel to the other islands in the water. Consequently, people in some islands can never go to the border directly, since the water near the islands may all be trapped. In [Example 1], people in the island in the center is trapped by the surroundings, i.e., they have to go to the trapped water first, and then to the land of the surroundings, and finally, leave for the border. We call the islands in the trapped water trapped as well. The surroundings, the trapped water, and the trapped island(s) (if there are any), together form a country. In [Example 1], there is one country. And, in [Example 2], there are three countries.

[Example 2]
.........................
..####.....####....####..
.#....#...#....#..#....#.
.#.##.##.##.##.#..#....#.
.#....#...#....#..#....#.
..####.....####....####..
.........................

Let the coordinates of two lands be (x1, y1), (x2, y2). The Manhattan distance between them is |x1 - x2| + |y1 - y2|, where |x| is the absolute value of x. If the Manhattan distance between ANY PIECE OF LAND from one country and ANY PIECE OF LAND from another country is no greater than k, these two countries will form an alliance. Analogously, if country A is in alliance with country B, and country B is in alliance with country C, then country A is also in alliance with country C. And, we can say country A, country B, and country C form one alliance. In [Example 2], if k = 2, there are two alliances. If k = 3, there is only one alliance.

Since we are going to be counting, we need to avoid some possible ambiguities:

  • Two surroundings are considered to be different if and only if they do not share any common islands. In [Example 3], there are two parts where water is trapped. However, there is only one surroundings -- two parts share the common island in the center. So, there is only one country in [Example 3].
    [Example 3]
    ..........
    ..##..##..
    .#..##..#.
    .#..##..#.
    ..##..##..
    ..........
    
  • Any islands that are neither trapped nor being parts of any surroundings are countries. In [Example 4], there are 4 islands and each of them is a country, i.e., there are 4 countries in [Example 4].
    [Example 4]
    .........
    ..#####..
    .#.....#.
    .##..##..
    .........
    
    Why are there 4??? Look at the explanation below.
    (1)       (2)
    #####     #
    
    (3)       (4)
    #         ##
    ##        
    

Now, given k, you are going to tell us the number of alliances in the given map.

HINT: In the last Sample Input, there is only one country. So there is only one alliance.

输入描述:

The first line of the input contains 3 positive integers n, m, k. (1 ≤ n, m, k ≤ 1000)
The following n lines describe the map. There will be m characters in each of them, consisting of '.' and '#' only.

输出描述:

Output one integer, which is the number of alliances in the given map.
示例1

输入

复制
7 25 1
.........................
..####.....####....####..
.#....#...#....#..#....#.
.#.##.##.##.##.#..#.##.#.
.#....#...#....#..#....#.
..####.....####....####..
.........................

输出

复制
3
示例2

输入

复制
7 25 2
.........................
..####.....####....####..
.#....#...#....#..#....#.
.#.##.##.##.##.#..#.##.#.
.#....#...#....#..#....#.
..####.....####....####..
.........................

输出

复制
2
示例3

输入

复制
7 25 3
.........................
..####.....####....####..
.#....#...#....#..#....#.
.#.##.##.##.##.#..#.##.#.
.#....#...#....#..#....#.
..####.....####....####..
.........................

输出

复制
1
示例4

输入

复制
7 11 1
...........
..##.###...
.#..#...#..
..##..#..#.
.#..#...#..
..##.###...
...........

输出

复制
1