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, adjacent in horizontal, vertical or diagonal direction. A cluster cannot be a part of a larger cluster.
Clusters may be similar. Two clusters are similar if they have the same shape and number of stars, irrespective of their orientation. In general, the number of possible orientations for a cluster is eight, as Figure 1 exemplifies.
Figure 1. Eight similar clusters
The night sky is represented by a sky map, which is a two-dimensional matrix of 0's and 1's. A cell contains the digit 1 if it has a star, and the digit 0 otherwise.
Given a sky map, mark all the clusters with lower case letters. Similar clusters must be marked with the same letter; non-similar clusters must be marked with different letters.
You mark a cluster with a lower case letter by replacing every 1 in the cluster by that lower case letter.
PROGRAM NAME: starry
INPUT FORMAT
The first two lines contain, respectively, the width W and the height H of a sky map. The sky map is given in the following H lines, of W characters each.
SAMPLE INPUT (file starry.in)
2315100010000000000100000000111110001111100010110101000000010001000111111000000000101010001011110000011101000100000000000001001011111000000000100000010000000000000000010100000011111001000000001000000100010011111000000011101010101000100000010011010001000000000010001110111110000000001000011100000001000000000100010000100010010100000001110001000111000
In this case, the sky map has width 23 and height 15. Just to make it clearer, notice that this input file corresponds to the following picture of the sky.
OUTPUT FORMAT
The output file contains the same map as the input file, except that the clusters are marked as described in Task.
There will generally be more than one way to label the clusters with letters. Your program should choose the labeling such that if the entire output file is read as a string, this string will be minimal in the lexicographical ordering.
SAMPLE OUTPUT (file starry.out)
a000a0000000000b00000000aaaaa000ccccc000d0dd0d0a0000000c000c000dddddd000000000c0b0c000d0dddd00000eee0c000c0000000000000e00e0ccccc000000000b000000e00000000000000000b0f000000ccccc00a00000000f000000c000c00aaaaa0000000ddd0c0b0c0a000a000000b00dd0c000c0000000000g000ddd0ccccc000000000g0000ddd0000000e000000000b000d0000f000e00e0b0000000ddd000f000eee000
This is one possible result for the sample input above. Notice that this output file corresponds to the following picture.
Constraints
0 <= W (width of the sky map) <= 100
0 <= H (height of the sky map) <= 1000 <= Number of clusters <= 5000 <= Number of non-similar clusters <= 26 (a..z)1 <= Number of stars per cluster <= 160
庞大的模拟题。
八种情况让你写得手软,所以在此要好好利用学习过的技能(Ctrl + c).
写这种题之前,要仔细想清细节,不要盲目敲代码(这会使你打一段删一段,从而使你速度大减,并且有很大的机会出错,也就是要减少更改的次数)优化判断的速度。
最后就是敲代码啦。
找一个星座出来的方法就是用floodfill。暴力判重即可。
代码200+行。
/*TASK:starryLANG:C++*/#include#include #include using namespace std;const int MAXM = 105;const int d[2][8] = { { 0, -1, -1, -1, 0, 1, 1, 1}, { -1, -1, 0, 1, 1, 1, 0, -1}};struct Cluster{ char s[MAXM][MAXM]; int numst, high, wide; bool operator == (const Cluster &b) { if (numst != b.numst) return false; if (high == b.high && wide == b.wide) { bool flag; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[i][j]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[high - i - 1][j]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[i][wide - j - 1]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[high - i - 1][wide - j - 1]) { flag = false; break; } if (!flag) break; } if (flag) return true; } if (high == b.wide && wide == b.high) { bool flag; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[wide - j - 1][i]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[j][i]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[j][high - i - 1]) { flag = false; break; } if (!flag) break; } if (flag) return true; for (int i = 0; i < high; ++i) { flag = true; for (int j = 0; j < wide; ++j) if (s[i][j] != b.s[wide - j - 1][high - i - 1]) { flag = false; break; } if (!flag) break; } if (flag) return true; } return false; }}clusters[30];char g[MAXM][MAXM];int h, w, mark[MAXM * MAXM], num[MAXM][MAXM], cnt1, cnt2, left, right, up, down;bool vis[MAXM][MAXM];Cluster create(){ Cluster tmp; memset(tmp.s, 0, sizeof(tmp.s)); tmp.numst = 0; tmp.high = down - up + 1; tmp.wide = right - left + 1; for (int i = 0; i < tmp.high; ++i) for (int j = 0; j < tmp.wide; ++j) { if (num[i + up][j + left] == cnt1) tmp.s[i][j] = '1', tmp.numst++; else tmp.s[i][j] = '0'; } return tmp;}bool test(int x, int y){ return 0 <= x && x < h && 0 <= y && y < w;}void floodfill(int x0, int y0){ vis[x0][y0] = false; num[x0][y0] = cnt1; up = min(up, x0); down = max(down, x0); left = min(left, y0); right = max(right, y0); for (int i = 0; i < 8; ++i) { int x = x0 + d[0][i], y = y0 + d[1][i]; if (test(x, y) && g[x][y] == '1' && vis[x][y]) floodfill(x, y); }}int main(){ freopen("starry.in", "r", stdin); freopen("starry.out", "w", stdout); scanf("%d%d", &w, &h); for (int i = 0; i < h; ++i) scanf("%s", g[i]); memset(vis, true, sizeof(vis)); memset(num, 0, sizeof(num)); cnt1 = cnt2 = 0; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) if (vis[i][j] && g[i][j] == '1') { ++cnt1; up = down = i; left = right = j; floodfill(i, j); Cluster cl = create(); bool flag = true; for (int k = 0; k < cnt2; ++k) if (cl == clusters[k]) { mark[cnt1] = k; flag = false; break; } if (flag) clusters[mark[cnt1] = cnt2++] = cl; } for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) if (g[i][j] != '0') printf("%c", mark[num[i][j]] + 'a'); else printf("0"); printf("\n"); } return 0;}