hdu1198

链接:点击打开链接

题意:A~K代表n个管道图,给出一个m*n的二维字符数组,判断当整个图都能流到睡时需要放几口井

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[11][4] = {{1,1,0,0},{1,0,0,1},{0,1,1,0},{0,0,1,1},
                {1,0,1,0},{0,1,0,1},{1,1,0,1},{1,1,1,0},{0,1,1,1},
                {1,0,1,1},{1,1,1,1}};                   //代表每个图能走的方向
int xx[]={-1,0,1,0};
int yy[]={0,-1,0,1};
int sign[55][55];
char str[55][55];
int m,n;
void dfs(int x,int y){
    int i,tempx,tempy;
    sign[x][y]=1;
    for(i=0;i<4;i++){
        tempx=x+xx[i];                                  //向左的时候是y加减,不是x,因此上下走是x加减,
        tempy=y+yy[i];                                  //这个是二维数组与坐标最大的区别就是方向的变换
        if(tempx>=0&&tempx<m&&tempy>=0&&tempy<n&&a[str[x][y]-'A'][i]==1&&a[str[tempx][tempy]-'A'][(i+2)%4]==1&&sign[tempx][tempy]!=1){
        dfs(tempx,tempy);
        }
    }
}
int main(){
    int i,j,sum;
    while(scanf("%d%d",&m,&n)!=EOF){
        if(m==-1&&n==-1)
        break;
        sum=0;
        for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        cin>>str[i][j];
        memset(sign,0,sizeof(sign));
        for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        if(!sign[i][j]){
            dfs(i,j);                                   //然后就是很简单的dfs了
            sum++;
//            cout<<i<<" "<<j<<endl;
//            for(int p=0;p<m;p++){
//                for(int q=0;q<n;q++)
//                cout<<sign[p][q]<<" ";
//                cout<<endl;
            }
        printf("%d\n",sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 09:40:05

hdu1198的相关文章

【HDU1198】Farm Irrigation(回溯+记忆化搜索)

数据流小,深搜即可.有些暴力.看其他人的题解用二维转换成一维做的并查集很巧妙,马上去研究一下!! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <num

hdu1198(并查集)

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

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

hdu1198 Farm Irrigation dfs

Problem Description 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 pi

并查集应用hdu1198

就是给并查集换了个形式 判断是否能连接那我写的比较麻烦 可以用四个字符表示四个方向 01开关 判断的时候只需要判断两个方向 一开始感觉无从想法 一步步顺着想有些还是会变简答的 #include<cstdio> #include<algorithm> using namespace std; char a[51][51]; int sett[2500 + 50]; int find2(int x) { while(x!=sett[x] ) x=sett[x]; return x; }

HDU--1198 Farm Irrigation (并查集做法+DFS做法)

Problem Description 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 pi

并查集练习1

举头望明月,低头敲代码... 推荐学习地址:http://www.cnblogs.com/cyjb/p/UnionFindSets.html 简单: hdu1213 How Many Tables:新手必秒 1 #include<cstdio> 2 const int N=1001; 3 int f[N]; 4 void init(int n){ 5 for(int i=1;i<=n;++i) 6 f[i]=i; 7 } 8 int fin(int x){ 9 if(x!=f[x])f[

并查集知识学习

(转) 并查集的作用:并和查,即合并和查找,将一些集合合并,快速查找或判断某两个集合的关系,或某元素与集合的关系,或某两个元素的关系. 并查集的结构:并查集主要操作对象是森林,树的结构赋予它独特的能力,对整个集合操作转换为对根节点(或称该集合的代表元素)的操作,一个集合里的元素关系不一定确定,但相对于根节点的关系很明了,这也是为了查找方便. 并查集优化方法:按秩合并和路径压缩的配合使用,使得查找过程优化到极致.按秩合并,每次将深度小的树合并到深度大的树里面去,使得整棵树尽量矮:路径压缩,将当前节

并查集 专题总结

一.题目类型: 1.普通并查集: poj2513 Colored Sticks hdu1198 Farm Irrigation SCAU 1138 代码等式 Gym - 100676F Palindrome Codeforces Round #363 (Div. 2) D. Fix a Tree Codeforces Round #376 (Div. 2) C. Socks 2.种类并查集: HDU3038 How Many Answers Are Wrong POJ1182 食物链 POJ24