【题目】:https://www.luogu.com.cn/problem/P1457
#include
#include
#include
#include
#define max(a, b) ((a > b) ? a : b)
#define SIZE 50 + 5
using namespace std;
struct node{
bool fang[5];
int number;
int belong;
node(){}
node(const bool &a, const bool &b, const bool &c, const bool &d) {
fang[0] = a, fang[1] = b, fang[2] = c, fang[3] = d;
}
};
struct node2{
int number1, number2;
node2() {}
node2(const int &x, const int &y) {
number1 = x;
number2 = y;
}
};
struct node3{
int x, y;
node3() {}
node3(const int &xx, const int &yy) {
x = xx, y = yy;
}
};
int walk[5][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, m;
node map[SIZE][SIZE];
bool vis[SIZE][SIZE];
bool pd(const int &x, const int &y) {return (x > 0 && y > 0 && x <= n && y <= m);}
node3 a[(int)1e5];
node2 count();
void fun_1();
int bfs(const int &x, const int &y, const int &z);
node count2(const int &x);
void fun_2();
int main() {
freopen("cpp.in", "r", stdin);
freopen("cpp.out", "w", stdout);
scanf("%d%d", &m, &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int x;
scanf("%d", &x);
map[i][j] = count2(x);
}
}
fun_1();
fun_2();
return 0;
}
void fun_1() {
memset(vis, 0, sizeof(vis));
node2 temp = count();
printf("%d\n%d\n", temp.number1, temp.number2);
}
node2 count() {
node2 ans = node2(0, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!vis[i][j]) {
++ans.number1;
int temp = bfs(i, j, ans.number1);
ans.number2 = max(temp, ans.number2);
}
}
}
return ans;
}
int bfs(const int &x, const int &y, const int &z) {
int begin, end;
begin = end = vis[x][y] = 1;
a[end++] = node3(x, y);
while (begin != end) {
node3 now = a[begin++];
for (int i = 0; i < 4; ++i) {
int nx = now.x + walk[i][0];
int ny = now.y + walk[i][1];
if (pd(nx, ny) && !map[now.x][now.y].fang[i] && !vis[nx][ny]) {
a[end++] = node3(nx, ny);
vis[nx][ny] = true;
}
}
}
for (int i = 1; i < end; ++i) {
map[a[i].x][a[i].y].number = end - 1;
map[a[i].x][a[i].y].belong = z;
}
return end - 1;
}
node count2(const int &x) {
node ans = node(0, 0, 0, 0);
if (x & 1) ans.fang[2] = true;
if (x & 2) ans.fang[0] = true;
if (x & 4) ans.fang[3] = true;
if (x & 8) ans.fang[1] = true;
return ans;
}
void fun_2() {
memset(vis, 0, sizeof(vis));
int ans = -1, ii = 51, jj = -1;
char ans_;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (map[i][j].fang[0] && i > 1 && map[i][j].belong != map[i - 1][j].belong) {
int temp = map[i][j].number + map[i - 1][j].number;
if (temp == ans) {
if (jj == j) {
if (i > ii) {
ans = temp, ii = i, jj = j, ans_ = 'N';
}
} else if (j < jj) {
ans = temp, ii = i, jj = j, ans_ = 'N';
}
} else if (temp > ans) {
ans = temp, ii = i, jj = j, ans_ = 'N';
}
}
if (map[i][j].fang[3] && j < m && map[i][j].belong != map[i][j + 1].belong) {
int temp = map[i][j].number + map[i][j + 1].number;
if (temp == ans) {
if (jj == j) {
if (i > ii) {
ans = temp, ii = i, jj = j, ans_ = 'E';
}
} else if (j < jj) {
ans = temp, ii = i, jj = j, ans_ = 'E';
}
} else if (temp > ans) {
ans = temp, ii = i, jj = j, ans_ = 'E';
}
}
}
}
printf("%d\n%d %d %c\n", ans, ii, jj, ans_);
}