1 条题解

  • 1
    @ 2026-7-25 15:58:00
    #include<bits/stdc++.h>
    using namespace std;
    int w,h,ans;
    bool vis[25][25];
    int dx[]={0,1,0,-1};
    int dy[]={1,0,-1,0};
    char a[25][25];
    bool check(int x,int y){
    	if(x<=0 || y<=0 || x>w || y>h){
    		return false;
    	}
    	if(vis[x][y]){
    		return false;
    	}
    	if(a[x][y]=='#'){
    		return false;
    	}
    	return true;
    }
    void dfs(int x,int y){
    	for(int i=0;i<4;++i){
    		int nx,ny;
    		nx=x+dx[i];
    		ny=y+dy[i];
    		if(check(nx,ny)){
    			vis[nx][ny]=1;
    			ans++;
    			dfs(nx,ny);
    		} 
    	}
    	return;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);
    	while(cin>>h>>w){
    		if(w==0 && h==0){
    			return 0;
    		}
    		memset(vis,0,sizeof(vis));
    		int bx,by;
    		ans=1;
    
    		for(int i=1;i<=w;++i){
    			for(int j=1;j<=h;++j){
    				cin>>a[i][j];
    				if(a[i][j]=='@'){
    					bx=i;
    					by=j;
    				}
    			}
    		}
    		vis[bx][by]=true;
    		dfs(bx,by);
    		cout<<ans<<endl;
    	}
    }
    • 1

    信息

    ID
    189
    时间
    1000ms
    内存
    32MiB
    难度
    10
    标签
    递交数
    3
    已通过
    2
    上传者