【搜索入门专题1】E
来源:互联网 发布:冰岛足球 知乎 编辑:程序博客网 时间:2024/06/06 11:39
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
ADCFJKIHE
then the water pipes are distributed likeFigure 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.
InputThere 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 2DKHF3 3ADCFJKIHE-1 -1
Sample Output23
题意:输入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 55int num[N*3][N*3];int n,m;int k,l,X,Y;int d;int count;char str[N][N];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;}
- 【搜索入门专题1】E
- 【字符串入门专题1】 E
- 【搜索入门专题1】I
- 【最短路入门专题1】E
- 【二分图匹配入门专题1】E
- 【并查集入门专题1】E
- 【搜索入门专题1】hdu1548 G
- 【搜索入门专题1】hdu2717 H
- 【搜索入门专题1】hdu1253 【BFS】 F
- 【搜索入门专题1】 hdu1242 J
- [kuangbin带你飞]专题1 简单搜索 E
- [最小生成树入门专题1] zoj1203 E
- 搜索专题:问题 E: 挑战ACM迷宫
- Search Algorithm-搜索入门专题
- 【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】
- 搜索专题训练【CSUST_Newer_12级入门】
- [kuangbin带你飞]专题一 简单搜索 E POJ1426
- [kuangbin带你飞]专题一 简单搜索E
- C++中,自定义string类,实现字符串的赋值“=”和连接函数“+”
- 数据类型初始化后的默认值
- 推荐一款makdown剪辑器-mditor
- STL游标—迭代器(itertor)的使用
- 【CSS 基础】06 盒子模型
- 【搜索入门专题1】E
- 深入理解Java虚拟机 第七章 虚拟机类加载时机与过程
- redis持久化选项
- 205. Isomorphic Strings
- API学习Stack
- 模板--Floyd Dijkstra Bellman-Ford spfa 四种最短路经典算法
- Hdu1329Hanoi Tower Troubles Again!
- Hibernate中many-to-one元素配置中的属性简介
- DOCKER安装两种方法