re求助
  • 板块P1141 01迷宫
  • 楼主acwing_cht
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/7/4 12:43
  • 上次更新2023/11/6 23:41:06
查看原帖
re求助
247533
acwing_cht楼主2020/7/4 12:43
#include<bits/stdc++.h>
using namespace std;
const int N = 10010;
int n, m, g[N][N];
pair<int,int> q[N * N];
bool st[N][N];
int alls[N][N];
void init()
{
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            if(g[i][j] == 1) alls[i][j] = 1;
            else alls[i][j] = 0;
}
int bfs(int x, int y)
{
    int hh = 0, tt = -1;
    int cnt = 0;
    q[ ++ tt] = {x, y};
    st[x][y] = true;
    int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
    while(hh <= tt)
    {
        pair<int, int> t = q[hh ++];
        for(int i = 0; i < 4; i ++)
        {
            int sx = dx[i] + t.first, sy = dy[i] + t.second;
            if(st[sx][sy] == true) continue;
            if(sx < 0 || sx >= n || sy < 0 || sy >= n) continue;
            if(alls[t.first][t.second] ==  alls[sx][sy]) continue;
            st[sx][sy] = true;
            q[ ++ tt] = {sx, sy};
            cnt ++;
        }
    }
    return cnt;
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            scanf("%d", &g[i][j]);
    while(m --)
    {
        int x, y;
        cin >> x >> y;
        int res = bfs(x, y);
        printf("%d\n", res);
    }
    return 0;
}

请用c++17进行编译!!!

2020/7/4 12:43
加载中...