其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了。

 #include <stdio.h>
#include <string.h> #define MAXNUM 55
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3 char buf[MAXNUM][MAXNUM];
int bin[MAXNUM*MAXNUM];
char visit[MAXNUM*MAXNUM][MAXNUM*MAXNUM]; int direct[][] = {{-,},{,},{,},{,-}};
// A B C D E
int pipes[][] = {{,,,},{,,,},{,,,},{,,,}, {,,,},
// F G H I J
{,,,},{,,,},{,,,},{,,,}, {,,,}, {,,,}}; int find(int x) {
int r = x; while (r != bin[r])
r = bin[r]; return r;
} void merge(int x, int y) {
int fx, fy; fx = find(x);
fy = find(y); if (fx != fy)
bin[fx] = fy;
} int main() {
int n, m;
int i, j, k, x, y, p, q, is, id, ns, nd; while (scanf("%d %d%*c", &n, &m) != EOF) {
if (m< || n<)
break;
for (i=; i<n; ++i)
scanf("%s", buf[i]);
memset(visit, , sizeof(visit));
for (i=; i<n*m; ++i)
bin[i] = i;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
is = buf[i][j] - 'A';
ns = i*m+j;
for (k=; k<; ++k) {
x = i + direct[k][];
y = j + direct[k][];
if (x< || x>=n || y< || y>=m)
continue;
nd = x*m+y;
if (visit[ns][nd] || visit[nd][ns])
continue;
id = buf[x][y] - 'A';
switch (k) {
case UP:
p = pipes[is][UP];
q = pipes[id][DOWN];
break;
case RIGHT:
p = pipes[is][RIGHT];
q = pipes[id][LEFT];
break;
case DOWN:
p = pipes[is][DOWN];
q = pipes[id][UP];
break;
case LEFT:
p = pipes[is][LEFT];
q = pipes[id][RIGHT];
break;
default: ;
}
if (p && q)
merge(ns, nd);
visit[ns][nd] = ;
visit[nd][ns] = ;
}
}
}
k = ; for (i=; i<n*m; ++i) {
if (i == bin[i])
++k;
}
printf("%d\n", k);
} return ;
}

【HDOJ】1198 Farm Irrigation的更多相关文章

  1. 【HDOJ】3696 Farm Game

    SPFA求最短路径.见图的时候注意逆向建图. /* 3696 */ #include <iostream> #include <queue> #include <vect ...

  2. 【BZOJ4061】[Cerc2012]Farm and factory(最短路,构造)

    [BZOJ4061][Cerc2012]Farm and factory(最短路,构造) 题面 BZOJ 然而权限题QwQ. 题解 先求出所有点到达\(1,2\)的最短路,不妨记为\(d_{u,1}, ...

  3. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  4. hdu.1198.Farm Irrigation(dfs +放大建图)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  8. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  9. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

随机推荐

  1. ACM——3n+1

    3n+1 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:937            测试通过:386 描述 对于任意大于1的自然 ...

  2. c常用字符串函数

    获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...

  3. 怎么安装MySQL,安装MySQL遇到的一些问题!!!!!!

    简介: 对于初学,我们在安装MySQL时,会出现各种各样的报错,这让我们非常的头痛.那么我来分享一下我在安装的过程中遇到的一些问题吧! 我们在安装MySQL之前,先安装好dotNetFx40_Clie ...

  4. mysql主从之主键冲突

    收到短信报警,两台数据库都报slave同步失败了,先说明一下环境,架构:lvs+keepalived+amoeba+mysql,主主复制,单台写入, 主1:192.168.0.223(写) 主2:19 ...

  5. mkpasswd

    -l #      (length of password, default = 7)                   指定密码的长度,默认是7位数 -d #      (min # of dig ...

  6. openwrt opkg update wget returned 4 wget returned 1

    最近在正捣鼓mt7620芯片的路由器,刷入openwrt Pandora系统以后想装wifidog实现web认证. 我用我自己的一个水星的路由器PPPOE拨号,通过水星的lan口连接网线到我openw ...

  7. c#面向对象小结

    特点: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在人们思考习惯的一种思想. 过程和对象在我们的程序中是如何体现的呢?过程其实就是函数: ...

  8. CSS 常用命名

    在前端开发中,规范使用 DIV+CSS 命名,可以增强团队合作提高开发效率,有利于页面后期的维护和优化. 1.页面结构 wrap:外套.包裹,用于最外层. wrapper:外套,用于页面外围控制整体布 ...

  9. css3怎么隐藏dom:4种方法

    1.display:none; 2.position:absolute; left:-99999px; 3.visibility:hidden; 4.opacity:0;

  10. php表单发送到邮箱V1.0

    html表单代码: <form action="index.php" name="form" method="POST"> &l ...