• HDUOJ--------(1198)Farm Irrigation


    Farm Irrigation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4491    Accepted Submission(s): 1947

    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
     
    Author
    ZHENG, Lu
     
    Source
     
    Recommend
    Ignatius.L
     
    代码:
      1 /*龚细军 宽度优先搜索bfs*/
      2 #include<iostream>
      3 #include<queue>
      4 #include<cstdio>
      5 #include<cstring>
      6 using namespace std;
      7 typedef struct G
      8 {
      9  /*true 代表此处有piper*/
     10  bool up,down,left,right;
     11 }gong; 
     12 struct point
     13 {
     14     int x;
     15     int y;
     16 }start;
     17  //依次代表A~K的管道特性;
     18  gong go[]={1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,1,1,1,0,0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,1,1,1,1,0,1,1,1,1,1};
     19  
     20  /*前行的步奏*/
     21  char map[51][51];
     22  int n,ans,m;
     23 void input()
     24 {
     25   memset(map,'',sizeof(map));
     26   for(int i=0;i<m;i++)
     27       scanf("%s",map[i]);
     28 }
     29 void dfs()
     30 {
     31     /*判断是否联通,这样便于构造重位*/
     32     bool isrr,isll,isuu,isdd;
     33     queue<point> q;
     34     point p1,p2;
     35     q.push(start);
     36     bool isbian=false;
     37     while(!q.empty())
     38     {
     39         isrr=isll=isuu=isdd=false;
     40         p1=q.front();
     41         q.pop();
     42         /*向下搜索*/
     43         if(p1.x+1<m&&map[p1.x+1][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].down!=0&&go[map[p1.x+1][p1.y]-'A'].up!=0)
     44         {
     45             p2.x=p1.x+1;
     46             p2.y=p1.y;
     47             q.push(p2);
     48             isdd=true; //说明经过这里;            
     49         }
     50         /*向上搜索*/
     51         if(p1.x>=1&&map[p1.x-1][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].up!=0&&go[map[p1.x-1][p1.y]-'A'].down!=0)
     52         {
     53           p2.x=p1.x-1;
     54           p2.y=p1.y;
     55           q.push(p2);
     56           isuu=true;
     57         }
     58         /*向左搜索*/
     59         if(p1.y>=1&&map[p1.x][p1.y-1]>='A'&&go[map[p1.x][p1.y]-'A'].left!=0&&go[map[p1.x][p1.y-1]-'A'].right!=0)
     60         {
     61             p2.x=p1.x;
     62             p2.y=p1.y-1;
     63             q.push(p2);
     64             isll=true;
     65         }
     66         /*向右搜索*/
     67         if(p1.y+1<n&&map[p1.x][p1.y+1]>='A'&&go[map[p1.x][p1.y]-'A'].right!=0&&go[map[p1.x][p1.y+1]-'A'].left!=0)
     68         {
     69             p2.x=p1.x;
     70             p2.y=p1.y+1;
     71             q.push(p2);
     72             isrr=true;
     73         }
     74         if(isuu||isll||isrr||isdd)
     75         {
     76             map[p1.x][p1.y]='0';
     77         }
     78       else 
     79             if((p1.x>=1&&map[p1.x-1][p1.y]=='0')||(p1.x+1<m&&map[p1.x+1][p1.y]=='0')
     80               ||(p1.y>=1&&map[p1.x][p1.y-1]=='0')||(p1.y+1<n&&map[p1.x][p1.y+1]=='0'))
     81           {
     82               if(map[p1.x][p1.y]!='0')
     83               {
     84                isbian=true;
     85                map[p1.x][p1.y]='1';
     86               }
     87           }
     88       
     89         else ans++;
     90     }
     91     if(isbian) ans++;
     92 }
     93 int main()
     94 {
     95     int i,j;
     96    
     97    /* for(i=0;i<11;i++)
     98     {
     99         printf("%d %d %d %d
    ",go[i].up,go[i].down,go[i].left,go[i].right);
    100     }*/
    101     
    102     while(scanf("%d%d",&m,&n),(m!=-1||n!=-1))
    103     {
    104       ans=0;
    105       input( );
    106      for(i=0;i<m;i++)
    107      {
    108          for(j=0;j<n;j++)
    109          {
    110              if(map[i][j]>='A')
    111              {
    112               start.x=i;
    113               start.y=j;
    114               dfs();
    115              }
    116          }
    117      }
    118      printf("%d
    ",ans);
    119     }
    120     return 0;  
    121 }
     
  • 相关阅读:
    css换行和超出隐藏
    [转]ObjectARX SDK 下载全集
    ObjectARX延时动画之定时器简单示意
    ObjectARX延时动画效果简单示意
    ObjectARX选择集快还是遍历块表记录获取实体objectid快?
    ObjectARX插入属性块简单例子
    AcDbRegion面域交集布尔运算简单实例
    wblockCloneObjects从块定义写块到外部文件
    ObjectARX 创建AcDbLeader引线附着块对象实例
    ObjectARX JIG简单示意,实现正交例子
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3371389.html
Copyright © 2020-2023  润新知