Depth First Search (DFS)入门

#include<iostream>
using namespace std;
 int n,m;
 const int maxn=100+5;
 char s[maxn][maxn];
 int dir[8][2]=
 {
     1,1,
     -1,1,
     1,0,
     -1,0,
     1,-1,
     -1,-1,
     0,-1,
     0,1
 };
 void dfs(int x,int y)
 {
     s[x][y]='*';
     for(int i=0;i<8;i++)
     {
         int tx=x+dir[i][0],ty=y+dir[i][1];
         if(tx<0||tx>=n||ty<0||ty>=m)continue;
         if(s[tx][ty]=='@')dfs(tx,ty);
     }
     return ;
 }
int main()
{

   while(cin>>n>>m&&m)
   {
       int ans=0;
       for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        cin>>s[i][j];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
    {
        if(s[i][j]=='@')
        {
            dfs(i,j);
        ans++;
        }
    }
    cout<<ans<<endl;
   }
    return 0;
}

HDu1312Red and Black


直接贴代码了:

#include <bits/stdc++.h>
using namespace std;
const int maxn=100000+10;

char s[maxn/1000][maxn/1000];
int n,m;
int derict[4][2]=
{
    -1,0,
    1,0,
    0,1,
    0,-1,
};
int ans;
bool flag[maxn/1000][maxn];
void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+derict[i][0];
        int yy=y+derict[i][1];
        if(xx>=0 && xx<n && yy>=0 && yy<m && flag[xx][yy] && s[xx][yy] == '.')
        {
            ans++;
            flag[xx][yy] = false;
            dfs(xx,yy);
        }
    }

    return ;
}

int main()
{


    while(~scanf("%d%d",&m,&n) && (n || m))
    {
        memset(flag , true ,sizeof(flag));
        for(int i=0;i<n ;i++)
            scanf("%s",s[i]);
        ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j] == '@')
                {
                    flag[i][j] = false;
                    ans++;
                    dfs(i,j);
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=100000+10;

char s[maxn/1000][maxn/1000];
int n,m;
int derict[4][2]=
{
    -1,0,
    1,0,
    0,1,
    0,-1,
};
int ans;
void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+derict[i][0];
        int yy=y+derict[i][1];
        if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy] == '.')
        {
            ans++;
            s[xx][yy ] = '*';
            dfs(xx,yy);
        }
    }

    return ;
}

int main()
{


    while(~scanf("%d%d",&m,&n) && (n || m))
    {
        for(int i=0;i<n ;i++)
            scanf("%s",s[i]);
        ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j] == '@')
                {
                    s[i][j]= '*';
                    ans++;
                    dfs(i,j);
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
Lake Counting  poj2386

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100000+10;

char s[maxn/1000][maxn/1000];
int n,m;
int derict[8][2]=
{
    -1,0,
    1,0,
    0,1,
    0,-1,
    1,1,
    1,-1,
    -1,1,
    -1,-1,
};
void dfs(int x,int y)
{
    for(int i=0;i<8;i++)
    {
        int xx=x+derict[i][0];
        int yy=y+derict[i][1];
        if(xx>=0 && xx < n&& yy>=0 && yy<m && s[xx][yy] == 'W')
        {
            s[xx][yy] = '.';
            dfs(xx,yy);
        }
    }
    return ;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i =0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j] == 'W')
                {
                    s[i][j]= '.';
                    dfs(i,j);
                     ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

小X学游泳   百炼10141

#include <bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
using namespace std;
const int maxn=100000+10;
char s[maxn/1000][maxn/1000];
int temp;
char tempc;
int n,m;
int derict[4][2]=
{
    -1,0,
    1,0,
    0,-1,
    0,1,
};
bool flag[maxn/1000][maxn/1000];

void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+derict[i][0];
        int yy=y+derict[i][1];
        if(xx>=0 && xx<n && yy>=0 && yy < m && flag[xx][yy] && s[xx][yy ] == tempc)
        {
            flag[xx][yy] = false;
            temp++;
            dfs(xx,yy);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        memset(flag,true ,sizeof(flag));
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0 ;i <n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(flag[i][j])
                {
                    temp=1;
                    tempc=s[i][j];
                    flag[i][j] = false;
                    dfs(i,j);
//                    cout<<i+1<<" "<<j+1<<" "<<temp<<" "<<tempc<<endl;
                    if(temp > ans)
                        ans = temp;
                }

            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include<iostream>
using namespace std;
const int maxn=100+5;
char map[maxn][maxn];
int dir[4][2]=
{
    1,0,-1,0,0,-1,0,1
};
int n,m;
int ans;
void  dfs(int x,int y,int k)
{
     map[x][y]=0;
     for(int i=0;i<4;i++)
     {
         int dx=x+dir[i][0];
         int dy=y+dir[i][1];
         if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]==k)
         {
             ans++;
             dfs(dx,dy,k);
             //cout<<ans<<endl;
         }
     }
}
int main()
{
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
            cin>>map[i];
        int maxm=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            ans=0;
            if(map[i][j])
                {
                    ans++;dfs(i,j,map[i][j]);}
            //cout<<ans<<endl;
            if(ans>maxm)
                maxm=ans;
        }
        cout<<maxm<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值