题意:很简单,不多说。
注意:墙和房子都应该占用一个位置,所以说示意图才会这么丑。
分析:
1)如何抽象出模型
2)如何辨别链接的房子
错误分析:
1)
bool find(int &i, int &j)
{
for (i = 1; i < 2 * m; i += 2)
for (j = 1; j < 2 * n; j += 2)
步长为2,不是1
2)
int y[4] = {-1, 0, 1, 0}; // calumn
int x[4] = {0, -1, 0, 1}; // row, north is -1,and south is +1
这种要分出前后左右顺序的是最烦的。
3)
size = 1;// init with 1
cnt++;
size是1开始,因为一开始就插入一个了。
4)
if(a[next.first][next.second] == 0 && 0 == a[cur.first][cur.second]) {
// cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
continue;
}
"000"连接方式是不可行的。
代码:
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
int a[110][110] = {0}; //0-base
int m, n;
bool v[110][110] = {0};
int y[4] = {-1, 0, 1, 0}; // calumn
int x[4] = {0, -1, 0, 1}; // row, north is -1,and south is +1
// ***
// *x* x is the dot, but wall is not same as the wall
// ***
void getWall(int i, int j)
{
int tmp;
cin >> tmp;
a[i][j] = 2;
for (int p = 0; p < 4; ++p)
if ((tmp >> p) & 1)
a[i + x[p]][j + y[p]] = 1; // cout << i + x[p] << ' ' << j + y[p] << '\n';
// else cout << "sda\n";
}
bool find(int &i, int &j)
{
for (i = 1; i < 2 * m; i += 2)
for (j = 1; j < 2 * n; j += 2)
if (!v[i][j]){
// cout << i << ' ' << j << '\n';
return true;
}
// return true;
return false;
}
bool visit(pair<int, int> p)
{
if (p.first < 1 || p.first >= 2 * m)
return true;
if (p.second < 1 || p.second >= 2 * n)
return true;
if (a[p.first][p.second] == 1)
return true;
if (v[p.first][p.second])
return true;
return false;
}
void bfs()
{
int cnt, maxSize, size; //max-size
cnt = 0;
maxSize = 0;
int i, j;
pair<int, int> cur, next;
queue<pair<int, int> > q;
while (find(i, j))
{
q.push(pair<int, int>(i, j));
v[i][j] = 1;
size = 1;// init with 1
cnt++;
while (!q.empty())
{
cur = q.front();
q.pop();
for (int r = 0; r < 4; ++r)
{
next.first = cur.first + x[r];
next.second = cur.second + y[r];
// cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
if(a[next.first][next.second] == 0 && 0 == a[cur.first][cur.second]) {
// cout << next.first << ' '<< next.second << '\t' << cur.first << ' '<< cur.second << '\n';
continue;
}
if (visit(next))
continue;
else
{
q.push(next);
// cout << next.first << ' '<< next.second << '\n';
v[next.first][next.second] = 1;
if (a[next.first][next.second] == 2)
size++;
}
}
// system("pause");
}
if (maxSize < size)
maxSize = size;
}
// cout << '\t';
// for (int i = 0; i <= n * 2; ++i)
// {
// if (i % 2)
// cout << i / 2 << ' ';
// else
// cout << " ";
// }
// cout << '\n';
// for (int i = 0; i <= m * 2; ++i)
// {
// if (i % 2)
// cout << i / 2;
// cout << '\t';
// for (int j = 0; j <= n * 2; ++j)
// cout << v[i][j] << ' ';
// cout << '\n';
// }
cout << cnt << '\n' << maxSize << '\n';
}
int main()
{
cin >> m >> n;
for (int i = 0; i <= 2 * n; ++i)
a[0][i] = a[2 * m][i] = 1;
for (int i = 0; i <= 2 * m; ++i)
a[i][0] = a[i][2 * n] = 1;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
getWall(i * 2 + 1, j * 2 + 1);
// cout << '\t';
// for (int i = 0; i <= n * 2; ++i)
// {
// if (i % 2)
// cout << i / 2 << ' ';
// else
// cout << " ";
// }
// cout << '\n';
// for (int i = 0; i <= m * 2; ++i)
// {
// if (i % 2)
// cout << i / 2;
// cout << '\t';
// for (int j = 0; j <= n * 2; ++j)
// cout << a[i][j] << ' ';
// cout << '\n';
// }
bfs();
return 0;
}