AcWing 1402. 星空之夜
原题链接
中等
作者:
回归线
,
2021-03-27 15:18:19
,
所有人可见
,
阅读 249
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-6;
int n, m;
char g[N][N];
pair<int, int> q[N * N];
int top;
int id = 0;
double get_dist(pair<int, int>& a, pair<int, int>& b) {
double x = a.first - b.first;
double y = a.second - b.second;
return hypot(x, y);
}
double get_hash() {
double sum = 0;
for (int i = 0; i < top; ++i) {
for (int j = i + 1; j < top; ++j) {
sum += get_dist(q[i], q[j]);
}
}
return sum;
}
char get_id(double key) {
static double hash[30];
for (int i = 0; i < id; ++i)
if (abs(hash[i] - key) < eps)
return i + 'a';
hash[id++] = key;
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 < m && y >= 0 && y < n && g[x][y] == '1') {
dfs(x, y);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i){
cin >> g[i];
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++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].first][q[k].second] = c;
}
}
}
}
for (int i = 0; i < m; ++i) {
cout << g[i] << endl;
}
return 0;
}