您的位置:首页 > 其它

【搜索入门专题1】E - Farm Irrigation 【BFS】ZOJ 2412

2017-07-30 17:43 621 查看




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


题意:输入N,M,N<0或者m<0为结束条件,再输入N行字符串,每行M个字符。

每个字符代表题目所展示

的图片,判断有多少根相连的管道,独立的一根管道同样算入总数。

思路:用三维数组存储'A'~'Z'图,用1模拟管道,0模拟草坪,每个图为3*3的小矩阵

读入字符数组之后,将二维字符数组用0,1,模拟为一个N*3行,M*3列的二维数组

将样例转换后在二维数组中的存储方式如图,如果有写过杭电1241,就会发现其实

到这一步,两道题已经没什么区别,找到1总数加1并就往下搜索且将连续的1全部置为0,再找

下一个连续的1,直到将全部1标记完。



#include<stdio.h>
#include<string.h>
#define N 55
int num[N*3][N*3];
int n,m;
int k,l,X,Y;
int d;
int count;
char str

;

int f[11][3][3] = {
{{0,1,0},
{1,1,0},
{0,0,0}},//A图
{{0,1,0},
{0,1,1},
{0,0,0}},//B图
{{0,0,0},
{1,1,0},
{0,1,0}},//C图
{{0,0,0},
{0,1,1},
{0,1,0}},//D图
{{0,1,0},
{0,1,0},
{0,1,0}},//E图
{{0,0,0},
{1,1,1},
{0,0,0}},//F图
{{0,1,0},
{1,1,1},
{0,0,0}},//G图
{{0,1,0},
{1,1,0},
{0,1,0}},//H图
{{0,0,0},
{1,1,1},
{0,1,0}},//I图
{{0,1,0},
{0,1,1},
{0,1,0}},//J图
{{0,1,0},
{1,1,1},
{0,1,0}},//K图
};

void dfs(int x,int y)//找到满足条件的点一直往下搜索,直到搜索完
{
int tx,ty;
int next[4][2] = {0,1,0,-1,1,0,-1,0};
int i,j;
for(i = 0; i < 4; i ++)
{
tx = x + next[i][0];
ty = y + next[i][1];
if(tx < 0||ty < 0||tx> X-1||ty > Y-1)
continue;
if(num[tx][ty])
{
num[tx][ty] = 0;//只要走过就标记为1,由于不是找最优路径,故不用回溯。
dfs(tx,ty);
}
}
return;
}

int main()
{
int i,j;
while(scanf("%d%d",&n,&m),n>0&&m>0)
{
count = 0;//记录相连的1的个数
memset(num,0,sizeof(num));
for(i = 0; i < n; i ++)
scanf("%s",str[i]);

for(i = 0; i < n; i ++)
{
for(j = 0; j < m; j ++)
{
d = str[i][j] - 'A';//将字符分离出来
X = i*3;//横坐标的起始点
for(k = 0; k < 3; k ++)
{
Y = j*3;//纵坐标的起始点
for(l = 0; l < 3; l ++)
{
num[X][Y++] = f[d][k][l];
}
X++;
}
}
}
for(i = 0; i < X; i ++)
{
for(j = 0; j < Y; j ++)
{
if(num[i][j] == 1)//找到满足条件的位置
{
count++;//总数加1
num[i][j] = 0;//起始位置已经走过所以置为0
dfs(i,j);
}
}
}
printf("%d\n",count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: