3
9
17
4 1 N
5 5
3 2 6 3 6
1 8 4 1 4
13 7 13 9 4
3 0 2 6 5
9 8 8 12 13
// 星之卡比!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10, INF = 0x3f3f3f3f;
const int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
struct sit
{
int x, y;
};
struct kss
{
int x, y, tans, cnt;
};
int n, m, a[N][N], ans1, ans2 = -INF, big, d, ansx, ansy, l;
bool v[N][N], t[N][N];
char ansd;
queue <sit> q;
kss pq[N * N];
bool cmp(kss a, kss b)
{
if (a.tans == b.tans)
return a.cnt <= b.cnt;
return a.tans >= b.tans;
}
bool check(int x, int y, int dxx, int dyy)
{
bool west = a[x][y] & 1;
bool north = a[x][y] & 2;
bool east = a[x][y] & 4;
bool south = a[x][y] & 8;
if (x + dxx >= 1 and x + dxx <= n and y + dyy >= 1 and y + dyy <= m and !v[x + dxx][y + dyy])
{
if (west and dyy == -1)
return false;
else if (east and dyy == 1)
return false;
else if (north and dxx == -1)
return false;
else if (south and dxx == 1)
return false;
else
return true;
}
return false;
}
void bfs(int x, int y)
{
q.push( {x, y} );
v[x][y] = true;
while (q.size())
{
big++;
sit top = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
if (check(top.x, top.y, dx[i], dy[i]))
{
int xx = top.x + dx[i];
int yy = top.y + dy[i];
q.push( {xx, yy} );
v[xx][yy] = true;
}
}
}
ans1++;
if (ans2 <= big)
{
// w>s>n>e
ans2 = big, ansx = x, ansy = y;
if (d == 1)
ansd = 'W', pq[++l] = {x, y, ans2, 1};
else if (d == 2)
ansd = 'N', pq[++l] = {x, y, ans2, 3};
else if (d == 4)
ansd = 'E', pq[++l] = {x, y, ans2, 4};
else if (d == 8)
ansd = 'S', pq[++l] = {x, y, ans2, 2};
}
sort(pq + 1, pq + l + 1, cmp);
for (int i = 1; i <= l; i++)
cout << pq[i].x << " " << pq[i].y << " " << pq[i].tans << " " << pq[i].cnt << endl;
cout << endl;
big = 0;
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!v[i][j])
bfs(i, j);
cout << ans1 << endl << ans2 << endl;
ans2 = -INF;
memset(v, false, sizeof(v));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!v[i][j])
{
memcpy(t, v, sizeof(v));
if (a[i][j] & 1)
{
d = 1;
a[i][j] -= 1;
bfs(i, j);
a[i][j] += 1;
memcpy(v, t, sizeof(t));
}
if (a[i][j] & 2)
{
d = 2;
a[i][j] -= 2;
bfs(i, j);
a[i][j] += 2;
memcpy(v, t, sizeof(t));
}
if (a[i][j] & 4)
{
d = 4;
a[i][j] -= 4;
bfs(i, j);
a[i][j] += 4;
memcpy(v, t, sizeof(t));
}
if (a[i][j] & 8)
{
d = 8;
a[i][j] -= 8;
bfs(i, j);
a[i][j] += 8;
memcpy(v, t, sizeof(t));
}
}
cout << ans2 << endl << pq[1].x << " " << pq[1].y << " ";
if (pq[1].cnt == 1) // w>s>n>e
cout << 'W';
else if (pq[1].cnt == 2)
cout << 'S';
else if (pq[1].cnt == 3)
cout << 'N';
else if (pq[1].cnt == 4)
cout << 'E';
return 0;
}```