FarmIrrigationProblemDescriptionBennyhasaspaciousfarmlandtoirrigate.Thefarmlandisarec Farm IrrigationProblem DescriptionBenny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Wat
Farm IrrigationProblem DescriptionBenny 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 mapADCFJKIHEthen 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.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.OutputFor each test case, output in one line the least number of wellsprings needed.Sample Input2 2DKHF3 3ADCFJKIHE-1 -1Sample Output23AuthorZHENG, LuSourceZhejiang University Local Contest 2005RecommendIgnatius.L 这道题目有够坑的!做了我足足有三个小时!不过说实话,收获挺大的!说实话,这道题目并不难,一道典型的并查集题目,当然我也只会这一种方法,我还看见有的大神居然用搜索做了出来,够牛的! 题目咋一看,貌似摸不着头脑,不过你要想办法把问题转化到并查集上面来,我开始尝试着做的时候也是一头雾水,不过做着做着头脑就清晰了!稍微参考了一下大神的代码,发现有几点可以借鉴,在这里总结一下! 1.set数组最好还是一维数组,如果用二维的话不好控制! 2.不要怕所谓的难题,这道题目在今天以前我是想也不敢想的,但是到今天,只用了三个小时就敲出来了,因此没有所谓的难题,之所以难,是因为你水平还没有达到那一个层次,努力学习吧!多学了一点之后,你会发现,真没有那么难! 代码不做过多分析,不懂的同学可以仔细学一下并查集,很简单!可以查看这里!http://blog.csdn.net/lishuhuakai/article/details/8524906 代码如下:#includeusing namespace std;const int MAX=60;int set[MAX*MAX];struct Irr{int l,r,u,d;}irr[MAX][MAX];int findx(int x) { int r=x; while(set[r]!=r) r=set[r]; return r; } void merge(int x,int y) { int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy) { set[fx]=fy; } } int main(){int line,row,cnt,a,b;int i,j;char ch;while(cin>>line>>row i>ch;switch(ch){case 'A':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=0;break; case 'B':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=0;break; case 'C':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=0;irr[i][j].d=1;break;case 'D':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=1;break;case 'E':irr[i][j].l=0;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=1;break;case 'F':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=0;break;case 'G':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=0;break; case 'H':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=1;break; case 'I':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=1;break;case 'J':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=1;break;case 'K':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=1;break;}} for(i=0;i=0 ) if(irr[a][b].d==1 a=i+1;b=j; if(a=0) if(irr[a][b].r==1 a=i;b=j+1; if(b>n>>m){ if(n==-1 init(); for (int i=0; i>map[i][j]; } } for (int i=0; i=0 ){ if (farms[map[i-1][j]-'A'].down int y=(i-1)*m+j; Union(x,y); } } if ((i + 1)= 0) { if (farms[map[i][j-1]-'A'].right int y = i * m + (j - 1); Union(x, y); } } if ((j + 1)
上一篇:Smack4.3.2发布,XMPP(jabber)的Java客户端类库
下一篇:没有了
上一篇:Smack4.3.2发布,XMPP(jabber)的Java客户端类库
下一篇:没有了