1402. 星空之夜 ###K //K

题目链接:https://www.acwing.com/problem/content/description/1404/

思路:利用一个星群中 每两点之间的欧几里得距离的和来作为key值即可

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn=1e2+10;
  4 const int mod=998244353;
  5 const double eps=1e-9;
  6 #define ll long long
  7 #define pb push_back
  8 #define pi pair<int,int>
  9 #define fi first
 10 #define sc second
 11 
 12 int n,m;
 13 char s[maxn][maxn];
 14 pi t[maxn*maxn];
 15 int tot;
 16 int xx[8]={-1,-1,-1,0,0,1,1,1};
 17 int yy[8]={-1,0,1,-1,1,-1,0,1};
 18 double ch[27];
 19 int cnt;
 20 
 21 double dis(pi a,pi b)
 22 {
 23     double x=a.fi-b.fi;
 24     double y=a.sc-b.sc;
 25     return sqrt(x*x+y*y);
 26 }
 27 
 28 void dfs(int x,int y)
 29 {
 30     t[++tot]={x,y};
 31     s[x][y]='0';
 32     for(int i=0;i<8;i++)
 33     {
 34         int tx=xx[i]+x;
 35         int ty=yy[i]+y;
 36         if(tx<1||tx>n||ty<1||ty>m||s[tx][ty]!='1')
 37             continue;
 38         dfs(tx,ty);
 39     }
 40 }
 41 double getsum()
 42 {
 43     double sum=0;
 44     for(int i=1;i<=tot;i++)
 45         for(int j=i+1;j<=tot;j++)
 46             sum+=dis(t[i],t[j]);
 47     return sum;
 48 }
 49 char getid(double x)
 50 {
 51     for(int i=1;i<=cnt;i++)
 52     {
 53         if(fabs(x-ch[i])<eps)
 54         {
 55             return char('a'+i-1);
 56         }
 57     }
 58     ch[++cnt]=x;
 59     return char('a'+cnt-1);
 60 }
 61 void change(char d)
 62 {
 63     for(int i=1;i<=tot;i++)
 64     {
 65         int x=t[i].fi,y=t[i].sc;
 66         s[x][y]=d;
 67     }
 68 }
 69 
 70 
 71 
 72 int main()
 73 {
 74     ios::sync_with_stdio(0);
 75     cin.tie(0);
 76     cin>>m>>n;
 77     for(int i=1;i<=n;i++) cin>>(s[i]+1);
 78     for(int i=1;i<=n;i++)
 79         for(int j=1;j<=m;j++)
 80         {
 81             if(s[i][j]=='1')
 82             {
 83                 tot=0;
 84                 dfs(i,j);
 85                 double sum=getsum();
 86                 char d=getid(sum);
 87                 change(d);
 88             }
 89         }
 90     for(int i=1;i<=n;i++)
 91     {
 92         for(int j=1;j<=m;j++)
 93         {
 94             cout<<s[i][j];
 95         }
 96         cout<<'\n';
 97     }
 98 
 99 
100 
101 
102 
103 
104 }
View Code

 

posted @ 2021-01-28 20:34  canwinfor  阅读(142)  评论(0)    收藏  举报