This page is still under construction.

Parts of this page are still being built. What you see may change.

Starry Night

Time limit1sMemory limit128 MB

Summary
Find 8-connected star clusters in a grid and assign the same letter to clusters that match under rotation and reflection.
Level

Medium6 of 10

Topics
DFS, Matrix, Hash map, Geometry
Solved
No attempts yet

Task

High up in the night sky, the shining stars appear in clusters of various shapes. A cluster is a non-empty group of neighbouring stars that are adjacent horizontally, vertically, or diagonally. A cluster is maximal: it cannot be part of a larger cluster.

Clusters may be similar. Two clusters are similar if they have the same shape and the same number of stars, regardless of orientation. In general a cluster can appear in eight orientations (four rotations, each optionally mirrored), as illustrated below.

Eight similar clusters

Eight similar clusters.

The night sky is represented by a sky map, a two-dimensional matrix of 0s and 1s. A cell holds 1 if it contains a star and 0 otherwise.

Mark every cluster with a lower-case letter by replacing each 1 of the cluster with that letter. Similar clusters must be marked with the same letter; clusters that are not similar must be marked with different letters.

Input

The first line contains the width WW of the sky map; the second line contains its height HH. The next HH lines contain the sky map, each with exactly WW characters (0 or 1).

  • 0≤W≤1000 \le W \le 100
  • 0≤H≤1000 \le H \le 100
  • 0≤0 \le number of clusters ≤500\le 500
  • 0≤0 \le number of non-similar clusters ≤26\le 26 (letters a..z)
  • 1≤1 \le number of stars per cluster ≤160\le 160

Output

Print the sky map with every cluster marked. To make the answer unique, assign the letters as follows.

Scan the map in row-major order (top to bottom, and within each row from left to right). The first time you reach a star that does not yet belong to a marked cluster, that star begins a new cluster; determine the cluster's shape. Keep a mapping from shapes to letters built in order of first appearance: the first distinct shape encountered is marked a, the next new distinct shape b, then c, and so on. Whenever a cluster's shape has already appeared (it is similar to an earlier cluster), reuse that shape's letter.

Replace every 1 of each cluster with its assigned letter and print the resulting HH lines.

Hint

The example below has a sky map of width 23 and height 15. This map corresponds to the following picture of the sky.

Picture of the sky

Picture of the sky.

After all clusters are marked, the picture becomes:

Picture with the clusters marked

Picture with the clusters marked.

Examples1

  1. Example 1

    Input
    23
    15
    10001000000000010000000
    01111100011111000101101
    01000000010001000111111
    00000000010101000101111
    00000111010001000000000
    00001001011111000000000
    10000001000000000000000
    00101000000111110010000
    00001000000100010011111
    00000001110101010100010
    00000100110100010000000
    00010001110111110000000
    00100001110000000100000
    00001000100001000100101
    00000001110001000111000
    
    Expected output
    a000a0000000000b0000000
    0aaaaa000ccccc000d0dd0d
    0a0000000c000c000dddddd
    000000000c0b0c000d0dddd
    00000eee0c000c000000000
    0000e00e0ccccc000000000
    b000000e000000000000000
    00b0f000000ccccc00a0000
    0000f000000c000c00aaaaa
    0000000ddd0c0b0c0a000a0
    00000b00dd0c000c0000000
    000g000ddd0ccccc0000000
    00g0000ddd0000000e00000
    0000b000d0000f000e00e0b
    0000000ddd000f000eee000