6 条题解

  • 1
    @ 2022-10-15 16:38:02
    #include <iostream>
    using namespace std;
    
    int dx[8] = {-1,  -1, -1,   0,  0,   1,  1,  1};
    int dy[8] = {-1,  0,   1,  -1,  1,  -1,  0,  1};
    
    int n, m;
    char a[105][105];
    
    void dfs(int x, int y,int cnt)
    {
    	// 染色标记
    	a[x][y] = '.';
    	for (int i = 0; i < 8; i++)
    	{
    		int nx = x + dx[i];
    		int ny = y + dy[i];
    		if (nx <= n and nx >= 1 and 1 <= ny and ny <= m and a[nx][ny] == 'W'){
    			dfs(nx,ny,cnt+1);
    		}
    	}
    }
    
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
        	for (int j = 1; j <= m; j++)
        	{
    	    	cin >> a[i][j];
        	}
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
        	for (int j = 1; j <= m; j++)
        	{
        		// 寻找连通块,记录个数
        		if (a[i][j] == 'W')
        		{
        			dfs(i,j,1);
        			ans++;
        		}
        	}
        }
        cout << ans << endl;
        return 0;
    }
    

    信息

    ID
    2617
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    52
    已通过
    25
    上传者