求助!!
查看原帖
求助!!
677139
Dreams_Knight楼主2023/8/2 20:57
#include<bits/stdc++.h>
using namespace std;
int n , m , ans = 0;
const int dx[4] = {1 , -1 , 0 , 0};
const int dy[4] = {0 , 0 , -1 , 1};
int a[505][505];
void dfs(int x,int y){
	if(x < 0 || x >= n || y < 0 || y >= m || a[x][y]){
		return;
	}	
	a[x][y] = -1;
	for(int i = 0;i < 4;i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		dfs(nx , ny);
	}
}
int main(){
    cin >> n >> m;
    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
        	char s;
            cin >> s;
            if(s == '*'){
            	a[i][j] = 1;
			}
            else if(s == '0'){
            	a[i][j] = 0;
			}
        }
    }
    dfs(0 , 0);
    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            if(!a[i][j]){
            	ans++;
			}
        }
	}
    cout << ans;
    return 0;
}
2023/8/2 20:57
加载中...