ACM1198Farm Irrigation

这个题目好吓人呀!嘿嘿---

不过仔细分析下就可以啦!

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=550;
 5 int Gra[11][4]={
 6 {1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},
 7 {1,0,1,0},{0,1,0,1},{1,1,0,1},
 8 {1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
 9 int direction[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
10 //11种铸造方式,1为开,0为关
11  //用输入的大写字母减去‘A‘ 即可
12   //2和下面的0可相加
13   //3->1;0->2;1->3;
14   int m,n;//m lines and n characters
15   int map[N][N];
16 int vis[N][N];
17 void dfs(int x,int y)
18 {
19     vis[x][y]=1;
20     for(int i=0;i<4;i++)
21     {
22         int X=x+direction[i][0];
23         int Y=y+direction[i][1];
24         int j=(i>=2)?(i-2):(i+2);//顺序绝对不可以改,或者要改一起改,有点变动就是wrong
25         if(Gra[map[x][y]][i]&&0<X&&X<=m&&0<Y&&Y<=n)
26         {
27             if(!vis[X][Y]&&Gra[map[X][Y]][j])
28             dfs(X,Y);
29         }
30     }
31 }
32 int main()
33 {
34     char ch[N];
35     while(cin>>m>>n)
36     {
37         if(m<0||n<0)break;//exit;
38         memset(vis,0,sizeof(vis));
39         for(int i=1;i<=m;i++)
40         {
41             cin>>ch;
42             for(int j=0;j<n;j++)
43                 map[i][j+1]=ch[j]-‘A‘;
44         }
45         int count=0;
46         for(int i=1;i<=m;i++)
47         {
48             for(int j=1;j<=n;j++)
49             if(!vis[i][j])
50             dfs(i,j),count++;
51         }
52         cout<<count<<endl;
53     }
54     return 0;
55 } 

ACM1198Farm Irrigation

时间: 2024-10-11 14:54:57

ACM1198Farm Irrigation的相关文章

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 时,这是一个祖先,或者他是别人的子

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

hdu1198Farm Irrigation(dfs找联通)

题目链接: 啊哈哈,选我选我 思路是:首先根据图像抽象出联通关系..首先确定每一种图形的联通关系,用01值表示不连通与不连通...然后从第1个图形进行dfs搜索.如果碰到两快田地可以联通的话那么标记..注意处理的过程中你的 搜索顺序要和你的每个图形的连通性的顺序相同..然后就是最后看上下.左右能否匹配...看最后有几个不同的快,这就是答案,感觉跟并查集一样..并查集应该也可以做.. 题目: Farm Irrigation Time Limit: 2000/1000 MS (Java/Others

【简单并查集】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

hdoj-1198-Farm Irrigation【并查集】

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