80分,第2和第9个点TLE求调(马上要去CSP-S)了
  • 板块P1141 01迷宫
  • 楼主wanglovezl
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/10/26 08:27
  • 上次更新2024/10/26 10:58:07
查看原帖
80分,第2和第9个点TLE求调(马上要去CSP-S)了
1467028
wanglovezl楼主2024/10/26 08:27
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a,b;
int cnt=0;
int kmp;
string s;
int xr[]={0,0,1,-1};
int yr[]={1,-1,0,0};
bool vis[1005][1005];
int Map[1005][1005];
int my_map[1005][1005];
int res[1000005];
bool check(int x,int y,int bx,int by){
	if(vis[x][y]==true) return false;
	if(x<1||x>n||y<1||y>n) return false;
	if(Map[x][y]==Map[bx][by]) return false;
	return true;
}
void dfs(int x,int y,int cnt){
	vis[x][y]=true;
	my_map[x][y]=cnt;
	for(int op=0;op<4;op++){
		int nx=x+xr[op];
		int ny=y+yr[op];
		if(check(nx,ny,x,y)==false) continue;
		my_map[nx][ny]=cnt;
		dfs(nx,ny,cnt);
	}
	return;
}
int main(){
	memset(my_map,0,sizeof(my_map));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		cin>>s;
		for(int j=0;j<n;j++){
		    Map[i][j+1]=s[j]-'0';
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(my_map[i][j]==0){
				memset(vis,false,sizeof(vis));
				cnt++;
				dfs(i,j,cnt);
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			res[my_map[i][j]]++;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d",&a,&b);
		cout<<res[my_map[a][b]]<<endl;
	}
	return 0;
}

#include<bits/stdc++.h> using namespace std; int n,m; queue Q; int a,b; int cnt=0; int kmp; string s; int xr[]={0,0,1,-1}; int yr[]={1,-1,0,0}; bool vis[1005][1005]; int Map[1005][1005]; int my_map[1005][1005]; int res[1000005]; bool check(int x,int y,int bx,int by){ if(vis[x][y]==true) return false; if(x<1||x>n||y<1||y>n) return false; if(Map[x][y]==Map[bx][by]) return false; return true; } void dfs(int x,int y,int cnt){ vis[x][y]=true; my_map[x][y]=cnt; for(int op=0;op<4;op++){ int nx=x+xr[op]; int ny=y+yr[op]; if(check(nx,ny,x,y)==false) continue; my_map[nx][ny]=cnt; dfs(nx,ny,cnt); } return; } int main(){ memset(my_map,0,sizeof(my_map)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ cin>>s; for(int j=0;j<n;j++){ Map[i][j+1]=s[j]-'0'; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(my_map[i][j]==0){ memset(vis,false,sizeof(vis)); cnt++; dfs(i,j,cnt); } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ res[my_map[i][j]]++; } } for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); cout<<res[my_map[a][b]]<<endl; } return 0; }

2024/10/26 08:27
加载中...