3D Space Exploration

Interview

Time limit1sMemory limit128 MB

Summary
Count connected groups of '*' blocks in an N x N x N grid, where blocks connect only across shared faces.
Level

Easy3 of 10

Topics
Graph, DFS, BFS, Implementation
Solved
No attempts yet

Problem

Farmer John's cows have finally blasted off from Earth and are now drifting through space in their Moocraft. To reach their kin on Io, a moon of Jupiter, they must first cross a dangerous asteroid belt.

Bessie is piloting the craft through an N×N×NN \times N \times N (1≤N≤1001 \le N \le 100) sector of space. Every asteroid in the sector is a group of 1×1×11 \times 1 \times 1 blocks of space-rock joined face to face. Two blocks that touch only along an edge, or only at a single corner, belong to different asteroids.

Count the number of distinct asteroids in the sector.

The sector is given as NN square slices stacked one behind another (front to back). Each slice is an N×NN \times N grid in which * marks a block of space-rock and . marks empty space. A block at slice ss, row rr, column cc shares a face with the blocks at (s±1,r,c)(s\pm1, r, c), (s,r±1,c)(s, r\pm1, c), and (s,r,c±1)(s, r, c\pm1), and blocks joined this way belong to the same asteroid.

For example, consider a 3×3×33 \times 3 \times 3 sector. Here M marks the Moocraft's start at (1,1,1)(1,1,1) and D marks the destination at (3,3,3)(3,3,3); these markers only help orient the map and are not needed to solve the problem.

   Close slice    Middle slice     Far Slice     Assembled with overlaps
     +---+            +---+          +---+                  +---+ Far
     |M..|            |..*|          |...|                  |...|
     |.*.|            |.*.|          |.*.|                +---+.|
     |...|            |*..|          |..D|                |..*|D|
     +---+            +---+          +---+              +---+.|-+
                                                        |M..|.|
                                                        |.*.|-+
                                                        |...|
                                                  Close +---+

Visual inspection shows three asteroids, including a long one running straight through the middle of the sector. The next figure labels each block with the number of the asteroid it belongs to:

                                 +---+ Far
                                 /|...|
                                / |.1.|
                               /  |...|
                              /   +---+
                             +---+   /
                            /|..3|  /
                           / |.1.| /
                          /  |2..|/
                         /   +---+
                        +---+   /
                        |...|  /
                        |.1.| /
                        |...|/
                  Close +---+

Input

  • The first line contains a single integer NN (1≤N≤1001 \le N \le 100).
  • The next N2N^2 lines describe the NN slices in order. Each slice is given as NN lines of exactly NN characters, where each character is * (a block of space-rock) or . (empty space).
  • Counting after the first line, lines s⋅N+1s \cdot N + 1 through s⋅N+Ns \cdot N + N (for s=0,1,…,N−1s = 0, 1, \dots, N-1) form slice ss. Within a slice the lines are its rows and the characters within a line are its columns, so the same row and column of two adjacent slices are aligned and share a face.

Output

  • Print a single integer: the number of distinct asteroids in the sector.

Examples1

  1. Example 1

    Input
    3
    ...
    .*.
    ...
    ..*
    .*.
    *..
    ...
    .*.
    ...
    
    Expected output
    3