ZOJ-Farm Irrigation(dfs)

85 阅读2分钟

Farm Irrigation

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

题目链接:

acm.zju.edu.cn/onlinejudge…

题意描述:

整体的意思就是让你判断这有几块连通的图,这个图是由一块一块的正方形组成的,正方形四个方向分别有是通或者不通

解题思路:

把每个输入的字母转化为放个图存到一个结构体数组中,然后把四个方向分别用1和0标记,1表示通0表示不通,然后深搜

程序代码:

#include<stdio.h>
#include<string.h>

struct data{
	int left;
	int up;
	int right;
	int down;
}a[60][60];
int book[60][60];
int m,n;


void dfs(int x,int y);

int main() 
{ 
	char ch;
	int i,j,count;
	while(scanf("%d%d",&m,&n),m!=-1||n!=-1)
	{
		memset(book,0,sizeof(book));
		for(i=0;i<m;i++)
		{
			getchar();
			for(j=0;j<n;j++)
			{
				scanf("%c",&ch);
				if(ch=='A')
				{
					a[i][j].left=1;
					a[i][j].up=1;
					a[i][j].right=0;
					a[i][j].down=0;
				}
				else if(ch=='B')
				{
					a[i][j].left=0;
					a[i][j].up=1;
					a[i][j].right=1;
					a[i][j].down=0;
				}
				else if(ch=='C')
				{
					a[i][j].left=1;
					a[i][j].up=0;
					a[i][j].right=0;
					a[i][j].down=1;
				}
				else if(ch=='D')
				{
					a[i][j].left=0;
					a[i][j].up=0;
					a[i][j].right=1;
					a[i][j].down=1;
				}
				else if(ch=='E')
				{
					a[i][j].left=0;
					a[i][j].up=1;
					a[i][j].right=0;
					a[i][j].down=1;
				}
				else if(ch=='F')
				{
					a[i][j].left=1;
					a[i][j].up=0;
					a[i][j].right=1;
					a[i][j].down=0;
				}
				else if(ch=='G')
				{
					a[i][j].left=1;
					a[i][j].up=1;
					a[i][j].right=1;
					a[i][j].down=0;
				}
				else if(ch=='H')
				{
					a[i][j].left=1;
					a[i][j].up=1;
					a[i][j].right=0;
					a[i][j].down=1;
				}
				else if(ch=='I')
				{
					a[i][j].left=1;
					a[i][j].up=0;
					a[i][j].right=1;
					a[i][j].down=1;
				}
				else if(ch=='J')
				{
					a[i][j].left=0;
					a[i][j].up=1;
					a[i][j].right=1;
					a[i][j].down=1;
				}
				else if(ch=='K')
				{
					a[i][j].left=1;
					a[i][j].up=1;
					a[i][j].right=1;
					a[i][j].down=1;
				}	
			}
		}
		count=0;
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				if(book[i][j]==0)
				{
					count++;
					dfs(i,j);
				}
		printf("%d\n",count);					
				
	}
	return 0;
}

void dfs(int x,int y)
{
	book[x][y]=1;
	int tx,ty;
	tx=x+1;
	ty=y;
	if(tx<m&&book[tx][ty]==0&&a[x][y].down==1&&a[tx][ty].up==1)
		dfs(tx,ty);
	tx=x-1;
	ty=y;
	if(tx>=0&&book[tx][ty]==0&&a[x][y].up==1&&a[tx][ty].down==1)
		dfs(tx,ty);	
		
	tx=x;
	ty=y+1;
	if(ty<n&&book[tx][ty]==0&&a[x][y].right==1&&a[tx][ty].left==1)
		dfs(tx,ty);	
	tx=x;
	ty=y-1;
	if(ty>=0&&book[tx][ty]==0&&a[x][y].left==1&&a[tx][ty].right==1)
		dfs(tx,ty);
}