http://acm.hdu.edu.cn/showproblem.php?pid=1198

裸并查集,主要工作在根据题目给出关系构图

#include <iostream>
#include <cstdio>
#include <map>
using namespace std ;
int idx[] ;
int m,n ;
char M[][] ;
int find(int x)
{
return idx[x]==x ? x : idx[x]=find(idx[x]) ;
}
int st ;
int dx[]={,-,,} ;
int dy[]={,,,-} ;
void check()
{
for(int i= ;i<m ;i++)
{
for(int j= ;j<n ;j++)
{
for(int k= ;k< ;k++)
{
int xx=i+dx[k] ;
int yy=j+dy[k] ;
if(xx< || xx>=m || yy< || yy>=n)continue ;
char t1=M[i][j] ;
char t2=M[xx][yy] ;
if(dx[k]==)
{
if((t1=='C' || t1=='D' || t1=='E' || t1=='H' || t1=='I' || t1=='J' || t1=='K') && (t2=='A' || t2=='B' || t2=='E' || t2=='G' || t2=='H' || t2=='J' || t2=='K'))
{
int pp=find(st) ;
int qq=find(st+n) ;
if(pp!=qq)
{
idx[pp]=qq ;
}
}
}
else if(dx[k]==-)
{
if((t1=='A' || t1=='B' || t1=='E' || t1=='H' || t1=='G' || t1=='J' || t1=='K') && (t2=='C' || t2=='D' || t2=='E' || t2=='H' || t2=='I' || t2=='J' || t2=='K'))
{
int pp=find(st) ;
int qq=find(st-n) ;
if(pp!=qq)
{
idx[pp]=qq ;
}
}
}
else if(dy[k]==)
{
if((t1=='B' || t1=='D' || t1=='F' || t1=='G' || t1=='I' || t1=='J' || t1=='K') && (t2=='A' || t2=='C' || t2=='F' || t2=='G' || t2=='H' || t2=='I' || t2=='K'))
{
int pp=find(st) ;
int qq=find(st+) ;
if(pp!=qq)
{
idx[pp]=qq ;
}
}
}
else if(dy[k]==-)
{
if((t1=='A' || t1=='C' || t1=='F' || t1=='G' || t1=='I' || t1=='H' || t1=='K') && (t2=='B' || t2=='D' || t2=='F' || t2=='G' || t2=='I' || t2=='J' || t2=='K'))
{
int pp=find(st) ;
int qq=find(st-) ;
if(pp!=qq)
{
idx[pp]=qq ;
}
}
}
}
st++ ;
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
if(m==- && n==-)break ;
for(int i= ;i<m ;i++)
scanf("%s",M[i]) ;
for(int i= ;i<=n*m ;i++)
idx[i]=i ;
st= ;
check() ;
map <int,int> mp ;
int ans= ;
for(int i= ;i<=n*m ;i++)
{
int fa=find(i) ;
if(!mp[fa])
{
mp[fa]= ;
ans++ ;
}
}
printf("%d\n",ans) ;
}
return ;
}

HDU 1198的更多相关文章

  1. 图论问题(1) : hdu 1198

    题目转自hdu 1198,题目传送门 题目大意: 给你11种单位水管摆放位置,若上下或左右有水管连接则视为这两点相连. 最后让你求这些张图中有几个连通块. 解题思路: 本来觉得这道题很简单,不就一个建 ...

  2. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

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

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

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

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

  5. HDU 1198 Farm Irrigation (并查集优化,构图)

    本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...

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

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

  7. HDU 1198(并查集)

    题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...

  8. hdu 1198 Farm Irrigation

    令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...

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

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

随机推荐

  1. Django Rest Framework(3)-----APIView与Viewsets

    REST framework提供了一个APIView类,它是Django的View类的子类. REST framework主要的几种view以及他们之间的关系: mixins 到目前为止,我们使用的创 ...

  2. (转)《SSO CAS单点系列》之 实现一个SSO认证服务器是这样的!

    上篇我们引入了SSO这个话题<15分钟了解SSO是个什么鬼!>.本篇我们一步步深入分析SSO实现机理,并亲自动手实现一个线上可用的SSO认证服务器!首先,我们来分析下单Web应用系统登录登 ...

  3. PAT 1065 A+B and C[大数运算][溢出]

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  4. Selenium之firefox浏览器的启动问题及解决

    启动firefox报错如下: rg.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 ...

  5. java构建树用的Node

    package org.ccnt.med.body; import java.util.ArrayList; import java.util.List; public class Node { // ...

  6. 4.7 Routing -- Redirecting

    一.Transitioning and Redirection 从一个route调用transitionTo或者从一个controller调用transitionToRoute将会停止任何进程中的任何 ...

  7. 22. Generate Parentheses(回溯)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online Solution

    A    Live Love 水. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; const i ...

  9. jQuery源码分析--Event模块(2)

    接下来就是触发事件了.事件触发后的处理函数的分发主要靠两个函数,一个jQuery.event.dispatch,一个是jQuery.event.handlers.这个dispatch会调用handle ...

  10. 使用NodeJS将文件或图像上传到服务器

    原文链接:http://www.codeceo.com/article/nodejs-upload-file-to-server.html