NYOJ-最大岛屿
最大岛屿
时间限制:1000 ms | 内存限制:65535 KB
难度:2
描述
神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等。加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王。 这是一个由海洋、岛屿和海盗组成的危险世界。面对危险重重的海洋与诡谲的对手,如何凭借智慧与运气,建立起一个强大的海盗帝国。
杰克船长手头有一张整个海域的海图,上面密密麻麻分布着各个海屿的位置及面积。他想尽快知道整个海域共有多少岛屿以及最大岛屿的面积。
输入
第1行:M N T,表示海域的长,宽及一个单位表示的面积大小
接下来有M行 ,每行有N个01组成的序列以及其中穿插一些空格。0表示海水,1表示陆地,其中的空格没用,可以忽略掉。
输出
输出一行,有2个整数,一个空格间隔,表示整个海域的岛屿数,以及最大岛屿的面积
样例输入
8 16 99
00000000 00000000
0000110011000000
0001111000111000
0000000 00 0000000
00111 111000001 10
001110000 0000000
0100001111 111100
0000000000000000
样例输出
5 990
dfs即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int M, N, T;
char s[501][501];
int a[501][501], book[501][501];
int MAX_N = -1, ans;
void dfs(int x, int y, int color) {
int tx, ty;
a[x][y] = color;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
tx = x + dx, ty = y + dy;
if (tx < 0 || ty < 0 || tx >= M || ty >= N) continue;
if (a[tx][ty] == 1 && book[tx][ty] == 0) {
book[tx][ty] = 1;
ans++;
dfs(tx, ty, color);
}
}
}
return;
}
int main() {
//freopen("in.txt", "r", stdin);
scanf("%d%d%d", &M, &N, &T);
getchar();
for (int i = 0; i < M; i++) {
for (int j = 0; j < N;) {
scanf("%c", &s[i][j]);
if (s[i][j] != ' ') {
a[i][j] = s[i][j] - '0';
j++;
}
else continue;
}
getchar();
}
int color = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (a[i][j] == 1) {
//book[i][j] = 1;
ans = 0;
color--;
dfs(i, j, color);
MAX_N = max(ans, MAX_N);
}
}
}
printf("%d %d",-color, (MAX_N+1)*T);
return 0;
}