3 条题解

  • 1
    @ 2022-11-8 16:27:45

    广搜写法

    #include <bits/stdc++.h>
    using namespace std;
    int n, m, ans;
    char g[115][115];
    //广搜
    bool vis[115][115];
    struct Point
    {
        int x, y;
    };
    queue<Point> q;
    int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
    int dy[] = {1, -1, 1, -1, 0, 0, 1, -1};
    void bfs(int sx, int sy)
    {
        //初始化
        memset(vis, false, sizeof(vis));
        while (!q.empty())
            q.pop();
        //起点入队
        q.push((Point){sx, sy});
        vis[sx][sy] = true;
        while (!q.empty())
        {
            //取出队头
            Point now = q.front();
            q.pop();
            //枚举能到达的所有点
            for (int i = 0; i < 8; i++)
            {
                int nx = now.x + dx[i];
                int ny = now.y + dy[i];
                // (now.x, now.y) -> (nx, ny)
                if (1 <= nx && nx <= n &&
                    1 <= ny && ny <= m &&
                    !vis[nx][ny] &&
                    g[nx][ny] == 'W')
                {
                    q.push((Point){nx, ny});
                    g[nx][ny] = '.';
                    vis[nx][ny] = true;
                }
            }
        }
    }
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> g[i][j];
        ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (g[i][j] == 'W')
                {
                    ans++;
                    //把这个点对应的水洼抽干
                    bfs(i, j);
                }
        cout << ans << endl;
        return 0;
    }
    
    • 0
      @ 2023-6-3 10:40:23
      #include<bits/stdc++.h>
      using namespace std;
      int n,m;
      char g[115][115];
      queue<int> qx,qy;
      int dx[8] = {-1,-1,-1,0,0,1,1,1};
      int dy[8] = {-1,0,1,-1,1,-1,0,1};
      int main()
      {
      	cin>>n>>m;
      	for(int i=1;i<=n;i++)
      		for(int j=1;j<=m;j++)
      			cin>>g[i][j];
      	int ans=0;
      	for(int i=1;i<=n;i++)
      		for(int j=1;j<=m;j++){
      			if(g[i][j]=='W'){
      				ans++;
      				//把所有与 (i,j) 相连的水全都去掉		
      				//起点入队
      				qx.push(i);
      				qy.push(j);
      				g[i][j]='.';
      				//只要队列非空(还有需要处理的水)
      				while(qx.size()>0){
      					//取出队头 
      					int nowX = qx.front();qx.pop();
      					int nowY =qy.front();qy.pop(); 
      					//枚举所有队头一步能走到的位置,如果没走过,就走一下
      					for(int i=0;i<8;i++){
      						int nxtX = nowX+dx[i];
      						int nxtY = nowY+dy[i];
      						if(1<=nxtX&&nxtX<=n&&1<=nxtY&&nxtY<=m&&g[nxtX][nxtY]=='W')
      						{
      							g[nxtX][nxtY]='.';
      							qx.push(nxtX);
      							qy.push(nxtY);
      						}						
      					} 
      				}
      				 
      			}			
      		} 
      	cout<<ans<<"\n"; 
      	return 0;
      }
      
      • 0
        @ 2022-11-11 17:52:53

        深搜写法

        #include<bits/stdc++.h>
        using namespace std;
        int n,m;
        char g[115][115];
        int dx[] = {-1,-1,-1,0,0,1,1,1};
        int dy[] = {-1,0,1,-1,1,-1,0,1};
        //把 (xy) 对应的联通块清空 
        void dfs(int x,int y){
        	g[x][y]='.';
        	for(int i=0;i<8;i++){
        		int nx=x+dx[i];
        		int ny=y+dy[i];
        		if(1<=nx&&nx<=n&&
        		   1<=ny&&ny<=m&&
        		   g[nx][ny]=='W'){
        			dfs(nx,ny);   	
        		}
        	}	
        }
        int main()
        {
        	cin>>n>>m;
        	for(int i=1;i<=n;i++)
        		for(int j=1;j<=m;j++)
        			cin>>g[i][j];
        	int ans=0;
        	for(int i=1;i<=n;i++)
        		for(int j=1;j<=m;j++)
        			if(g[i][j]=='W'){
        				ans++;
        				dfs(i,j);
        			}
        	cout<<ans<<endl;
        	return 0;
        }
        
        • 1

        信息

        ID
        468
        时间
        1000ms
        内存
        128MiB
        难度
        5
        标签
        (无)
        递交数
        160
        已通过
        62
        上传者