zoukankan      html  css  js  c++  java
  • Oil Deposits

    hdu1241:http://acm.hdu.edu.cn/showproblem.php?pid=1241

    题意:就是找出有多少块有石油的区域,就是数组中的@,这边相邻指的是是周围的八个位置。

    题解:  dfs,从一块油田的位子开始,朝着与他相邻的8个方向收索,遇到油田就把它变成不是油田,并往下继续。遍历一下,就知道有多少了

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 char map[102][102];
     7 int m,n;
     8 void dfs(int i,int j){
     9      if(i<1||i>m||j<1||j>n)
    10        return;
    11       if(map[i-1][j]=='@')
    12       {  map[i-1][j]='*';
    13           dfs(i-1,j);
    14            }
    15        if(map[i+1][j]=='@')
    16       {  map[i+1][j]='*';
    17           dfs(i+1,j);
    18            }
    19         if(map[i][j+1]=='@')
    20       {  map[i][j+1]='*';
    21           dfs(i,j+1);
    22            }
    23       if(map[i][j-1]=='@')
    24         {  map[i][j-1]='*';
    25           dfs(i,j-1);
    26            }
    27            
    28       if(map[i+1][j+1]=='@')
    29       {  map[i+1][j+1]='*';
    30           dfs(i+1,j+1);
    31            }
    32        if(map[i-1][j+1]=='@')
    33       {  map[i-1][j+1]='*';
    34           dfs(i-1,j+1);
    35            }
    36       if(map[i-1][j-1]=='@')
    37       {  map[i-1][j-1]='*';
    38           dfs(i-1,j-1);
    39            }
    40         if(map[i+1][j-1]=='@')
    41       {  map[i+1][j-1]='*';
    42           dfs(i+1,j-1);
    43            }
    44            
    45 }
    46 int main(){
    47      int count;
    48    while(~(scanf("%d%d",&m,&n))&&m!=0){
    49      count=0;
    50      memset(map,0,sizeof(map));
    51       for(int i=1;i<=m;i++)
    52          for(int j=1;j<=n;j++){
    53            cin>>map[i][j];
    54          }
    55        for(int i=1;i<=m;i++)
    56           for(int j=1;j<=n;j++)
    57           { if(map[i][j]=='@'){
    58               count++;
    59               dfs(i,j);
    60            }
    61           }
    62           printf("%d
    ",count);
    63     }
    64   }
    View Code
  • 相关阅读:
    微信小程序开发入门(二)
    微信小程序开发入门(一)
    django入门与实践(续)
    django入门与实践(开)
    Python六剑客
    python入门(二十讲):爬虫
    python入门(十九讲):多进程
    ES6箭头函数
    ES6
    数据库常用命令
  • 原文地址:https://www.cnblogs.com/chujian123/p/3366547.html
Copyright © 2011-2022 走看看