Starry Night
IOI 98

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.

USACO 5.1 Starry Night_git 
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)

23
15
10001000000000010000000
01111100011111000101101
01000000010001000111111
00000000010101000101111
00000111010001000000000
00001001011111000000000
10000001000000000000000
00101000000111110010000
00001000000100010011111
00000001110101010100010
00000100110100010000000
00010001110111110000000
00100001110000000100000
00001000100001000100101
00000001110001000111000
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.

USACO 5.1 Starry Night_ico_02
Figure 2. 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)

a000a0000000000b0000000
0aaaaa000ccccc000d0dd0d
0a0000000c000c000dddddd
000000000c0b0c000d0dddd
00000eee0c000c000000000
0000e00e0ccccc000000000
b000000e000000000000000
00b0f000000ccccc00a0000
0000f000000c000c00aaaaa
0000000ddd0c0b0c0a000a0
00000b00dd0c000c0000000
000g000ddd0ccccc0000000
00g0000ddd0000000e00000
0000b000d0000f000e00e0b
0000000ddd000f000eee000
This is one possible result for the sample input above. Notice that this output file corresponds to the following picture.

USACO 5.1 Starry Night_ico_03
Figure 3. Picture with the clusters marked

 

Constraints

0 <= W (width of the sky map) <= 100
0 <= H (height of the sky map) <= 100
0 <= Number of clusters <= 500
0 <= Number of non-similar clusters <= 26 (a..z)
1 <= Number of stars per cluster <= 160

————————————————————————————题解

这道题就是个暴力模拟题

除了恶心人没有别的作用

对称轴我们可以选成50

旋转就是把行的序号=列的序号,列的序号=100-行的序号+1

相似维护就是简单的并查集

真的好恶心啊qwq但是过了还是有一点成就感的,拿最后的数据输出了一下给自己看了看图像的样子是什么

  1 /*
  2 ID: ivorysi
  3 LANG: C++
  4 TASK: starry
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <set>
 12 #include <vector>
 13 #include <string.h>
 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 18 #define inf 0x7fffffff
 19 #define ivorysi
 20 #define mo 97797977
 21 #define hash 974711
 22 #define base 47
 23 #define pss pair<string,string>
 24 #define MAXN 30005
 25 #define fi first
 26 #define se second
 27 #define pii pair<int,int>
 28 using namespace std;
 29 char m[105][105];
 30 int maze[105][105],id[105][105];
 31 int w,h,cnt,fa[505],color[505],tot,used[505];
 32 int dix[8]={1,-1,0,0,1,-1,1,-1};
 33 int diy[8]={0,0,1,-1,1,1,-1,-1};
 34 vector<pii > v[505];
 35 pii size[505],poi[505];
 36 int getfa(int x) {
 37     return fa[x]==x ? x : fa[x]=getfa(fa[x]);
 38 }
 39 int to[505],bo[505],lf[505],ri[505];
 40 void dfs(int x,int y) {
 41     id[x][y]=cnt;
 42     if(y<lf[cnt]) lf[cnt]=y;
 43     if(y>ri[cnt]) ri[cnt]=y;
 44     if(x<to[cnt]) to[cnt]=x;
 45     if(x>bo[cnt]) bo[cnt]=x;
 46      v[cnt].push_back(make_pair(x,y));
 47     xiaosiji(i,0,7) {
 48         if(maze[x+dix[i]][y+diy[i]] && id[x+dix[i]][y+diy[i]]==0) {
 49             dfs(x+dix[i],y+diy[i]);
 50         }
 51     }
 52 }
 53 bool circ(int ori,int ch) {
 54     pii t=make_pair(0,0);
 55     xiaosiji(i,0,v[ori].size()) {
 56         int temp=v[ori][i].fi;
 57         v[ori][i].fi=v[ori][i].se;
 58         v[ori][i].se=100-temp+1;
 59         if(v[ori][i]>t) t=v[ori][i];
 60     }
 61     int inx=poi[ch].fi-t.fi,iny=poi[ch].se-t.se;
 62     xiaosiji(i,0,v[ori].size()) {
 63         v[ori][i].fi+=inx;
 64         v[ori][i].se+=iny;
 65     }
 66     sort(v[ori].begin(),v[ori].end());
 67     xiaosiji(i,0,v[ori].size()) {
 68         if(v[ori][i]!=v[ch][i]) {
 69             return false;
 70         }
 71     }
 72     return true;
 73 }
 74 void sym(int ori) {
 75     xiaosiji(i,0,v[ori].size()) {
 76         v[ori][i].se=100-v[ori][i].se;
 77     }
 78 }
 79 void solve() {
 80     scanf("%d%d",&w,&h);
 81     siji(i,1,h) {
 82         scanf("%s",m[i]+1);
 83     }
 84     siji(i,1,h) {
 85         siji(j,1,w) {
 86             maze[i][j]=m[i][j]-'0';
 87         }
 88     }
 89     memset(to,1,sizeof(to));
 90     memset(lf,1,sizeof(lf));
 91     siji(i,1,h){
 92         siji(j,1,w) {
 93             if(maze[i][j]) {
 94                 if(id[i][j]==0) {
 95                     v[++cnt].clear();
 96                     dfs(i,j);
 97                     size[cnt]=make_pair(bo[cnt]-to[cnt],ri[cnt]-lf[cnt]);
 98                     if(size[cnt].fi > size[cnt].se) 
 99                         swap(size[cnt].fi,size[cnt].se);
100                 }
101             }
102         }
103     }
104     siji(i,1,cnt) fa[i]=i;
105     siji(i,1,cnt) {
106         sort(v[i].begin(),v[i].end());
107         pii t=make_pair(0,0);
108         xiaosiji(j,0,v[i].size()) {
109             if(v[i][j]>t) t=v[i][j];
110         }
111         poi[i]=t;
112         siji(j,i+1,cnt) {
113             if(getfa(i)!=getfa(j)) {
114                 if(v[j].size() != v[i].size()) {continue;}
115                 if(size[i] != size[j]) {continue;}
116                 siji(k,1,4){
117                     if(circ(j,i)) {fa[getfa(j)]=getfa(i);break;}
118                 }
119                 sym(j);
120                 siji(k,1,4){
121                     if(circ(j,i)) {fa[getfa(j)]=getfa(i);break;}
122                 }
123             }
124         }
125     }
126     siji(i,1,cnt) {
127         if(!used[getfa(i)]) {
128             used[getfa(i)]=1;
129             color[getfa(i)]=++tot;
130         }
131         else {
132             color[i]=color[getfa(i)];
133         }
134     }
135     siji(i,1,h) {
136         siji(j,1,w) {
137             if(id[i][j]!=0) {
138                 printf("%c",color[id[i][j]]+'a'-1);
139             }
140             else {printf("0");}
141         }
142         puts("");
143     }
144 } 
145 int main(int argc, char const *argv[])
146 {
147 #ifdef ivorysi
148     freopen("starry.in","r",stdin);
149     freopen("starry.out","w",stdout);
150 #else
151     freopen("f1.in","r",stdin);
152 #endif
153     solve();
154     return 0;
155 }