hdu1198 不一样的搜索水题
2016-07-27 20:25
267 查看
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.
题意 A--K对应不同水管 要事整个田地都能灌溉到水
问 需要到少水源 一开始想的就是深搜 只是方向都不同罢了 和求连通区域个数没什么区别 属于最水的搜索题
ac code
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.
题意 A--K对应不同水管 要事整个田地都能灌溉到水
问 需要到少水源 一开始想的就是深搜 只是方向都不同罢了 和求连通区域个数没什么区别 属于最水的搜索题
ac code
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=60; char mat[maxn][maxn]; bool vis[maxn][maxn]; int row,col; int dir[12][4]= { {1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0}, {1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1}, {0,1,1,1},{1,1,1,0},{1,1,1,1} }; int dx[]= {-1,0,1,0}; int dy[]= {0,1,0,-1}; bool judge(int x,int y) { return x>0&&x<=row&&y>0&&y<=col&&!vis[x][y]; } void dfs(int from,int x,int y) { int num=mat[x][y]-'A',xx,yy; if(from<4) if(!dir[num][(from+2)%4]){vis[x][y]=false; return ;} for(int i=0; i<4; i++) { if(dir[num][i]) { xx=x+dx[i]; yy=y+dy[i]; if(judge(xx,yy)) { vis[xx][yy]=true; dfs(i,xx,yy); } } } } int main() { while(scanf("%d%d",&row,&col),row>0&&col>0) { getchar(); for(int i=1; i<=row; i++) scanf("%s",mat[i]+1); memset(vis,false,sizeof vis); int ans=0; for(int i=1; i<=row; i++) for(int j=1; j<=col; j++) if(!vis[i][j]) { ans++; vis[i][j]=true; dfs(maxn,i,j); } printf("%d\n",ans); } return 0; }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- 书评:《算法之美( Algorithms to Live By )》
- IE:使用搜索助手
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解