Usaco Training Section 5.1 Starry Night
阿新 • • 發佈:2018-11-12
有很多星座,讓你給這些星座編號。(一個星座翻折、旋轉得到的星座算同一種)
直接dfs找聯通塊,最主要的是判同一種。我是把一個星座的每個點排序,用每個點減去第一個點,得出每個點的相對位置,再把每個點的行列座標連成一個字串,放入map。這樣寫起來可能比直接比對好寫一些。
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define inf 2147483647 #define mp make_pair #define pii pair<int,int> #define pb push_back using namespace std; char a[105][105]; int v[8][2]={-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1},n,m; map<string,char> ma; vector<pii> p,p2; inline void dfs(int x,int y,char c){ a[x][y]=c; p.push_back(mp(x,y)); for(int i=0;i<8;++i){ int x2=x+v[i][0],y2=y+v[i][1]; if(x2>0&&x2<=n&&y2>0&&y2<=m&&a[x2][y2]=='1') dfs(x2,y2,c); } } inline string work(vector<pii> b,int sz){ sort(b.begin(),b.end()); string s=""; for(int k=0;k<sz;++k){ int x=b[k].first-b[0].first+100,y=b[k].second-b[0].second+100; char ch; while(x!=0) ch=(x%10)+48,s+=ch,x/=10; s+=' '; while(y!=0) ch=(y%10)+48,s+=ch,y/=10; s+=' '; } return s; } int main() { ios::sync_with_stdio(false); freopen("starry.in","r",stdin); freopen("starry.out","w",stdout); cin>>m>>n; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) cin>>a[i][j]; char c='a'; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(a[i][j]=='1'){ p.clear(); dfs(i,j,c); int sz=p.size(); string s=work(p,sz); char ch=ma[s]; if(ch>='a'&&ch<='z'){ for(int k=0;k<sz;++k) a[p[k].first][p[k].second]=ch; } else{ ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(p[k].second,p[k].first)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(p[k].second,-p[k].first)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(-p[k].second,p[k].first)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(-p[k].second,-p[k].first)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(-p[k].first,p[k].second)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(p[k].first,-p[k].second)); s=work(p2,sz); ma[s]=c; p2.clear(); for(int k=0;k<sz;++k) p2.pb(mp(-p[k].first,-p[k].second)); s=work(p2,sz); ma[s]=c; ++c; } } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j) cout<<a[i][j]; cout<<'\n'; } return 0; }