Usaco Training Section 5.1 Starry Night

有很多星座,让你给这些星座编号。(一个星座翻折、旋转得到的星座算同一种)

直接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;
}

猜你喜欢

转载自blog.csdn.net/qq_36911709/article/details/82284974
今日推荐