#3 WA 求助
  • 板块P1141 01迷宫
  • 楼主SymmFz
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/16 14:26
  • 上次更新2024/3/16 15:54:55
查看原帖
#3 WA 求助
1067945
SymmFz楼主2024/3/16 14:26

求助

代码如下:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int n,m;
char mapp[1100][1100];  // 地图 
int sx, sy;  // 初始坐标 
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int mapthrouth[1100][1100]; // 属于第几个连通图
int mapthrouth_sum[150000];   // 每个连通图连通的数量
int d=0; // 第几个连通图

queue <int> q;

bool check(int x, int y, int xx, int yy) {
    if (xx<0 || xx>=n || yy<0 || yy>=n)  return false;
    if (mapthrouth[xx][yy] > 0) return false;

    bool false0 = (mapp[x][y] == '0' && mapp[xx][yy] == '0') ? true : false;
    bool false1 = (mapp[x][y] == '1' && mapp[xx][yy] == '1') ? true : false;
    if (false0 || false1)  return false;

    return true;
}

void bfs(int x, int y) {
	// 处于连通图上 
    if (mapthrouth[x][y] > 0)  return;
    
    d++;
    q.push(x);
    q.push(y);
    mapthrouth[x][y] = d;
    mapthrouth_sum[d]++;

    while (!q.empty()) {
        int xx=q.front(); q.pop();
        int yy=q.front(); q.pop();

        for (int i=0; i<4; i++) {
            int xxx=xx+dx[i], yyy=yy+dy[i];
            if (!check(xx, yy, xxx, yyy))  continue;

            q.push(xxx);
            q.push(yyy);
            // 处理连通图
            mapthrouth[xxx][yyy] = d;
            mapthrouth_sum[d]++;
        }
    }
}

signed main() {
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> mapp[i][j];
        }
    }
    
    for (int i=0; i<n; i++) {
    	for (int j=0; j<n; j++) {
    		bfs(i, j);
		}
	} 
    
    for (int i=0; i<m; i++) {
        cin >> sx >> sy;
        sx = sx-1, sy=sy-1;  // 坐标从 0 开始 
		ans = mapthrouth_sum[mapthrouth[sx][sy]];
        cout << ans << endl;
    }
    return 0;
}
2024/3/16 14:26
加载中...