杭电1198--Farm Irrigation(BFS外…

来源:互联网 发布:数据采集卡原理设计 编辑:程序博客网 时间:2024/05/18 14:26
Problem Description
Benny has a spacious farm land to irrigate. The farm land is arectangle, and is divided into a lot of samll squares. Water pipesare placed in these squares. Different square has a different typeof pipe. There are 11 types of pipes, which is marked from A to K,as Figure 1 shows.

杭电1198--Farm <wbr>Irrigation(BFS外加点技巧)

Figure 1


Benny has a map of his farm, which is an array of marks denotingthe 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

杭电1198--Farm <wbr>Irrigation(BFS外加点技巧)

Figure 2


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

Now Benny wants to know at least how many wellsprings should befound to have the whole farm land irrigated. Can you helphim?

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

Input
There are several test cases! In each test case, the firstline contains 2 integers M and N, then M lines follow. In each ofthese lines, there are N characters, in the range of 'A' to 'K',denoting the type of water pipe over the corresponding square. Anegative 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 ofwellsprings needed.
 

Sample Input
2 2 DK HF 33 ADC FJK IHE -1 -1
 

Sample Output
2 3

# include<stdio.h>

# include<string.h>

int mark[55][55],m,n;//mark[][]做标记用

int dir[4][2]={-1,0,1,0,0,-1,0,1};//某点的四个方向(分别为上,下,左,右);

intflag[][4]={{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}};//分别标记A~F田的管道开口情况,有则为1,无则为0;注意每块田的管道标记方向也依次为上,下,左,右,与dir[4][2]的方向保持一致,这是本题的关键之处,对于后面的搜索,可简化很多!!

char map[55][55];

struct node {

      int x,y;

};

void bfs(int a,int b)

{

      struct node cur,next;

      int i;

      cur.x=a;

      cur.y=b;

      for(i=0;i<4;i++)

      {

             next.x=cur.x+dir[i][0];

             next.y=cur.y+dir[i][1];

             if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&mark[next.x][next.y]==0)

             {

                    if(i==0)//i==0说明next位于cur的上方,故只考虑next的下方管道是否与cur的上方管道相通即可;

                    {

                           if(flag[map[cur.x][cur.y]-'A'][0]==1&&flag[map[next.x][next.y]-'A'][1]==1) //如果相通,递归搜索next

                           {

                                  mark[next.x][next.y]=1;

                                  bfs(next.x,next.y);

                           }

                    }

                    else if(i==1)//原理同上

                    {

                           if(flag[map[cur.x][cur.y]-'A'][1]==1&&flag[map[next.x][next.y]-'A'][0]==1)

                           {

                                  mark[next.x][next.y]=1;

                                  bfs(next.x,next.y);

                           }

                    }

                    else if(i==2)//原理同上

                    {

                           if(flag[map[cur.x][cur.y]-'A'][2]==1&&flag[map[next.x][next.y]-'A'][3]==1)

                           {

                                  mark[next.x][next.y]=1;

                                  bfs(next.x,next.y);

                           }

                    }

                    else//原理同上

                    {

                           if(flag[map[cur.x][cur.y]-'A'][3]==1&&flag[map[next.x][next.y]-'A'][2]==1)

                           {

                                  mark[next.x][next.y]=1;

                                  bfs(next.x,next.y);

                           }

                    }

             }

      }

}

int main()

{

      int i,j,num;

      while(scanf("%d%d",&m,&n))

      {

             if(m==-1&&n==-1)

                    break;

             for(i=0;i<m;i++)

                    scanf("%s",map[i]);

             num=0;

             memset(mark,0,sizeof(mark));

             for(i=0;i<m;i++)

                    for(j=0;j<n;j++)

                    {

                           if(mark[i][j]==0)

                           {

                                  num++;

                                  mark[i][j]=1;

                                  bfs(i,j);

                           }

                    }

             printf("%d\n",num);

      }

      return 0;

}

                    

0 0
原创粉丝点击