Nowruz 9
Time limit1sMemory limit512 MB
Given a grid with rocks, plant bushes so the remaining free cells form a tree, maximizing the number of cells that have exactly one free neighbor.
- Level
Hard8 of 10
- Topics
- Graph, Tree, Greedy, Implementation
- Solved
- No attempts yet
Problem
It is a few days until Nowruz, the Persian new year, and grandpa has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, grandpa is going to run a game of hide-and-seek.
The garden can be represented by an grid of unit cells. Some cells (possibly zero) are blocked by rocks, and the remaining cells are called free. Two cells are called neighbors if they share an edge. That is, each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction. Grandpa wants to turn his garden into a maze. For this purpose, he can block some free cells by planting bushes in them. The cells where he plants the bushes are no longer free.
A maze must have the following property. For each pair and of free cells in the maze there must be exactly one simple path between them. A simple path between cells and is a sequence of free cells in which the first cell is , the last cell is , all cells are distinct, and each two consecutive cells are neighbors.
A kid can hide in a cell if and only if that cell is free and has exactly one free neighbor. No two kids can hide in the same cell.
You are given the map of the garden as input. Your task is to help grandpa create a maze in which many kids can hide.
This is an output-only task with partial scoring. You are given input files, each describing grandpa's garden. For each input file you should submit an output file with a map of a maze. For each output file you will get points based on the number of kids that can hide in your maze.
You are not supposed to submit any source code for this task.
Input
Each input file describes one grid representing a garden and gives the number of kids invited by grandpa. The format is as follows:
-
line :
-
line (for ): row of the grid, which is a string of length , consisting of the following characters (without any whitespace):
- '
.': a free cell, - '
#': a rock.
- '
Output
-
line (for ): row of the maze (the garden, after bushes are planted). It is a string of length , consisting of the following characters (without any whitespace):
- '
.': a free cell, - '
#': a rock, - '
X': a bush. (Note that the letter X must be in uppercase.)
- '