3个TLE,加了快读快写,谢谢大佬
查看原帖
3个TLE,加了快读快写,谢谢大佬
450893
yangyuanxi44楼主2021/8/9 17:43

3个TLE,加了快读快写,吸氧RE了,谢谢大佬

#include<bits/stdc++.h>
using namespace std;
int a[1005][1005];
bool vis[1005][1005];
const int dx[5] = {0,1, 0, 0, -1};
const int dy[5] = {0,0, 1, -1, 0};
int n,m;
int ans;
queue<pair<int, int> > Q;
inline int read(){
	int s=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*f;
}
inline void write(int x){
    if(x<0){
    	putchar('-');
		x=-x;
	}
    if(x>9) 
		write(x/10);
    putchar(x%10+'0');
}
int main(){
	cin>>n>>m;
	for(int i=1 ; i<=n ; i++)
	    for(int j=1 ; j<=n ; j++)
	        scanf("%1d",&a[i][j]);
	for(int i=1 ; i<=m ; i++){
		int x=read(),y=read();
		Q.push(make_pair(x,y)); 
		vis[x][y]=true; 
		while(!Q.empty()){
			int u=Q.front().first,v=Q.front().second;Q.pop();
			for(int j=1 ; j<=5 ; j++){
				int tx=u+dx[j],ty=v+dy[j];
				if((tx<1)||(ty<1)||(tx>n)||(ty>n)||(a[tx][ty]==a[u][v])||(vis[tx][ty]==true)) continue;
				ans++;
				Q.push(make_pair(tx,ty));
				//cout<<tx<<" "<<ty<<endl;
				vis[tx][ty]=true; 
			}
		}
		write(ans+1);
		puts("");
		memset(vis,false,sizeof(vis));
		ans=0; 
	}
	return 0;
}

2021/8/9 17:43
加载中...