【HDOJ】1198 Farm Irrigation
其实就是并查集,写麻烦了,同样的代码第一次提交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的更多相关文章
- 【HDOJ】3696 Farm Game
SPFA求最短路径.见图的时候注意逆向建图. /* 3696 */ #include <iostream> #include <queue> #include <vect ...
- 【BZOJ4061】[Cerc2012]Farm and factory(最短路,构造)
[BZOJ4061][Cerc2012]Farm and factory(最短路,构造) 题面 BZOJ 然而权限题QwQ. 题解 先求出所有点到达\(1,2\)的最短路,不妨记为\(d_{u,1}, ...
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
随机推荐
- angularjs ios title不能修改的bug的解决方法
在app.js加入下面这句代码 就可以解决. function ($rootScope, $state, $stateParams, $log, httpService, apiUrl, cookie ...
- @property在内存管理中的参数问题
// // Created by wanghy on 15/8/14. // // /* retain : release旧值,retain新值(用于OC对象),要配合nonatomic使用. ass ...
- jjQuery 源码分析1: 整体结构
目前阅读的是jQuery 1.11.3的源码,有参考nuysoft的资料. 原来比较喜欢在自己的Evernote上做学习基类,并没有在网上写技术博客的习惯,现在开始学习JS的开源代码,想跟大家多交流, ...
- 一个开源的可视化的jQuery工作流插件
特点 1.跨浏览器,可兼容IE7--IE11, FireFox, Chrome, Opera等几大内核的浏览器,且不需要浏览器再加装任何控件. (IE7-IE8时,使用VML:IE9以上,FF,OPE ...
- 更新ACCESS数据库出现“字段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据。”的解决方法
今天进行数据调试时出现“字段太小而不能接受所要添加的数据的数量.试着插入或粘贴较少的数据.”,跟踪发现是在更新数据库的数据时出现的. 打开数据库表格发现出错的数据字段类型被定义为“文本”,也就是数据最 ...
- mongodb数据库操作--备份 还原 导出 导入
首先数据库备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 mongodump -h 127.0.0.1 -u admin -p ...
- git push用法和常见问题分析
在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push origin test:master // 提交本地test分支作为远程的m ...
- C++中弱符号(弱引用)的意义及实例
今天读别人代码时看到一个“#pragma weak”,一时没明白,上网研究了一个下午终于稍微了解了一点C.C++中的“弱符号”,下面是我的理解,不正确的地方望大家指正. 本文主要从下面三个方面讲“弱符 ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- wysiwyg editor
http://www.bootcss.com/p/bootstrap-wysiwyg/