您的位置:首页 > 其它

杭电1198--Farm Irrigation(BFS外…

2014-06-07 17:43 323 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case, output in one line the least number of
wellsprings needed.

 

[align=left]Sample Input[/align]

2 2 DK HF 3
3 ADC FJK IHE -1 -1

 

[align=left]Sample Output[/align]

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};//某点的四个方向(分别为上,下,左,右);

int
flag[][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;

}

                    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: