求连通块问题(flood fill、dfs)

AcWing 1097. 池塘计数

在这里插入图片描述
bfs(flood fill):

#include<iostream>
#include<queue>
using namespace std;
const int N = 1005;

#define x first
#define y second
typedef pair<int,int> PII;
char g[N][N];
queue<PII> q;
int st[N][N];
int n,m;

void bfs(int sx,int sy)
{
    q.push({sx,sy});
    st[sx][sy] = 1;
    
    while(q.size())
    {
       PII t = q.front();
       q.pop();
       for(int i=t.x-1; i<=t.x+1; i++)
        for(int j=t.y-1; j<=t.y+1; j++)
        {
            if(i==t.x && j==t.y) continue; //遍历周围8个格子,将中间格子剔除
            if(i<0 || i>=n || j<0 || j>=m) continue;
            if(st[i][j] || g[i][j]=='.') continue;
            
            q.push({i,j});
            st[i][j] = 1;
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++) 
      scanf("%s",g[i]);
    
    int res = 0;
    for(int i=0; i<n; i++)
     for(int j=0; j<m; j++)
         if(g[i][j]=='W' && !st[i][j])//为水且没有标记过
          {
             bfs(i,j);
             res++;
          } 
          
    printf("%d",res);
    return 0;
}

dfs:

#include<iostream>

using namespace std;

const int N = 1005;
char g[N][N];
int st[N][N];
int n,m;

void dfs(int x,int y)
{
    st[x][y] = 1;
    g[x][y] = '.';
    
    for(int i=x-1; i<=x+1; i++)
     for(int j=y-1; j<=y+1; j++)
     {
        if(i==x && j==y) continue;
        if(i<0 || i>=n || j<0 || j>=m) continue;
        if(st[i][j] || g[i][j]=='.') continue;
        
        dfs(i,j);
     }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++) scanf("%s",g[i]);
    
    int res = 0;
    for(int i=0; i<n; i++)
     for(int j=0; j<m; j++)
     if(g[i][j] == 'W' && !st[i][j])
     {
         dfs(i,j);
         res++;
     }
     printf("%d",res);
}

AcWing 1098. 城堡问题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<queue>
#define x first
#define y second

using namespace std;

const int N = 55;

typedef pair<int,int> PII;
int dx[4] = {0,-1,0,1},dy[4] = {-1,0,1,0};

int g[N][N];
queue<PII> q;
int st[N][N];
int n,m;

int bfs(int sx,int sy)
{
    q.push({sx,sy});
    st[sx][sy] = 1;
    
    int area = 0;
     while(q.size())
    {
        PII t = q.front();
        q.pop();
        area++;
        
        for(int i=0; i<4; i++)
        {
            int a = t.x + dx[i], b = t.y + dy[i];
            if(a<0 || a>=n || b<0 || b>=m) continue;
            if(st[a][b]) continue;
            if(g[t.x][t.y] >> i & 1) continue;//有墙
            
            q.push({a,b});
            st[a][b] = 1;
        }
    }
    return area;
}

int main()
{
    cin >> n >> m;
    for(int i=0; i<n; i++)
     for(int j=0; j<m; j++)
      cin >> g[i][j];
    
    int res = 0,area = 0;
    for(int i=0; i<n; i++)
     for(int j=0; j<m; j++)
      if(!st[i][j])
      {
          area = max(area,bfs(i,j));
          res++;
      }
    cout << res << endl << area;
    return 0;
}

AcWing 1106. 山峰和山谷

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<queue>
#define x first
#define y second
using namespace std;

typedef pair<int,int> PII;

const int N = 1005;
int g[N][N];
queue<PII> q;
int st[N][N];
int n;

void bfs(int sx,int sy,bool &has_higher,bool &has_lower)
{
    q.push({sx,sy});
    st[sx][sy] = 1;
    
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        
        for(int i=t.x-1; i<=t.x+1; i++)
         for(int j=t.y-1; j<=t.y+1; j++)
         {
             if(i==t.x && j==t.y) continue;
             if(i<0 || i>=n || j<0 || j>=n) continue;
             if(g[i][j] != g[t.x][t.y])
             {
                 if(g[i][j] > g[t.x][t.y]) has_higher = true;
                 else has_lower = true;
             }
             else if(!st[i][j])
             {
                 q.push({i,j});
                 st[i][j] = 1;
             }
         }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
     for(int j=0; j<n; j++)
      scanf("%d",&g[i][j]);
    
    int peak = 0,valley = 0; //山峰 、山谷
    for(int i=0; i<n; i++)
     for(int j=0; j<n; j++)
      if(!st[i][j])
      {
         bool has_higher = false, has_lower = false;
         bfs(i,j,has_higher,has_lower);
         if(!has_higher) peak++;
         if(!has_lower) valley++;
      }
    printf("%d %d",peak,valley); 
}

AcWing 1112. 迷宫

在这里插入图片描述
bfs求解(Flood Fill):

#include<iostream>
#include<queue>
#include<cstring>
#define x first
#define y second

using namespace std;

typedef pair<int,int> PII;

const int N = 105;

char g[N][N];
int st[N][N];
int dx[4] = {0,1,0,-1}, dy[4] = {-1,0,1,0};

int k,n;
int x1,y1,x2,y2;

int bfs()
{
    if(g[x1][y1] == '#') return 0;
    queue<PII> q;
    q.push({x1,y1});
    st[x1][y1] = 1;
    
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        
        for(int i=0; i<4; i++)
        {
            int a = t.x + dx[i], b = t.y + dy[i];
            if(a<0 || a>=n || b<0 || b>=n) continue;
            if(g[a][b] == '#' || st[a][b]) continue;
            
            q.push({a,b});
            st[a][b] = 1;
        }
    }
    return st[x2][y2] == 1;
}

int main()
{
    cin >> k;
    while(k--)
    {
        cin >> n;
        for(int i=0; i<n; i++) cin >> g[i];
        cin >> x1 >> y1 >> x2 >> y2;
        memset(st,0,sizeof st);
        if(bfs()) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

dfs求解:

#include<iostream>
#include<cstring>
#define x first
#define y second

using namespace std;

const int N = 105;
char g[N][N];
int st[N][N];
int n,k;
int x1,y1,x2,y2;

int dx[4] = {0,-1,0,1}, dy[4] = {-1,0,1,0};

int dfs(int x,int y)
{
    if(g[x][y] == '#') return 0;
    if(x == x2 && y == y2) return 1;
    st[x][y] = 1;
    
    for(int i=0; i<4; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        
        if(a<0 || a>=n || b<0 || b>=n) continue;
        if(g[a][b] == '#' || st[a][b]) continue;
        
        if(dfs(a,b)) return 1;
    }
    return 0;
}

int main()
{
    
    cin >> k;
    while(k--)
    {
        cin >> n;
        for(int i=0; i<n; i++) cin >> g[i];
        cin >> x1 >> y1 >> x2 >> y2;
        
        memset(st,0,sizeof st);
        
        if(dfs(x1,y1)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

AcWing 1113. 红与黑

在这里插入图片描述
bfs求解(Flood Fill):

#include<iostream>
#include<queue>
#include<cstring>
#define x first
#define y second

using namespace std;

typedef pair<int,int> PII;

const int N = 25;
char g[N][N];
int st[N][N];
int n,m;

int dx[4] = {0,-1,0,1}, dy[4] = {-1,0,1,0};

int bfs(int x,int y)
{
    queue<PII> q;
    q.push({x,y});
    st[x][y] = 1;
    
    int res = 1;
    while(q.size())
    {
        PII t = q.front();
        q.pop();
        
        for(int i=0; i<4; i++)
        {
            int a = t.x + dx[i], b = t.y + dy[i];
            
            if(a<0 || a>=n || b<0 || b>=m) continue;
            if(g[a][b] !='.' || st[a][b]) continue;
            
            res++;
            q.push({a,b});
            st[a][b] = 1;
           
        }
    }
    return res;
}

int main()
{
    while(cin >> m >> n)
    {
        if(m==0 && n==0) break; 
        memset(st,0,sizeof st);
        for(int i=0; i<n; i++) cin >> g[i];
        
        for(int i=0; i<n; i++)
         for(int j=0; j<m; j++)
          if(g[i][j] == '@')
          {
              cout<<bfs(i,j)<<endl;
              break;
          }
    }
}

dfs求解:

#include<iostream>
#include<cstring>
#define x first
#define y second

using namespace std;


const int N = 25;
char g[N][N];
int st[N][N];
int n,m;

int dx[4] = {0,-1,0,1}, dy[4] = {-1,0,1,0};

int dfs(int x,int y)
{
    int res = 1;
    st[x][y] = 1;
    for(int i=0; i<4; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        
        if(a<0 || a>=n || b<0 || b>=m) continue;
        if(g[a][b]!='.' || st[a][b]) continue;
        
        res += dfs(a,b);
    }
    return res;
}

int main()
{
    while(cin >> m >> n)
    {
        if(m==0 && n==0) break; 
        memset(st,0,sizeof st);
        for(int i=0; i<n; i++) cin >> g[i];
        
        for(int i=0; i<n; i++)
         for(int j=0; j<m; j++)
          if(g[i][j] == '@')
          {
              cout<<dfs(i,j)<<endl;
              break;
          }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值