联通块问题,BFS就可以了
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
class point
{
public:
int x;
int y;
point(int i,int j)
{
x = i;
y = j;
}
};
int main()
{
string s;
int m=1;
int n = 1;
cin >> s;
int mark = s.find(',');
m=atoi(s.substr(0, mark).c_str());
n = atoi(s.substr(mark+1,s.size()).c_str());
bool ** a = new bool*[m];
bool ** visited = new bool*[m];
for (int i = 0; i < n; i++)
{
a[i] = new bool[n];
visited[i] = new bool[n];
for (int j = 0; j < n; j++)
{
visited[i][j] = false;
}
}
for (int i = 0; i < m; i++)
{
s = "";
cin >> s;
int k = 0;
for (int j = 0; j < s.size(); j++)
{
if (s[j] == '0')
{
a[i][k] = false;
k++;
}
if (s[j] == '1')
{
a[i][k] = true;
k++;
}
}
}
//input finished
vector<int> group;
queue<point>que;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i][j] == true&&visited[i][j]!=true)
{
int number = 0;
//push queue
point p(i,j);
que.push(p);
while (!que.empty())
{
point p1 = que.front();
que.pop();
number++;
//visit p1
visited[p1.x][p1.y] = true;
//check
for (int ii = p1.x - 1; ii <= p1.x + 1; ii++)
{
for (int jj = p1.y - 1; jj <= p1.y+1; jj++)
{
if (ii < 0 || ii >= m || jj < 0 || jj >= n)
{
continue;
}
if (a[ii][jj] == true&&visited[ii][jj]!=true)
{
que.push(point(ii, jj));
visited[ii][jj] = true;
}
}
}
}
group.push_back(number);
}
}
}
int max = 0;
for (int i = 0; i < group.size(); i++)
{
max = max > group[i] ? max : group[i];
}
cout << group.size() << "," << max << endl;
}