2 条题解

  • 1
    @ 2021-10-18 21:27:06
    #include <stdio.h>
    #include <iostream>
    #include <queue> 
    using namespace std;
    
    int n , m;
    char a[200][200];
    struct node 
    {
    	int x,y;	
    };
    int dx[] = {1,0,-1,0};
    int dy[] = {0,1,0,-1};
    void bfs(int x, int y)
    {
    	queue<node> p;
    	p.push( (node){x,y} );
    	a[x][y] = '0';
    	while(!p.empty())
    	{
    		node t = p.front();
    		p.pop();
    		for(int i = 0 ; i < 4 ; i++)
    		{
    			x = t.x + dx[i];
    			y = t.y + dy[i];
    			if(x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '0')
    			{
    				p.push((node){x,y});
    				a[x][y] = '0';
    			}
    		}
    		 
    	}
    }
    int main()
    {
    	cin >>n >> m;
    	for(int i = 0 ;i < n ; i++)
    		cin >> a[i];
    	
    	int ans = 0;
    	for(int i = 0 ; i < n ; i++)
    	{
    		for(int j = 0 ; j < m ; j++)
    		{
    			if(a[i][j] != '0')
    			{
    				bfs(i,j);
    				ans++;
    			}
    		}
    	}
    	cout << ans << endl; 
    }
    

    信息

    ID
    1309
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    128
    已通过
    57
    上传者