BFS-1

池塘计数

题目:池塘计数
方法一:BFS

#include <bits/stdc++.h>
using namespace std;
const int N=1010,M=N*N;

#define x first
#define y second

typedef pair<int,int> PII;

char g[N][N];
int n,m;
PII q[M];
bool st[N][N];

void bfs(int sx,int sy)
{
    int hh=0,tt=0;
    q[0]={sx,sy};
    st[sx][sy]=true;
    while(hh<=tt)
    {
        PII t=q[hh++];
        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>=m) continue;
             if(g[i][j]=='.'||st[i][j]) continue;
             
             q[++tt]={i,j};
             st[i][j]=true;
         }
    }
}

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;
    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++;
      }
     cout<<res;
}

方法二:DFS

#include <bits/stdc++.h>
using namespace std;
const int N=1009;
int n,m;
char a[N][N];


void dfs(int x,int y)
{
    int dx[8]={1,1,1,0,0,-1,-1,-1};
    int dy[8]={-1,0,1,-1,1,-1,0,1};
    
    a[x][y]='.';
    for(int i=0;i<8;i++)
    {
       if(a[x+dx[i]][y+dy[i]]=='W')
       {
           dfs(x+dx[i],y+dy[i]);
       }
    }
}

int main()
{
    int ans=0;
    
    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')
            {
               
                dfs(i,j);
                ans++;
            }
        }
      
    }
    cout<<ans;
}

城堡问题

题目:城堡问题

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=50;
int g[N][N];
int st[N][N];
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int n,m;

int bfs(int i,int j)
{
    queue<PII> q;
    q.push({i,j});
    st[i][j]=true;
    int cnt=0;
    while(!q.empty())
    {
        PII t=q.front();
        q.pop();
        cnt++;
        for(int k=0;k<4;k++)
        {
            if(g[t.first][t.second]>>k&1) continue;
            int x=t.first+dx[k];
            int y=t.second+dy[k];
            if(st[x][y]) continue;
            q.push({x,y});
            st[x][y]=true;
        }
    }
    return cnt;
}

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

山峰和山谷

题目:山峰和山谷

#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N=1020;
typedef pair<int,int> PII;

int g[N][N];
bool st[N][N];

PII q[N*N];

int n;

void bfs(int x,int y,bool& hig,bool& dow)
{
    int hh=0,tt=0;
    q[0]={x,y};
    st[x][y]=true;
    while(hh<=tt)
    {
        PII t=q[hh++];
        for(int i=t.x-1;i<=t.x+1;i++)
         for(int j=t.y-1;j<=t.y+1;j++)
         {
             if(i<0||i>=n||j<0||j>=n) continue;
             if(g[i][j]>g[t.x][t.y])
                  hig=true;
             else if(g[i][j]<g[t.x][t.y])
                 dow=true;
            else if(!st[i][j])
            {
                q[++tt]={i,j};
                st[i][j]=true;
            }
         }
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
     for(int j=0;j<n;j++)
      scanf("%d",&g[i][j]);
      
     int h=0,d=0;
     for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
      {
          if(!st[i][j])
          { bool hig=false,dow=false;
              bfs(i,j,hig,dow);
              if(!hig)
               h++;
              if(!dow)
               d++;
          }
      }
     printf("%d %d\n",h,d);
}

迷宫问题

题目:迷宫问题

#include <bits/stdc++.h>
using namespace std;
#define x first 
#define y second
typedef pair<int,int> PII;

const int N=1000;

PII pre[N][N];
int g[N][N];
bool st[N][N];
int n;
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};

void bfs()
{
    queue<PII> q;
    q.push({n-1,n-1});
    st[n-1][n-1]=true;
    
    while(q.size())
    {
        PII t=q.front();
        q.pop();
        
        for(int i=0;i<4;i++)
        {
            int x=t.x+dx[i];
            int y=t.y+dy[i];
            if(x<0||x>=n||y<0||y>=n) continue;
            if(g[x][y]||st[x][y]) continue;
            st[x][y]=true;
            q.push({x,y});
            pre[x][y]=t;
        }
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
     for(int j=0;j<n;j++)
      cin>>g[i][j];
    
    bfs();
    
    PII end(0,0);
    
   while(true)
   {
       cout<<end.x<<' '<<end.y<<endl;
       if(end.x==n-1&&end.y==n-1)
        break;
        end=pre[end.x][end.y];
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴戴.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值