70pts求调,悬2关,急急急
  • 板块P1141 01迷宫
  • 楼主TankGuo
  • 当前回复12
  • 已保存回复13
  • 发布时间2025/7/24 07:41
  • 上次更新2025/7/24 12:28:50
查看原帖
70pts求调,悬2关,急急急
936437
TankGuo楼主2025/7/24 07:41

7070pts,TLE三个。

路过的大佬能不能在我的代码上改一下再加一点注释。

急急急

#include<bits/stdc++.h>
using namespace std;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n,m,ans,x,y; char c;
bool mp[1010][1010],flag[1010][1010];
int ansxy[1010][1010];
void BFS(){
	queue<int> qx,qy;
	qx.push(x); qy.push(y);
	while(!qx.empty()){
		int nx = qx.front();
		int ny = qy.front();
		qx.pop(); qy.pop();
		for(int i = 0;i < 4;i++){
			int tx = nx + dx[i];
			int ty = ny + dy[i];
			if(tx < 1 || tx > n || ty < 1 || ty > n
			|| flag[tx][ty]) continue;
			if(mp[nx][ny] == mp[tx][ty]) continue;
			flag[tx][ty] = 1;
			qx.push(tx); qy.push(ty);
		}
	} return ;
} int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    cin >> n >> m;
	for(int i = 1;i <= n;i++)
	for(int j = 1;j <= n;j++){
		ansxy[i][j] = -1;
		cin >> c;
		mp[i][j] = c - '0';
	} while(m--){
		cin >> x >> y;
		if(ansxy[x][y] != -1){
		 	cout << ansxy[x][y] << "\n";
		 	continue;
		} for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++){
			flag[i][j] = 0;
		} flag[x][y] = 1;
		BFS(); ans = 0;
		for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++){
			ans += flag[i][j];
		} cout << ans << "\n";
		ansxy[x][y] = ans;
	} return 0;
}
2025/7/24 07:41
加载中...