玄学代码求条

P1141 01迷宫

limuqian_luogu @ 2024-12-14 20:03:46

#include <bits/stdc++.h>
using namespace std;
int in()
{
    int t = 0, f = 1;char c = getchar();
    while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
    while(c >= '0' && c <= '9')t = t * 10 + c - '0', c = getchar();
    return t * f;
}
char c[1005][1005];
int n, m, a[1005][1005];
int flag[1005][1005];
int ans[1005];
int dx[10] = {0, 0, 0, 1, 1};
int dy[10] = {0, 1, 1, 0, 0};
void dfs(int x, int y, int t)
{
    flag[x][y] = t;
    ans[t]++;
    for(int i = 1 ; i <= n ; i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(xx < 1 || xx > n || yy < 1 || yy > n || c[xx][yy] == c[x][y])
            continue;
        dfs(xx, yy, t);
    }

}
int main()
{
    n = in();
    m = in();
    for(int i = 1 ; i <= n ; i++)
        for(int j = 1 ; j <= n ; j++)
            cin >> c[i][j];
    for(int i = 1 ; i <= m ; i++)
    {
        int x, y;
        x = in();
        y = in();
        if(flag[x][y] == 0)dfs(x, y, i);
        cout << ans[flag[x][y]] << endl;
    }
    return 0;
}

|