P2749 [USACO5.1] starry night sky Starry Night

P2749 [USACO5.1] starry night sky Starry Night     

Large simulation    

1. Unicom block scan (eight directions Development)    

2. deduplication    

3. Mark    

4. Output    

Good code is shorter than kill a pig with country can not be compared    

Code:     

#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m;
bool Map[N][N]={0};
int cnt=0;
int col[N][N]={0};
int dx[8]={0,0,1,-1,1,-1,1,-1},dy[8]={1,-1,0,0,1,-1,-1,1};
map<int,double> s;
map<int,char> p;
vector<pair<int,int> > f[N<<5];
double ans[N<<5];
int ano=0;
void dfs(int x,int y){
    if(x<1||x>n||y<1||y>m) return;
    col[x][y]=cnt;
    pair<int,int> a;
    a.first=x,a.second=y;
    f[cnt].push_back(a);
    for(int d=0;d<8;d++){
        int xx=x+dx[d],yy=y+dy[d];
        if(!col[xx][yy]&&Map[xx][yy]) dfs(xx,yy);
    }
}
double cal(int cc){
    double yuy=0;
    for(int i=0;i<f[cc].size();i++){
        for(int j=i+1;j<f[cc].size();j++){
            yuy=yuy+sqrt((double)(f[cc][i].first-f[cc][j].first)*(f[cc][i].first-f[cc][j].first)+(f[cc][i].second-f[cc][j].second)*(f[cc][i].second-f[cc][j].second));
        }
    }
    int u=-1;
    for(int i=1;i<cc;i++){
        if(yuy-ans[i]<=0.000001&&yuy-ans[i]>=-0.000001){
            u=i;
            break;    
        }
    }
    if(u!=-1){
        for(int i=0;i<f[cc].size();i++){
            col[f[cc][i].first][f[cc][i].second]=u;
        }
    }
    ans[cc]=yuy;
}
int main(){
    s.clear();
    scanf("%d%d",&n,&m);
    swap(n,m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char c;
            cin>>c;
            if(c=='0') Map[i][j]=0;
            else Map[i][j]=1;
        }
    } 
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(Map[i][j]==1&&!col[i][j]){
                cnt++;
                dfs(i,j);
            }
        }
    }
    
    for(int i=1;i<=cnt;i++){
        cal(i);
    }
    s.clear();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(!Map[i][j]) printf("0");
            else{
                if(s[col[i][j]]==0){
                    s[col[i][j]]=1;
                    ano++;
                    int t='a'+ano-1;
                    p[col[i][j]]=(char)t;
                }
                printf("%c",p[col[i][j]]);
            }
        }
        printf("\n");
    }
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/QYJ060604/p/11424205.html