求助对一个对一个点 0tps(DFS)

P1141 01迷宫

3_14 @ 2025-01-03 15:41:05

#include<bits/stdc++.h>
#define Freopen(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
#define lcm(x,y) x/__gcd(x,y)*y;
#define lb(x) (x&-x)
#define lf(x) x==0?1:0
#define str to_string
#define Made return
#define by 0
#define _3_14 ;
using namespace std;
const int MAX=1e3+1;
const double EPS=1e-6;
int n,t,mp[MAX][MAX],fx[4][2]={1,0,0,1,-1,0,0,-1},ans=1;
bool book[MAX][MAX];
void dfs(int x,int y,int now){
    book[x][y]=true;
    bool flag=true;
    for(int i=0;i<4;i++){
        int xx=x+fx[i][0],yy=y+fx[i][1];
        if(xx<1||yy<1||xx>n||yy>n||book[xx][yy]||mp[xx][yy]==now)continue;
        flag=false;
        dfs(xx,yy,lf(now));
        ans++;
    }
    if(flag)return;
}
string s;
int main(){
    cin>>n>>t;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=1;j<=n;j++)mp[i][j]=s[j-1];
    }
    while(t--){
        int x,y;
        cin>>x>>y;
        dfs(x,y,mp[x][y]);
        cout<<ans<<'\n';
    }
    Made by _3_14
}

by do_it_tomorrow @ 2025-01-03 15:45:15

不是您在写什么?


|