玄关求调
  • 板块P1141 01迷宫
  • 楼主Zmk2009
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/3 21:49
  • 上次更新2024/7/4 09:34:29
查看原帖
玄关求调
625671
Zmk2009楼主2024/7/3 21:49
#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 5;
#define all(s) s.begin(),s.end()
const int M = 1005;
typedef long long ll;
#define endl "\n"
int a[M][M];
queue <pair<int,int> > q;
int dx[] = {0 , 1 , 0 , -1 , 0};
int dy[] = {0 , 0 , 1 , 0 , -1};
int vis[M][M];
int cnt[N];
int n , m;
void BFS(int t){
    cnt[t] ++;
    while (!q.empty()){
        pair <int,int> u = q.front();q.pop();
        for (int i = 1 ; i <= 4 ; i ++){
            int x = u.first + dx[i], y = u.second + dy[i];
            if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || a[u.first][u.second] == a[x][y]) continue;
            vis[x][y] = t;
            q.push(make_pair(x,y));
            cnt[t] ++;
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int qu;
    cin >> n >> qu;
    m = n;
    for (int i = 1 ; i <= n ; i ++){
        for(int j = 1 ; j <= m ; j ++){
            char c = getchar();
            while (c != '0' && c != '1') c = getchar();
            a[i][j] = c - '0';
        }
    }
    int tot = 0;
    for (int i = 1 ; i <= n ; i ++){
        for (int j = 1 ; j <= m ; j ++){
            if (!vis[i][j]) {
                q.push(make_pair(i,j));
                vis[i][j] = ++ tot;
                BFS(tot);
            }
        }
    }
    for (int i = 1 ; i <= qu ; i ++){
        int x , y;
        cin >> x >> y;
        cout << cnt[vis[x][y]] << endl;
    }
    return 0;
}

不知道为什么RE + TLE

2024/7/3 21:49
加载中...