[简单图论]「微软面试」找联通子图
来源:互联网 发布:知乎 父母 编辑:程序博客网 时间:2024/06/07 14:43
从一个无向图里找出各个联通子图,每个子图作为一组。
如何测试。
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4498 Accepted Submission(s): 1951
Problem Description
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.
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
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
Author
ZHENG, Lu
Source
Zhejiang University Local Contest 2005
Recommend
Ignatius.L.
就如标题说的,这题就是找出图中所有联通子图,做法和那个找油田的相似。对每个顶点做次dfs就行了,做过后在标记上。开始没有想到用ac数组标记当前不能访问位置,只标记*,结果错了。还有就是那个字符的处理,和方向有关的,这个细心点,我没什么好办法,就是手打……
#include<stdio.h>#include<string.h>#define MAX 55char map[MAX][MAX];int ac[MAX][MAX];int dr[4]={-1,1,0,0};int dc[4]={0,0,-1,1};int N,M;int ans;int inmap(int x,int y){ if(x<0||y<0||x>=N||y>=M) return 0; return 1;}int cango(int now_x,int now_y,int dirct){ int x,y; char ch=map[now_x][now_y]; char next; x=now_x+dr[dirct]; y=now_y+dc[dirct]; next=map[x][y]; if(dirct==0) { if(ch=='C'||ch=='D'||ch=='F'||ch=='I') return 0; if(next=='A'||next=='B'||next=='F'||next=='G') return 0; else return 1; } else if(dirct==1) { if(ch=='A'||ch=='B'||ch=='F'||ch=='G') return 0; if(next=='C'||next=='D'||next=='F'||next=='I') return 0; else return 1; } else if(dirct==2) { if(ch=='B'||ch=='D'||ch=='E'||ch=='J') return 0; if(next=='A'||next=='C'||next=='E'||next=='H') return 0; else return 1; } else if(dirct==3) { if(ch=='A'||ch=='C'||ch=='E'||ch=='H') return 0; if(next=='B'||next=='D'||next=='E'||next=='J') return 0; else return 1; }}int dfs(int now_x,int now_y){ int i; if(map[now_x][now_y]=='*') return 0; for(i=0;i<4;i++) { int x=now_x+dr[i]; int y=now_y+dc[i]; if(!inmap(x,y)||map[x][y]=='*'||ac[x][y]==1) continue; if(!cango(now_x,now_y,i)) continue; ac[now_x][now_y]=1; dfs(x,y); ac[now_x][now_y]=0; } map[now_x][now_y]='*'; return 1;}int main(){ scanf("%d%d",&N,&M); while(scanf("%d%d",&N,&M),N>=0&&M>=0) { int i,j; ans=0; memset(ac,0,sizeof(ac)); for(i=0;i<N;i++) scanf("%s",map[i]); for(i=0;i<N;i++) for(j=0;j<M;j++) ans+=dfs(i,j); printf("%d\n",ans); } return 0;}
分类: ACM, 图论
0 0
- [简单图论]「微软面试」找联通子图
- bzoj 1093 最大半联通子图
- Networkx_找出最大联通子图及联通子图规模排序
- [BZOJ1093]ZJOI2007最大半联通子图|强联通分量|DP
- [BZOJ1093][ZJOI2007][Tarjan][DP]最大半联通子图
- twitter电面题目 - 同色最大联通子图
- 【bzoj1093】【zjoi2007】【最大半联通子图】【缩点+dp】
- 【bzoj1093】【tarjan+dp】最大半联通子图
- [题解]bzoj1093(ZJOI2007)最大半联通子图
- 2017.9.2 最大半联通子图 思考记录
- 微软面试简单算法题目
- 微软面试3 最大子数组问题
- HDU 4738 Caocao's Bridges(找无向图的桥 双联通)
- 微软面试百题011——找数
- 微软与联通不合?
- 微软面试中简单的算法题目
- 【图论算法及模版】割点,割边,强联通分量,点双联通分量,边双联通分量
- 【图论算法及模版】割点,割边,强联通分量,点双联通分量,边双联通分量
- git 笔记
- Vs2012 编译配置 Lua5.2.3
- linux 设置网关,IP,子网掩码
- Shell遍历文件夹删除指定后缀名的文件
- 【FreeMarker】【模板文件FTL】模板指令/标签 <#> <@>
- [简单图论]「微软面试」找联通子图
- 树的三种遍历方式 白话解读 + 波兰表达式为例
- 【mysql】linux下mysql的root密码忘记解决方
- HDU 1164 Eddy's research I【素数筛选法】
- NFS初探
- 用编程解二元一次方程
- 数据结构排序之桶排序
- Servlet中的小问题
- 【FreeMarker】【模板文件FTL】模板表达式