求调
  • 板块P1141 01迷宫
  • 楼主shuman
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/8/7 10:56
  • 上次更新2024/8/7 11:45:49
查看原帖
求调
988869
shuman楼主2024/8/7 10:56
#include <bits/stdc++.h>
using namespace std;
char a[1010][1010];
int  vis[1010][1010];
int st[1010][1010], s[101000], n, m, p = 1, sum, u, v;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
queue<pair<int, int> > bfs;
int main()
{
    memset (vis, false, sizeof(vis));
    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 (!st[i][j])
            {
                s[p] = 1, st[i][j] = p;
                while(!bfs.empty()) bfs.pop();
                bfs.push(make_pair(i, j));
                while (!bfs.empty())
                {
                    int xx = bfs.front().first, yy = bfs.front().second;
                    bfs.pop();
                    for (int k = 0; k < 4; k++)
                    {
                        int x = xx + dx[k], y = yy + dx[k];
                        if (x < 1 || x > n || y < 1 || y > n || a[xx][yy] - '0' + a[x][y] - '0' != 1 || vis[x][y])
                        {
                            continue;
                        }
                        st[x][y] = p, s[p]++;
                        vis[x][y] = true;
                        bfs.push(make_pair(x, y));
                    }
                }
                p += 1;
            }
    while (m--)
    {
        cin >> u >> v;
        cout << s[st[u][v]] << endl;
    }
    return 0;
}

2024/8/7 10:56
加载中...