BFS 70tps,TLE in 2 9 10,求求调试
  • 板块P1141 01迷宫
  • 楼主SunsetVoice
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/12/25 17:30
  • 上次更新2023/10/24 06:39:00
查看原帖
BFS 70tps,TLE in 2 9 10,求求调试
659460
SunsetVoice楼主2022/12/25 17:30
#include<bits/stdc++.h>
using namespace std;
int b[1001][1001] = {0};
int xp[5] = {0,-1,0,0,1},yp[5] = {0,0,-1,1,0},ans;
int ma[1001][1001] = {0};
int n,m,a,bs;

queue<int>x;
queue<int>y;

bool check(int xx,int yy){
	return xx<=n and xx>=1 and yy<=n and yy>=1 and b[xx][yy]==0;
}
void bfs(int sx,int sy){
	//cout<<sx<<" "<<sy<<endl;
	x.push(sx);
	y.push(sy);
	ans = 0;
	b[sx][sy] = 1;
	int newx,newy;
	while(x.empty()==false){
		for(int i = 0;i<=5;i++){
			newx = x.front()+xp[i];
			newy = y.front()+yp[i];
			if(check(newx,newy) and abs(ma[x.front()][y.front()]-ma[newx][newy])==1){
				x.push(newx);
				y.push(newy);
				b[newx][newy] = 1;
			}
		}
		x.pop();
		y.pop();
	}
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=n;j++){
			if(b[i][j]==1)ans++;
		//	cout<<b[i][j]<<" ";
		}
		//cout<<endl;
	}
}
void clear(){
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=n;j++){
			b[i][j] = 0;
		}
	}
	while(!x.empty())x.pop();
	while(!y.empty())y.pop();
}
int main(){
	char g;
	
	cin>>n>>m;
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=n;j++){
			cin>>g;
			if(g=='1')ma[i][j] = 1;
			else ma[i][j] = 0;
		//	cout<<ma[i][j]<<" ";
		}
		//cout<<endl;
	}
	for(int i = 1;i<=m;i++){
		clear();
		scanf("%d %d",&a,&bs);
		bfs(a,bs);
		printf("%d\n",ans);
	}
	system ("pause");
//	return 0;
}

2022/12/25 17:30
加载中...