Problem
Sample-1 in
5 7
*......
..**..*
.*...*.
...*...
....*..
Sample-1 out
3 4
Sample-2 in
10 10
**..**.**.
***....*..
*...**.**.
...*..*...
..........
**...**.*.
..*.*....*
..........
***..*.*..
.***..*...
Sample-2 out
4 12
思路
这天空如此狭小
暴力。
dfs处理每个星座,桶排序处理星系。反正数据不大,随便敲。
Code
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int n,m,maxx,tongji,a[2001][2001],b[2001][2001],ans[100010];
int fx[10]={-1,0,1,-1,1,-1,0,1},fy[10]={-1,-1,-1,0,0,1,1,1};
char c;
bool check(int xx,int yy){
if(xx == 0 || yy == 0 || xx > n || yy > m) return 0;
return 1;
}
int dfs(int x,int y){ //dfs得出此星座的大小
int anss = 1;
b[x][y] = 1;
for(int i = 0; i < 8; ++i)
if(check(x+fx[i],y+fy[i]) == 1) //单纯判断边界
if(b[x+fx[i]][y+fy[i]] == 0 && a[x+fx[i]][y+fy[i]] == 1)
anss += dfs(x+fx[i],y+fy[i]);
return anss;
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; ++i) //输入
for(int j = 1; j <= m; ++j){
c = getchar();
while(c!='*' && c!='.') c = getchar();
if(c == '*') a[i][j] = 1;
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(b[i][j] == 0 && a[i][j] == 1)
++ans[dfs(i,j)]; //这个星座加入大小为dfs(i,j)的星系
for(int i = 1; i <= 100000; ++i) //统计星系
if(ans[i] > 0){
maxx = max(maxx,i*ans[i]); //星系大小 = 星座大小*星座数
++tongji;
}
printf("%d %d",tongji,maxx);
}