zoukankan      html  css  js  c++  java
  • hdu1241Oil Deposits(dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

    用dfs求连通块。

     1 //读错题了,下面写的比较复杂,不过可以解决更复杂的连通块问题。
     2 //num.size()  为形状不同的油田数量!还可以求面积不同的连通块数量。
     3 //cnt才是油田数量,本题只是简单的连通块问题。
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<string>
     7 #include<set>
     8 using namespace std;
     9 set<int> uni;
    10 set<set<int> > num;
    11 int n,m;
    12 int cnt;
    13 char pic[110][110];
    14 int dir[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,1,-1,-1,1};
    15 
    16 void  dfs(int x,int y,int ox,int oy)
    17 {
    18     pic[x][y]='*';
    19     int xx=x-ox;
    20     int yy=y-oy;
    21     int c=xx*n+yy;
    22     uni.insert(c);
    23     for(int i=0;i<8;i++)
    24     {
    25         int nx=x+dir[i][0];
    26         int ny=y+dir[i][1];
    27         if(nx>=0&&nx<n&&ny>=0&&ny<m&&pic[nx][ny]=='@')
    28             dfs(nx,ny,ox,oy);
    29     }
    30     return ;
    31 
    32 
    33 }
    34 
    35 int main()
    36 {
    37     while(scanf("%d%d",&n,&m)&&(n||m))
    38     {
    39         cnt=0;
    40         num.clear();
    41         for(int i=0;i<n;i++)
    42             scanf("%s",pic[i]);
    43         for(int i=0;i<n;i++)
    44             for(int j=0;j<m;j++)
    45         {
    46             if(pic[i][j]=='@')
    47                 {
    48                     uni.clear();
    49                     dfs(i,j,i,j);
    50                     cnt++;
    51                     num.insert(uni);
    52                 }
    53         }
    54         printf("%d
    ",cnt);
    55     }
    56 }

     有兴趣可以看这道关于连通块的题:https://hihocoder.com/problemset/problem/1310

    题解:

     1 #include<cstdio>
     2 #include<set>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 int n,m;
     7 char pic[52][52];
     8 int dir[4][2]={0,1,0,-1,1,0,-1,0};
     9 int cnt;
    10 set<int>area;
    11 set<int>a;
    12 set<set<int> >shape;
    13 
    14 void dfs(int x,int y,int ox,int oy)
    15 {
    16     int w=(x-ox)*m+y-oy;
    17     a.insert(w);
    18     for(int i=0;i<4;i++)
    19     {
    20        x+=dir[i][0];
    21        y+=dir[i][1];
    22         if(x>=0&&x<n&&y>=0&&y<m&&pic[x][y]=='#')
    23         {
    24             pic[x][y]='.';
    25             dfs(x,y,ox,oy);
    26         }
    27         x-=dir[i][0];  //注意此处一定要改回去!
    28         y-=dir[i][1];
    29     }
    30 
    31 }
    32 int main()
    33 {
    34     while(~scanf("%d%d",&n,&m))
    35     {
    36         cnt=0;
    37         shape.clear();
    38         area.clear();
    39         for(int i=0;i<n;i++)
    40             scanf("%s",pic[i]);
    41 
    42         for(int i=0;i<n;i++)
    43             for(int j=0;j<m;j++)
    44         {
    45             if(pic[i][j]=='#')
    46             {
    47                 pic[i][j]='.';
    48                 a.clear();
    49                 dfs(i,j,i,j);
    50                 cnt++;
    51                 shape.insert(a);
    52                 area.insert((int)a.size());
    53 
    54             }
    55         }
    56         printf("%d %d %d
    ",cnt,area.size(),shape.size());
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    AS3打开本地文件
    Android上的Adobe AIR应用程序运行环境发布了!
    as3 创建遮罩层和自定义带参数的事件
    NetStream.appendBytes, 走向Flash P2P VOD的第一步
    BitmapData的整理
    AS3 in FlashDevelop
    站内站外AS3资源导航帖
    swf不能访问本地资源的解决办法
    JSON 序列化和反序列化——JavaScriptSerializer实现
    Json.net说法——(一)修饰标签,日期序列化
  • 原文地址:https://www.cnblogs.com/yijiull/p/6613283.html
Copyright © 2011-2022 走看看