Farm Irrigation

题目:Farm Irrigation

题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494

题目思路:并查集

  1 #include<stdio.h>
  2 //并查集重点就是实现:查询某点在哪个集合、将两个集合合并
  3 //查找点的祖先
  4 int a[10000];
  5 int fun(int x)
  6 {
  7   int p=x;
  8   // 初始化数组 a[i]=i;
  9   // 也就是说当 a[i]==i 时,这是一个祖先,或者他是别人的子树
 10   while(a[p]!=p)
 11   {
 12     p=a[p];
 13   }
 14   // p就是祖先,下面是路径压缩
 15   int q=x;
 16   while(a[q]!=p)
 17   {
 18     x=a[q];
 19     a[q]=p;
 20     q=x;
 21   }
 22   return p;
 23 }
 24 //合并两个集合
 25 void join(int x,int y)
 26 {
 27   int xt=fun(x);
 28   int yt=fun(y);
 29   if(xt!=yt)
 30   {
 31     a[xt]=yt;
 32   }
 33 }
 34 int n,m;
 35 int bian(int i,int j)
 36 {
 37   return i*m+j;
 38 }
 39 int islian_heng(char c1,char c2)
 40 {
 41   if(c1==‘B‘)
 42   {
 43     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 44     else return 0;
 45   }
 46   else if(c1==‘D‘)
 47   {
 48     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 49     else return 0;
 50   }
 51   else if(c1==‘F‘)
 52   {
 53     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 54     else return 0;
 55   }
 56   else if(c1==‘G‘)
 57   {
 58     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 59     else return 0;
 60   }
 61   else if(c1==‘I‘)
 62   {
 63     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 64     else return 0;
 65   }
 66   else if(c1==‘J‘)
 67   {
 68     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 69     else return 0;
 70   }
 71   else if(c1==‘K‘)
 72   {
 73     if(c2==‘A‘||c2==‘C‘||c2==‘F‘||c2==‘G‘||c2==‘H‘||c2==‘I‘||c2==‘K‘) return 1;
 74     else return 0;
 75   }
 76   else return 0;
 77 }
 78 int islian_shu(char c1,char c2)
 79 {
 80   if(c1==‘C‘)
 81   {
 82     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
 83     else return 0;
 84   }
 85   else if(c1==‘D‘)
 86   {
 87     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
 88     else return 0;
 89   }
 90   else if(c1==‘E‘)
 91   {
 92     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
 93     else return 0;
 94   }
 95   else if(c1==‘H‘)
 96   {
 97     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
 98     else return 0;
 99   }
100   else if(c1==‘I‘)
101   {
102     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
103     else return 0;
104   }
105   else if(c1==‘J‘)
106   {
107     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
108     else return 0;
109   }
110   else if(c1==‘K‘)
111   {
112     if(c2==‘A‘||c2==‘B‘||c2==‘E‘||c2==‘G‘||c2==‘H‘||c2==‘J‘||c2==‘K‘) return 1;
113     else return 0;
114   }
115   else return 0;
116 }
117 int main()
118 {
119   char s[100][100];
120   while(scanf("%d%d",&n,&m)!=EOF)
121   {
122     if(n==-1&&m==-1) break;
123     for(int i=0;i<n;i++)
124       scanf("%s",s[i]);
125     for(int i=0;i<n*m;i++)
126       a[i]=i;
127     for(int i=0;i<n;i++)
128     {
129       for(int j=0;j<m-1;j++)
130       {
131         if(islian_heng(s[i][j],s[i][j+1])==1)
132         {
133           join(bian(i,j),bian(i,j+1));
134         }
135       }
136     }
137     for(int i=0;i<m;i++)
138       for(int j=0;j<n-1;j++)
139         if(islian_shu(s[j][i],s[j+1][i])==1)
140         {
141           join(bian(j,i),bian(j+1,i));
142         }
143     int co=0;
144     for(int i=0;i<n*m;i++)
145     {
146       if(a[i]==i) co++;
147     }
148     printf("%d\n",co);
149   }
150   return 0;
151 }

AC代码

时间: 2024-10-07 02:39:06

Farm Irrigation的相关文章

HDU 1198 Farm Irrigation

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7897    Accepted Submission(s): 3418 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

ZOJ 2412 Farm Irrigation

Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 di

【简单并查集】Farm Irrigation

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious fa

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

Farm Irrigation HDU - 1198 (并查集)

Farm Irrigation HDU - 1198 题意:给11种管道,问草地最少需要打多少个井才可以全部灌溉. 把每种管道的状态用二进制表示一下,然后对每一块草地,判断能否和上面或者左面的草地的管道连接. 然后并查集搞一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=55; 4 5 int g[12]={10,9,6,5,12,3,11,14,7,13,15}; 6 int f[maxn*maxn]

HDU1198水管并查集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

(hdu step 5.1.4)Farm Irrigation(在两个节点合并有限制条件的情况下,求集合的个数)

题目: Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 202 Accepted Submission(s): 104   Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle,

【HDOJ】1198 Farm Irrigation

其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 55 5 #define UP 0 6 #define RIGHT 1 7 #define DOWN 2 8 #define LEFT 3 9 10 char buf[MAXNUM][MAXNUM]; 11 int bin[MAXNUM*MAXNUM]; 12 char visit[MAXN

HDOJ1198 Farm Irrigation 【并查集】

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5051    Accepted Submission(s): 2167 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle