hdu Farm Irrigation
这题第一感觉是用搜索做,暴力就可以解决,这里将水管转换成一个个3*3的矩阵,然后搜素就可以了。写完之后确实一遍过掉了,31ms。附上代码:
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"cmath"
#include"queue"
#include"stack"
#include"vector"
#include"string"
#include"string.h"
using namespace std;
const int mx=;
int A[][]={{,,},{,,},{,,}};
int B[][]={{,,},{,,},{,,}};
int C[][]={{,,},{,,},{,,}};
int D[][]={{,,},{,,},{,,}};
int E[][]={{,,},{,,},{,,}};
int F[][]={{,,},{,,},{,,}};
int G[][]={{,,},{,,},{,,}};
int H[][]={{,,},{,,},{,,}};
int I[][]={{,,},{,,},{,,}};
int J[][]={{,,},{,,},{,,}};
int K[][]={{,,},{,,},{,,}};
int dir[][]={{,},{,},{,-},{-,}};
int maze[mx][mx];
bool visited[mx][mx];
int m,n,wellspring;
bool judge(int x,int y)
{
if(x>=&&x<*m&&y>=&&y<*n&&maze[x][y]==&&!visited[x][y])
return true;
return false;
}
void dfs(int x,int y)
{
int dx,dy;
for(int i=;i<;i++)
{
dx=x+dir[i][];
dy=y+dir[i][];
if(judge(dx,dy))
{
visited[dx][dy]=true;
dfs(dx,dy);
}
}
}
void creat_maze(int a[][],int i,int j)
{
int p,q;
for(p=i*;p<i*+;p++)
{
for(q=j*;q<j*+;q++)
{
maze[p][q]=a[p-i*][q-j*];
}
}
}
void wellspring_count()
{
int i,j;
for(i=;i<*m;i++)
{
for(j=;j<*n;j++)
{
if(maze[i][j]==&&!visited[i][j])
{
wellspring++;
visited[i][j]=true;
dfs(i,j);
}
}
}
printf("%d\n",wellspring);
}
void input()
{
int i,j;
while(scanf("%d%d",&m,&n),m!=-||n!=-)
{
memset(visited,false,sizeof(visited));
wellspring=;
char pipe;
for(i=;i<m;i++)
{
getchar();
for(j=;j<n;j++)
{
scanf("%c",&pipe);
switch(pipe)
{
case 'A': creat_maze(A,i,j);break;
case 'B': creat_maze(B,i,j);break;
case 'C': creat_maze(C,i,j);break;
case 'D': creat_maze(D,i,j);break;
case 'E': creat_maze(E,i,j);break;
case 'F': creat_maze(F,i,j);break;
case 'G': creat_maze(G,i,j);break;
case 'H': creat_maze(H,i,j);break;
case 'I': creat_maze(I,i,j);break;
case 'J': creat_maze(J,i,j);break;
case 'K': creat_maze(K,i,j);break;
}
}
}
wellspring_count();
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
input();
return ;
}
但是这是一个并查集专题里的题,所以应该还会有更高效的方法,毕竟我的代码里没有用到并查集。
所以又想了一下,瞬变看了一下别人的并查集思路,又写了一个代码。然而,这次用并查集却死活过不了。。。找bug找了接近两个小时了,自己找的案例都过掉了。。。
先附上wa掉的代码:
#include"iostream"
#include"stdio.h"
#include"string"
#include"string.h"
#include"cmath"
#include"algorithm"
#include"ctype.h"
#include"queue"
#include"stack"
#include"vector"
using namespace std;
const int mx=;
int maze[mx][mx];
int fa[mx*mx];
int m,n,wellspring;
int dir[][]={{,-},{-,},{,},{,}};//左上右下
//左上右下有水管接口的用1表示
int farm[][]=
{
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,}
};
void Set()
{
int i;
for(i=;i<m*n;i++)
fa[i]=i;
} int Find(int x)
{
int t1,t2=x;
while(t2!=fa[t2]) t2=fa[t2];
return t2;
} void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
fa[fx]=fy;
wellspring--;
}
} bool judge(int k,int x,int y)
{
switch(k)
{
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
}
return false;
} void Count()
{
int i,j,k,dx,dy;
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
for(k=;k<;k++)
{
dx=i+dir[k][];
dy=j+dir[k][];
if(dx>=&&dx<m&&dy>=&&dy<n)//判定越界条件
if(judge(k,maze[i][j],maze[dx][dy]))
Union(i*m+j,dx*m+dy);
}
}
}
} void Input()
{
int i,j;
while(scanf("%d%d",&m,&n),m>=&&n>=)
{
Set();
wellspring=m*n;
char pipe;
for(i=;i<m;i++)
{
getchar();
for(j=;j<n;j++)
{
scanf("%c",&pipe);
maze[i][j]=pipe-'A';
}
}
Count();
printf("%d\n",wellspring);
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
Input();
return ;
}
hdu Farm Irrigation的更多相关文章
- 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 ...
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
随机推荐
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
- 初探Docker
本文旨在让大家了解什么是Docker,并带领大家体验Docker使用的整个流程. 开启Docker学习之旅前,我们简单描述几个场景,应该很多人都有碰到过: 小凹同学开发了一个web应用,服务器环境是: ...
- 序列化 Serializable
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object states,但 ...
- UNIX索引技术访问文件初阶
背景: 软考里面,多次碰到一道题: 过程 以前对于这样的题,仅仅知道: 在文件系统中,文件的存储设备通常划分为若干个大小相等的物理块,每块长为512或1024字节.文件的理结构是指文件在存储设备上的存 ...
- ado.net增删改查操作
ado.net是数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层最基础的数据库访问技术 使用ado.net对数据 ...
- asp.net权限控制配置web.config
项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...
- windows安装python
1:首先去python网站下载安装包:https://www.python.org/downloads/,注意自己的系统版本 2:在自己指定目录安装即可: 3:将python路径加入PATH环境变量: ...
- react-基础(1)
props 创建组件 React.createClass; 直接继承React.Component;与上面不同的是初始化props和state的方法: export class Counter ext ...
- 为什么使用BeagleBoneBeagleBone的优点
为什么使用BeagleBone BeagleBone的优点 当前,一个典型的基于微控制器板的售价在120元左右,而BeagleBone Black的售价在330元左右.除了更强大的处理器之外,你额外的 ...
- 【Clr in c#】方法
1. 引用类型(class)与值类型(strust)的构造函数(实例构造器) 1, 创建一个引用类型的实例时,首先为实例的数据字段分配内存,然后初始对象的附加字段,最后调用实例构造器来设置对象的初始 ...