BFS 2 9 10TLE求助
  • 板块P1141 01迷宫
  • 楼主DrAlfred
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/4/2 07:50
  • 上次更新2023/10/28 04:53:24
查看原帖
BFS 2 9 10TLE求助
583610
DrAlfred楼主2022/4/2 07:50
#include<bits/stdc++.h>
#define point pair<int,int>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
static int n,m,gone[1001][1001],hans[1001][1001],rec[1001][1001];
static char ma[1001][1001];
inline bool bj(int x,int y){
	return x>0&&x<=n&&y>0&&y<=n&&(!gone[x][y]);
}
inline void bfs(int x,int y){
	int ans=1;
	queue<point> q;
	++gone[x][y];
	++rec[x][y];
	q.push(make_pair(x,y));
	while(!(q.empty())){
		point top=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int nx=top.first+dx[i];
			int ny=top.second+dy[i];
			if(ma[nx][ny]!=ma[top.first][top.second]&&bj(nx,ny)){
				++rec[x][y];
				++gone[nx][ny];
				q.push(make_pair(nx,ny));
			}
		}
	}
}
int main(int argc,const char *argv[]){
	ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>ma[i][j];
		}
	}
	int x,y;
	for(int i=1;i<=m;i++){
		memset(gone,0,sizeof(gone));
		cin>>x>>y;
		if(hans[x][y]){
			cout<<rec[x][y]<<endl;
		}
		else{
		    ++hans[x][y];
		    bfs(x,y);
		    cout<<rec[x][y]<<endl;
		}
	}
	return 0;
}
2022/4/2 07:50
加载中...