70pts TLE求助
  • 板块P1141 01迷宫
  • 楼主SANJIAOJIE
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/11/3 11:05
  • 上次更新2024/11/3 14:17:36
查看原帖
70pts TLE求助
1074157
SANJIAOJIE楼主2024/11/3 11:05
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,q,x,y,ans;
char a[N][N];bool f[N][N];
int fx[5]={0,1,-1,0,0};
int fy[5]={0,0,0,1,-1};
int dfs(int x,int y,char c){
    for(int i=1;i<=4;i++){
        int tx=x+fx[i],ty=y+fy[i];
        if(tx>=1&&ty>=1&&tx<=n&&ty<=n&&f[tx][ty]==false){
            if(c=='0'&&a[tx][ty]=='1'){
                ans++;
				f[tx][ty]=true;
				dfs(tx,ty,a[tx][ty]);
            }
            if(c=='1'&&a[tx][ty]=='0'){
            	ans++;
				f[tx][ty]=true;
				dfs(tx,ty,a[tx][ty]);
			}
        }
    }
    return ans;
}
int main(){
	cin>>n>>q;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    while(q--){
        cin>>x>>y;
        memset(f,false,sizeof(f));
        ans=1;
        f[x][y]=true;
        cout<<dfs(x,y,a[x][y])<<"\n";
    }
	return 0;
}
2024/11/3 11:05
加载中...