HDU - 1198Farm Irrigation (BFS|DFS|并查集)

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10263 Accepted Submission(s): 4512

Problem Description

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

then the water pipes are distributed like

Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

Input

There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of ‘A’ to ‘K’, denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample Input

2 2
DK
HF

3 3
ADC
FJK
IHE

-1 -1

Sample Output

2
3

Author

ZHENG, Lu

Source

Zhejiang University Local Contest 2005

Recommend

Ignatius.L | We have carefully selected several similar problems for you: 1272 1232 1213 1856 1811

题意:

图加载不出给原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1198

主要算是模拟题,问有多少个连通快,只不过连通的方式是要水管对应相连,那么就对11个字母打一张左上右下是否可行的表,vaild[11][4],在记对于某个方向是否可以到这种连通块的ok[4][11]的表(其实可以只需要vaild),最后就是普通的BFS或者DFS求连通快,并查集其实也一样。。。

作死写了并查集

#include
#include
#include
using namespace std;
const int  maxn=11,maxm=50+5;
int vaild[11][4]={{1,1,0,0},
                  {0,1,1,0},
                  {1,0,0,1},
                  {0,0,1,1},
                  {0,1,0,1},
                  {1,0,1,0},
                  {1,1,1,0},
                  {1,1,0,1},
                  {1,0,1,1},
                  {0,1,1,1},
                  {1,1,1,1}};
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};

int ok[4][11]={{0,1,0,1,0,1,1,0,1,1,1},
               {0,0,1,1,1,0,0,1,1,1,1},
               {1,0,1,0,0,1,1,1,1,0,1},
               {1,1,0,0,1,0,1,1,0,1,1}};

char s[maxm];
int v[maxm][maxm],size,fa[maxm*maxm],n,m;

inline int get(int u)
{
  if(fa[u]==u)return u;
  return fa[u]=get(fa[u]);
}

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    if(n<0&&m<0)break;
    for(int i=1;i<=m*n;++i)fa[i]=i;
    for(int i=1;i<=n;++i)
    {
      scanf("%s",s+1);
      for(int j=1;j<=m;++j)v[i][j]=s[j]-'A';
    }

    size=n*m;
    for(int i=1;i<=n;++i)
    {
      for(int j=1;j<=m;++j)
      {
        int id=v[i][j],x=i,y=j,now=(i-1)*m+j;
        int fx=get(now);
        for(int k=0;k<4;++k)
        if(vaild[id][k])
        {
          int tx=x+dx[k],ty=y+dy[k];
          if(tx<0||tx>n||ty<0||ty>m)continue;
          if(!ok[k][v[tx][ty]])continue;

          int fy=(tx-1)*m+ty;
          fy=get(fy);
          //fx=get(now);
          if(fx!=fy)
          {
            size--;
            fa[fy]=fx;
            //printf("i=%d   j=%d  tx=%d   t=%d\n",i,j,tx,ty);
          }
        }
      }
    }

    printf("%d\n",size);
  }
  return 0;
}

你可能感兴趣的:(BFS,DFS,并查集)