zoukankan      html  css  js  c++  java
  • hdu 1198 Farm Irrigation

    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 pipes, which is marked from A to K, as Figure 1 shows.


    Figure 1


    Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map 

    ADC
    FJK
    IHE

    then the water pipes are distributed like 


    Figure 2


    Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn. 

    Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him? 

    Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
     
    Input
    There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
     
    Output
    For each test case, output in one line the least number of wellsprings needed.
     
    Sample Input
    2 2
    DK
    HF
    3 3
    ADC
    FJK
    IHE
    -1 -1
     
    Sample Output
    2
    3
     
    http://acm.hdu.edu.cn/showproblem.php?pid=1198;
    简单搜索题目;
    题目思路:
    1、首先把A——F的每一个形状描述出来(可以与另一个想通的赋值为1,无法相通的赋值为0,每一个方形都有四个值up,down,left,right);
    2、判断与下一个方形是否会相通,根据相对位置,如果本身在下一个方形的上面,则判断本身的down与下一个方形的up值是不是都是1,是的话,就能相通,则标记次方形。
    其他的相对位置同理去思考,不作具体解释。。。
      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <queue>
      4 #include <string.h>
      5 using namespace std;
      6 struct node
      7 {
      8     int up,down,left,right;
      9 }dd[11];
     10 
     11 int dir[][2]={{1,0},{0,1},{-1,0},{0,-1}};
     12 
     13 struct node2
     14 {
     15     int xx,yy;
     16 };
     17 
     18 int m,n;
     19 int map[55][55];
     20 bool flag[55][55];
     21 queue<node2>q;
     22 
     23 bool judge(node2 p,node2 temp,int kk)
     24 {
     25     if(kk==0)//上下;
     26     {
     27         if(dd[map[p.xx][p.yy]].down==1 && dd[map[temp.xx][temp.yy]].up==1)
     28         {
     29             return true;
     30         }
     31         else
     32         {
     33             return false;
     34         }
     35     }
     36     else if(kk==1)//左右;
     37     {
     38         if(dd[map[p.xx][p.yy]].right==1 && dd[map[temp.xx][temp.yy]].left==1)
     39         {
     40             return true;
     41         }
     42         else
     43         {
     44             return false;
     45         }
     46     }
     47     else if(kk==2)//下上;
     48     {
     49         if(dd[map[p.xx][p.yy]].up==1 && dd[map[temp.xx][temp.yy]].down==1)
     50         {
     51             return true;
     52         }
     53         else
     54         {
     55             return false;
     56         }
     57     }
     58     else   //右左;
     59     {
     60         if(dd[map[p.xx][p.yy]].left==1 && dd[map[temp.xx][temp.yy]].right==1)
     61         {
     62             return true;
     63         }
     64         else
     65         {
     66             return false;
     67         }
     68     }
     69 }
     70 
     71 bool judge2(node2 temp)
     72 {
     73     if(temp.xx<0 || temp.xx>=m || temp.yy<0 || temp.yy>=n || flag[temp.xx][temp.yy])
     74     return false;
     75     return true;
     76 }
     77 
     78 void bfs(int x,int y)
     79 {
     80     while(!q.empty())
     81     {
     82         q.pop();
     83     }
     84     node2 temp,p;
     85     int i;
     86     p.xx=x;
     87     p.yy=y;
     88     q.push(p);
     89     flag[x][y]=true;
     90     while(!q.empty())
     91     {
     92         p=q.front();
     93         q.pop();
     94         for(i=0;i<4;i++)
     95         {
     96             temp=p;
     97             temp.xx+=dir[i][0];
     98             temp.yy+=dir[i][1];
     99             if(!judge2(temp))
    100             continue;
    101             if(judge(p,temp,i))
    102             {
    103                 q.push(temp);
    104                 flag[temp.xx][temp.yy]=true;
    105             }
    106         }
    107     }
    108 
    109 }
    110 void init()
    111 {
    112     dd[0].up=1,dd[0].down=0,dd[0].left=1,dd[0].right=0;
    113     dd[1].up=1,dd[1].down=0,dd[1].left=0,dd[1].right=1;
    114     dd[2].up=0,dd[2].down=1,dd[2].left=1,dd[2].right=0;
    115     dd[3].up=0,dd[3].down=1,dd[3].left=0,dd[3].right=1;
    116     dd[4].up=1,dd[4].down=1,dd[4].left=0,dd[4].right=0;
    117     dd[5].up=0,dd[5].down=0,dd[5].left=1,dd[5].right=1;
    118     dd[6].up=1,dd[6].down=0,dd[6].left=1,dd[6].right=1;
    119     dd[7].up=1,dd[7].down=1,dd[7].left=1,dd[7].right=0;
    120     dd[8].up=0,dd[8].down=1,dd[8].left=1,dd[8].right=1;
    121     dd[9].up=1,dd[9].down=1,dd[9].left=0,dd[9].right=1;
    122     dd[10].up=1,dd[10].down=1,dd[10].left=1,dd[10].right=1;
    123     memset(flag,false,sizeof(flag));
    124 
    125 }
    126 int main()
    127 {
    128     while(~scanf("%d%d",&m,&n))
    129     {
    130         int i,j;
    131         if(m<0 || n<0)
    132         break;
    133         init();
    134         char op;
    135         for(i=0;i<m;i++)
    136         {
    137             for(j=0;j<n;j++)
    138             {
    139                 cin>>op;
    140                 map[i][j]=op-'A';
    141             }
    142         }
    143 
    144         int cnt=0;
    145 
    146         for(i=0;i<m;i++)
    147         {
    148             for(j=0;j<n;j++)
    149             {
    150                 if(!flag[i][j])
    151                 {
    152                     bfs(i,j);
    153                     cnt++;
    154                 }
    155             }
    156         }
    157         printf("%d\n",cnt);
    158 
    159     }
    160     return 0;
    161 }
    View Code
  • 相关阅读:
    Mybatis和Spring整合也是能用BatchExecutor的
    与Spring整合的Mybatis没法真正使用BatchExecutor
    Mybatis和Spring整合后sqlsession啥时候关闭的
    Mybatis和Spring的整合原理
    Mybatis是怎么执行一条语句的
    8.11查询结果排序
    8.10、11(select分组和过滤)()
    8.7、8、9(select语句基本用法)(select语句基本运算符)(select聚合查询)
    8.4SQL(DML数据操作语言)-(insert插入数据)(updata更新数据),(delete数据)
    8.2数据库DDL语言(即数据库定义语言)(命名规则以及数据类型)
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3077818.html
Copyright © 2011-2022 走看看