HDU 1198 Farm Irrigation



题意:有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。

题解:联通块类型题,关键是预处理好给定的几个图形,按照4位二进制保存

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-4
#define maxn 100010
#define MOD 1000000007
char a[11][5]= {"1010","1001","0110","0101","1100","0011","1011","1110","0111","1101","1111"};
int s[550][550];
int vis[550][550],m,n;
void DFS(int x,int y)
{
    vis[x][y] = 1;
    int k = s[x][y];
    if(x > 0)
    {
        int kk = s[x-1][y];
        if(a[k][0] == '1' && a[kk][1] == '1' && !vis[x-1][y])
            DFS(x-1,y);
    }
    if(y > 0)
    {
        int kk = s[x][y-1];
        if(a[k][2] == '1' && a[kk][3] == '1' && !vis[x][y-1])
            DFS(x,y-1);
    }
    if(x < m-1)
    {
        int kk = s[x+1][y];
        if(a[k][1] == '1' && a[kk][0] == '1' && !vis[x+1][y])
            DFS(x+1,y);
    }
    if(y < n-1)
    {
        int kk = s[x][y+1];
        if(a[k][3] == '1' && a[kk][2] == '1' && !vis[x][y+1])
            DFS(x,y+1);
    }
}
int main()
{
    int t;
    //scanf("%d",&t);
    while(scanf("%d%d",&m,&n) && m > 0)
    {
        getchar();
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                char c;
                scanf("%c",&c);
                s[i][j] = c - 'A';
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        int ans = 0;
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(!vis[i][j])
                {
                    DFS(i,j);
                    ans++;
                }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(搜索—DFS)