H - Farm Irrigation寒假练习1-H

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

扫描二维码关注公众号,回复: 2159137 查看本文章

Sample Output
2
3
来源:zoj2412
解题思路:
查找图中有几个联通块儿·,有几个联通块儿就建几座水井,我是用广搜写的,如果板块能被联通就扫描所有能联通的块,并标记,遍历所有板块,遇到没联通的,就标记其所有能联通的,并多建一座井,输出水井数。
代码:

#include<stdio.h>
#include<string.h>
int m,n;
int bk[550][550],map[550][550];
int next[11][8]={{-1,0,0,-1,0,0,0,0},{0,-1,1,0,0,0,0,0},{-1,0,0,1,0,0,0,0},{1,0,0,1,0,0,0,0},{0,1,0,-1,0,0,0,0},{1,0,-1,0,0,0,0,0},{-1,0,1,0,0,-1,0,0},{0,1,0,-1,-1,0,0,0},{-1,0,0,1,1,0,0,0},{0,-1,0,1,1,0,0,0},{1,0,-1,0,0,1,0,-1}};
typedef struct{
    int x,y;
}node;
node q[300000];
int fs(int x,int y,int ex,int ey)
{
    int s=0;
    for(int k=0;k<8;k+=2)
    {
        int tx=x+next[map[x][y]][1+k];
        int ty=y+next[map[x][y]][k];
        if(tx==ex&&ty==ey)
        {
            s=1;
            break;
        }
    }
    return s;
}
void pt(){
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",bk[i][j]);
        }
        printf("\n");
    }
}
void bfs(int x,int y)
{
    bk[x][y]=1;
    int h,t;
    h=t=1;
    q[h].x=x;
    q[h].y=y;
    t++;
    while(h<t)
    {
        for(int k=0;k<8;k+=2)
        {
            int tx=q[h].x+next[map[q[h].x][q[h].y]][k+1];
            int ty=q[h].y+next[map[q[h].x][q[h].y]][k];
            if(bk[tx][ty]==0)
            {
                int r=fs(tx,ty,q[h].x,q[h].y);
                if(r)
                {
                    if(tx<0||ty<0||ty>=n||tx>=m)
                    continue;
//                  printf("%d %d %d %d\n",tx,ty,x,y);

                    bk[tx][ty]=1;
//                  bfs(tx,ty);
                    q[t].x=tx;
                    q[t].y=ty;
                    t++;
                }
            }
        }

//      printf("\n||\n");
//      pt();
        h++;
    }

}
int main()
{
    char s[1010]; 
    while(scanf("%d%d",&m,&n),m>0&&n>0)
    {
        memset(bk,0,sizeof(bk));
        memset(map,0,sizeof(map));
        char c;
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            for(int j=0;j<n;j++)
            {

                map[i][j]=s[j]-'A';

            }
        }
//      for(int i=0;i<m;i++)
//      {
//          
//          for(int j=0;j<n;j++)
//          {
//              printf("%d ",map[i][j]);
//              
//          }
//          printf("\n");
//      }
        int z=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(bk[i][j]==0)
                {
                    z++;
//
//                  pt();
//                  printf("\n");
                    bfs(i,j);                   
                }

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

错误解析:
每次的搜的方向一定要注意!!!我一直坑在这里了,另外见别的大佬有用并查集写的,可以借鉴借鉴。

猜你喜欢

转载自blog.csdn.net/watestill/article/details/79182412
h'h