AcWing 1402 Starry Night

Title description:

In the depths of the night sky, shining stars appear in people's eyes in the form of clusters of stars, in various forms.

A constellation refers to a group of non-empty stars that are adjacent in the horizontal, vertical or diagonal directions.

A constellation cannot be part of a larger constellation.

The constellations may be similar.

If two constellations have the same shape and number of stars, they are considered similar regardless of their orientation.

Generally, there may be 88 orientations of constellations, as shown in the following figure:

starry-1.gif

Now, we use a two-dimensional 0101 matrix to represent the night sky. If the number at a location is 11, then there is a star at this location, otherwise the number at this location should be 00.

Given a two-dimensional matrix of the night sky, please mark all the constellations in it with lowercase letters. When marking similar constellations use the same letter, and dissimilar constellations use different letters.

To label a constellation means to replace all 11 in the constellation with lowercase letters.

Input format

The first line contains an integer WW, which represents the width of the matrix.

The second line contains an integer HH, which represents the height of the matrix.

The next HH rows, each row contains a 0101 sequence of length WW, used to describe the entire night sky matrix.

Output format

Output the two-dimensional matrix after marking all constellations.

There are many ways to mark constellations with lowercase letters. We read the entire output as a string. The marking method that can make the string lexicographically smallest is the marking method we want.

Output the final two-dimensional matrix marked by this marking method.

data range

0≤W, H≤1000≤W, H≤100,
0≤0≤Number of constellations ≤500≤500,
0≤0≤Number of dissimilar constellations ≤26≤26,
1≤1≤Number of stars in the constellation ≤160≤160

Input sample:

23
15
10001000000000010000000
01111100011111000101101
01000000010001000111111
00000000010101000101111
00000111010001000000000
00001001011111000000000
10000001000000000000000
00101000000111110010000
00001000000100010011111
00000001110101010100010
00000100110100010000000
00010001110111110000000
00100001110000000100000
00001000100001000100101
00000001110001000111000

Sample output:

a000a0000000000b0000000
0aaaaa000ccccc000d0dd0d
0a0000000c000c000dddddd
000000000c0b0c000d0dddd
00000eee0c000c000000000
0000e00e0ccccc000000000
b000000e000000000000000
00b0f000000ccccc00a0000
0000f000000c000c00aaaaa
0000000ddd0c0b0c0a000a0
00000b00dd0c000c0000000
000g000ddd0ccccc0000000
00g0000ddd0000000e00000
0000b000d0000f000e00e0b
0000000ddd000f000eee000

Sample explanation

The starry sky map corresponding to the sample is as follows:

starry-2.gif

The marked starry sky map corresponding to the answer is as follows:

starry-3.gif

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int MAX = 109;
const double eps = 1e-6;

char g[MAX][MAX];
int n, m;
PII q[MAX * MAX]; // 记录连通块
int top;

double getdis(PII a, PII b)
{
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
}

double get_hash()
{
    double sum = 0;

    for(int i = 0; i < top; i++)
    {
        for(int j = i + 1; j < top; j++)
        {
            sum += getdis(q[i], q[j]);
        }
    }
    return sum;
}

char get_id(double h_num)
{
    static double Hash[30];
    static int id = 0;
    for(int i = 0; i < id; i++)
    {
        if(fabs(Hash[i] - h_num) < eps)
            return i + 'a';
    }
    Hash[id++] = h_num;
    return id - 1 + 'a';
}

void dfs(int a, int b)
{
    q[top++] = {a, b};
    g[a][b] = '0';

    for(int x = a - 1; x <= a + 1; x ++)
    {
        for(int y = b - 1; y <= b + 1; y++)
        {
            if(x == a && y == b)
                continue;
            if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '1')
                dfs(x, y);
        }
    }
}

int main()
{
    cin >> m >> n;

    for(int i = 0; i < n; i++)
        cin >> g[i];

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(g[i][j] == '1')
            {
                top = 0;
                dfs(i, j);
                char c = get_id(get_hash());

                for(int k = 0; k < top; k++)
                    g[q[k].x][q[k].y] = c;
            }
        }
    }

    for(int i = 0; i < n; i++)
        cout << g[i] << endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113988181