bfs全WA求调
查看原帖
bfs全WA求调
1090508
511017802yrb楼主2024/4/1 21:59
#define _CRT_SECURE_NO_WARNINGS 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
#define PI 3.1415926
struct p {
	int x;
	int y;
};
int n;
int a[1001][1001] = { 0 };
bool is[1001][1001] = { 0 };
int ans = 0;
queue<p> Q;
p pp;
p tmkp;
int posi[4][2] = { 0,1,0,-1,1,0,-1,0 };
void bfs() {
	while (!Q.empty()) {
		p tmp = Q.front();
		is[tmp.x][tmp.y] = 1;
		Q.pop();
		int re = a[tmp.x][tmp.y];
		for (int i = 0; i < 4; i++) {
			int ntx = tmp.x + posi[i][0];
			int nty = tmp.y + posi[i][1];
			if (!is[ntx][nty] && ((re + a[ntx][nty]) == 1) && ntx >= 1 && ntx <= n && nty >= 1 && nty <= n) {
				tmkp.x = ntx;
				tmkp.y = nty;
				Q.push(tmkp);
				ans++;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	int n, m;
	void bfs();
	cin >> n >> m;
	getchar();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			char t1;
			scanf("%c", &t1);
			a[i][j] = t1 - '0';
		}
		getchar();
	}
	
	while (m--) {
		ans = 0;
		cin >> pp.x >> pp.y;
		Q.push(pp);
		ans++;
		bfs();
		cout << ans << '\n';
	}
}

2024/4/1 21:59
加载中...