info

MAPS OS v1.1.4 © 2025

Glory to the Segtree

Glory to the Algorithm.

3D MazeGo home

Login to view input

no submission

Login to submit an answer

Problem Leaderboard

Problem Leaderboard

3D Maze

Dear Explorer,

Eric is stuck in a 3D maze arranged as an n × n × n cube. Each cell is either a wall 'X' or an empty space '.'. From any empty cell, movement is allowed only to the six face-adjacent neighbors (no diagonals).

Some empty cells allow Eric to reach the outside of the cube via a path of empty cells that touches any outer face of the cube; others are enclosed by walls and cannot reach the outside. Your task is to count how many empty cells have a path to the outside.

Input Format

  • Line 1: An integer n (1 ≤ n), the side length of the cube.
  • Next n lines: The i-th line (0-indexed) encodes the i-th layer of the cube as a flat string of length n² consisting only of 'X' and '.'. This string is the layer in row-major order: for row j = 0..n-1 and column k = 0..n-1, character at index j·n + k corresponds to cell (layer i, row j, column k).

Output Format

  • Single integer: the number of empty cells '.' that have a path through empty cells to any empty cell on the outer surface of the cube.

Notes & Clarifications

  • Movement is allowed only between face-adjacent empty cells (±x, ±y, ±z); diagonal moves are not allowed.
  • An empty cell on the outer surface is itself considered to “reach the outside”.
  • There are no spaces in the layer strings; each layer line is exactly n² characters long.

Example 1 Input

2
....
..X.

Example 1 Output

7

Explanation: The cube is 2×2×2. All empty cells except the single interior-blocked cell are connected to the surface.

Example 2 Input

3
XXXX.XXXX
XXXX.XXXX
XXXX.XXXX

Example 2 Output

3

Explanation: A vertical tunnel of 3 empty cells connects top to bottom; only those 3 cells reach the outside.

Example 3 Input

3
X.X.X.X.X
.X.X.X.X.
X.X.X.X.X

Example 3 Output

12