我wh 2021-11-06 19:09 采纳率: 100%
浏览 39
已结题

这道c++的题,我不知道错那,帮我来看看。

题目:题意:有一块N×M的土地,雨后积起了水,有水标记为‘W’,干燥为‘.’。八连通的积水被认为是连接在一起的。请求出院子里共有多少水洼?




```#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[1001][1001];
int dx[8]={0,1,0,-1,-1,1,-1,1};
int dy[8]={1,0,-1,0,-1,-1,1,1}; 
int q[1000001][3];
int cnt;
void bfs(int x,int y)
{
    int h,t;
    h==t==1;
    q[t][1]=x;
    q[t][2]=y;
    a[x][y]='.';
    while(h<=t)
    {
        for(int i=0;i<8;i++)
        {
            int nx=q[h][1]+dx[i];
            int ny=q[h][2]+dy[i];
            if(a[nx][ny]=='W')
            {
                t++;
                q[t][1]=nx;
                q[t][2]=ny;
                a[nx][ny]='.';
            }
        }
        h++;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {    
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='W')
            {
                cnt++;
                bfs(i,j);
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}
  • 写回答

2条回答 默认 最新

  • 六年级初中生 2021-11-06 19:15
    关注

    离谱,刚刚做完这道题,我把代码给你看吧,你自己看看看哪里错了

    #include<bits/stdc++.h>
    using namespace std;
    
    int a[121][121];
    bool vst[121][121];
    int n, m;
    struct node{int x; int y;};
    
    int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
    int dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
    
    void bfs(int x, int y) {
        queue<node>q;
        q.push({x, y});
        vst[x][y] = true;
        while (q.size()) {
            node ft = q.front();
            q.pop();
            
            for (int i = 0; i < 8; ++i) {
                int xx = ft.x + dx[i];
                int yy = ft.y + dy[i];
                
                if (a[xx][yy] == 0) continue;
                if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
                if (vst[xx][yy]) continue;
            //    cout << xx << " " << yy << endl;
                vst[xx][yy] = true;
                q.push({xx, yy});
            }
        } 
    }
    
    int main(){
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                char c;
                cin >> c;
                if (c == 'W') a[i][j] = 1;
                else a[i][j] = 0;
            }
        }
        
        int cnt = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (!vst[i][j] && a[i][j] == 1) {
                    ++cnt;
                    bfs(i, j);
                }
            }
        }
        
    //    for (int i = 1; i <= n; ++i) {
    //        for (int j = 1; j <= m; ++j) {
    //            cout << vst[i][j];
    //        }
    //        cout << endl;
    //    }
        
        cout << cnt << endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月22日
  • 已采纳回答 11月14日
  • 创建了问题 11月6日

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真