Depressed_ @ 2024-09-25 21:36:37
Rt
#include <bits/stdc++.h>
#define FstIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mem(a, v) memset(a, v, sizeof a)
#define pii pair<ll, ll>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll N = 1e3 + 5, M = 2e2 + 5;
const ll mod = 1e9 + 7, mod2 = 998244353;
const ld eps = 1e-6;
ll n, m;
ll Mp[N][N], f[N][N];
ll dx[] = {0, 0, 1, -1},
dy[] = {1, -1, 0, 0};
vector <vector <pii> > Point;
vector <pii> ne;
void dfs(ll x, ll y)
{
ne.pb({x, y});
for (ll i = 0; i < 4; ++ i )
{
ll nx = x + dx[i];
ll ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !f[nx][ny] && Mp[nx][ny] != Mp[x][y])
{
f[nx][ny] = 1;
dfs(nx, ny);
}
}
}
// 110
// 000
// 010
signed main()
{
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
FstIO;
cin >> n >> m;
for (ll i = 1; i <= n; ++ i )
{
for (ll j = 1; j <= n; ++ j )
{
char c; cin >> c;
Mp[i][j] = c - '0';
}
}
for (ll i = 1; i <= n; ++ i )
{
for (ll j = 1; j <= n; ++ j )
{
if (f[i][j] == 0) ne.clear(), f[i][j] = 1, dfs(i, j), Point.pb(ne);
}
}
for (ll i = 0; i < Point.size(); ++ i )
{
vector <pii> p = Point[i];
for (auto x : p) f[x.first][x.second] = p.size();
}
// for (ll i = 1; i <= n; ++ i )
// {
// for (ll j = 1; j <= n; ++ j )
// {
// cout << f[i][j] << ' ';
// }
// cout << '\n';
// }
for (ll _ = 1, x, y; _ <= m; ++ _ ) cin >> x >> y, cout << f[x][y] << '\n';
return 0;
cout.flush();
}
by NIKEAJ @ 2024-09-25 21:39:01
吧#define pii pair<ll, ll>的ll改成int
by NIKEAJ @ 2024-09-25 21:39:41
把#define pii pair<ll, ll>的ll改成int
@Depressed_
by Depressed_ @ 2024-09-26 19:40:34
@NIKEAJ thx 已关