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=1005;
int A[3][3]={{0,1,0},{1,1,0},{0,0,0}};
int B[3][3]={{0,1,0},{0,1,1},{0,0,0}};
int C[3][3]={{0,0,0},{1,1,0},{0,1,0}};
int D[3][3]={{0,0,0},{0,1,1},{0,1,0}};
int E[3][3]={{0,1,0},{0,1,0},{0,1,0}};
int F[3][3]={{0,0,0},{1,1,1},{0,0,0}};
int G[3][3]={{0,1,0},{1,1,1},{0,0,0}};
int H[3][3]={{0,1,0},{1,1,0},{0,1,0}};
int I[3][3]={{0,0,0},{1,1,1},{0,1,0}};
int J[3][3]={{0,1,0},{0,1,1},{0,1,0}};
int K[3][3]={{0,1,0},{1,1,1},{0,1,0}};
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int maze[mx][mx];
bool visited[mx][mx];
int m,n,wellspring;
bool judge(int x,int y)
{
    if(x>=0&&x<3*m&&y>=0&&y<3*n&&maze[x][y]==1&&!visited[x][y])
        return true;
    return false;
}
void dfs(int x,int y)
{
    int dx,dy;
    for(int i=0;i<4;i++)
    {
        dx=x+dir[i][0];
        dy=y+dir[i][1];
        if(judge(dx,dy))
        {
            visited[dx][dy]=true;
             dfs(dx,dy);
        }
    }
}
void creat_maze(int a[][3],int i,int j)
{
    int p,q;
    for(p=i*3;p<i*3+3;p++)
    {
        for(q=j*3;q<j*3+3;q++)
        {
            maze[p][q]=a[p-i*3][q-j*3];
        }
    }
}
void wellspring_count()
{
    int i,j;
    for(i=0;i<3*m;i++)
    {
        for(j=0;j<3*n;j++)
        {
            if(maze[i][j]==1&&!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!=-1||n!=-1)
    {
        memset(visited,false,sizeof(visited));
        wellspring=0;
        char pipe;
        for(i=0;i<m;i++)
       {
           getchar();
        for(j=0;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 0;
}

但是这是一个并查集专题里的题,所以应该还会有更高效的方法,毕竟我的代码里没有用到并查集。

所以又想了一下,瞬变看了一下别人的并查集思路,又写了一个代码。

时间: 2024-10-13 11:42:58

hdu Farm Irrigation的相关文章

hdu 1198 Farm Irrigation (搜索或并查集)

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

Farm Irrigation HDU - 1198 (并查集)

Farm Irrigation HDU - 1198 题意:给11种管道,问草地最少需要打多少个井才可以全部灌溉. 把每种管道的状态用二进制表示一下,然后对每一块草地,判断能否和上面或者左面的草地的管道连接. 然后并查集搞一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=55; 4 5 int g[12]={10,9,6,5,12,3,11,14,7,13,15}; 6 int f[maxn*maxn]

HDU 1198 Farm Irrigation (并查集)

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

HDU 1198 Farm Irrigation

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

(hdu step 5.1.4)Farm Irrigation(在两个节点合并有限制条件的情况下,求集合的个数)

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

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

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 38   Accepted Submission(s) : 24 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious

HDU 1198 Farm Irrigation(并查集,自己构造连通条件)

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

HDU 1198-Farm Irrigation(并查集)

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

Farm Irrigation

题目:Farm Irrigation 题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494 题目思路:并查集 1 #include<stdio.h> 2 //并查集重点就是实现:查询某点在哪个集合.将两个集合合并 3 //查找点的祖先 4 int a[10000]; 5 int fun(int x) 6 { 7 int p=x; 8 // 初始化数组 a[i]=i; 9 // 也就是说当 a[i]==i 时,这是一个祖先,或者他是别人的子