查错

· · 个人记录

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1005;
char a[N][N];
int  n, m, vis[N][N], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, qx, qy, l, sum[N * N], b[N][N];
struct node{
    int x, y;
};
bool check(int x, int y, int l1, int l2)
{
    if(x < 1 || x > n || y < 1 || y > n)
        return 1;
    else if(vis[x][y] || a[x][y] == a[l1][l2])
        return 1;
    return 0; 
}
void bfs()
{
    queue<node>q;
    b[qx][qy] = l;
    q.push({qx, qy});
    vis[qx][qy] = 1;
    while(q.size())
    {
        node t = q.front();
        q.pop();
        sum[l] ++;
        for(int i = 0 ;i < 4; ++ i)
        {
            int px = t.x + dx[i], py = t.y + dy[i];
            if(check(px, py, t.x, t.y))
                continue;
            vis[px][py] = 1;
            b[px][py] = l;
            q.push({px, py});
        }
    } 
    return ;
}
signed main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; ++ i)
    {
        for(int j = 1; j <= n; ++ j)
        {
            cin >> a[i][j];
        }
    }
    for(int i =1 ;i <= n; ++ i)
    {
        for(int j = 1; j<= n; ++ j)
        {
            if(!vis[i][j])
            {
                int qx = i, qy = j, l = l + 1;
                bfs();
            }
        }
    }
    while(m --)
    {
        int x, y;
        cin >> x >> y;
        cout << sum[b[x][y]] << "\n";
    }
    return 0;
}