这里输入时有一个小技巧,scanf(“%1d”, &Map[i][j]) 表示每次只读入1个数,如果前面不加1表示读入到非数字的地方停止。这样每次就只能读入一个数,可以用整型数组存放地图,而不是字符数组。
每当遇到一个是1的点,就开始搜索,同时把搜索到的点都变成0,
#include <bits/stdc++.h>
#define N 510
#define ll long long
#define MAX 11111
using namespace std;
const int dir[][2]={1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};
int Map[N][N], n, m, t;
int bfs(int x, int y){
int i, j, xx, yy, ret = 1;
queue<pair<int, int> > q;
q.push(pair<int, int>(x, y));
Map[x][y] = 0;
while(!q.empty()){
xx = q.front().first;
yy = q.front().second;
q.pop();
for (i = 0; i < 8; i++){
int tx = xx + dir[i][0];
int ty = yy + dir[i][1];
if (tx < 0 || tx >= n || ty < 0 || ty >= m || !Map[tx][ty]) continue;
Map[tx][ty] = 0;
ret++;
q.push(pair<int, int>(tx, ty));
}
}
return ret;
}
int main(){
//#ifndef ONLINE_JUDGE
// freopen("1.txt", "r", stdin);
//#endif
int i, j, k, ans, cnt;
while(~scanf("%d%d%d", &n, &m, &t)){
ans = 0;
cnt = 0;
for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
scanf("%1d", &Map[i][j]);
}
}
for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
if (Map[i][j]){
cnt++;
ans = max(ans, bfs(i, j));
}
}
}
printf("%d %d\n", cnt, ans*t);
}
return 0;
}