Skip to main content
Back to problems
Codeforces
Easy
Geometry
Matrices
Math
Draw a Square

Complete a small grid so that the four marked cells form the corners of an axis-aligned square.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given a square grid containing exactly four marked cells represented by # and all other cells represented by ..

Your task is to add the minimum number of # cells so that the four original marked cells become the corners of an axis-aligned square. The square may be larger than the current arrangement, but its sides must be parallel to the grid axes.

If the four existing marked cells already form a square, the grid should remain unchanged.

What to output

Return the final grid after placing the extra marks.

Notes

  • The four original # cells are guaranteed to be positioned so that a valid square can be completed.
  • The answer is unique up to the required placement of the missing corners.

Input Format

  • The first line contains an integer t, the number of test cases.
  • For each test case, four lines follow, each containing four characters . or # describing the grid.

Output Format

For each test case, output the completed 4 x 4 grid after adding the missing # cells.

Constraints

  • 1 <= t <= 100
  • Each grid is 4 x 4
  • Exactly four cells in the grid are marked with #
  • A valid axis-aligned square can always be formed by adding cells
Examples
Sample cases returned by the problem API.

Example 1

Input

1
....
.#..
....
..#.

Output

....
.#.#
....
.#.#

Explanation

The marked cells are on different rows and columns. Completing the square requires adding the other two corners at the matching row/column intersections.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.