zoukankan      html  css  js  c++  java
  • zoj 1709 Oil Deposits

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=709

    解题思路:DFS搜索

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: zoj 1709
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 typedef long long LL;
     26 const double PI=acos(-1.0);
     27 ///////////////////////////////////////////////////////////////////////////
     28 
     29 ///////////////////////////////////////////////////////////////////////////
     30 //Add Code:
     31 int m,n,cnt;
     32 char oil[105][105];
     33 const int x[]={0,1,1,1,0,-1,-1,-1};
     34 const int y[]={1,1,0,-1,-1,-1,0,1};
     35 
     36 void DFS(int i,int j){
     37     if(i<1 || i>m || j<1 || j>n) return ;
     38     for(int p=0;p<8;p++){
     39         if(oil[i+x[p]][j+y[p]]=='@'){
     40             oil[i+x[p]][j+y[p]]='*';
     41             cnt--;
     42             DFS(i+x[p],j+y[p]);
     43         }
     44     }
     45     return ;
     46 }
     47 ///////////////////////////////////////////////////////////////////////////
     48 
     49 int main(){
     50     ///////////////////////////////////////////////////////////////////////
     51     //Add code:
     52     while(scanf("%d%d",&m,&n)!=EOF){
     53         if(!(m||n)) break;
     54         int i,j;
     55         char ch;
     56         cnt=0;
     57         scanf("%c",&ch);
     58         for(i=1;i<=m;i++){
     59             for(j=1;j<=n;j++){
     60                 scanf("%c",&oil[i][j]);
     61                 if(oil[i][j]=='@') cnt++;
     62             }
     63             scanf("%c",&ch);
     64         }
     65         int ans=0;
     66         while(cnt){
     67             for(i=1;i<=m;i++){
     68                 for(j=1;j<=n;j++){
     69                     if(oil[i][j]=='@'){
     70                         oil[i][j]='*';
     71                         cnt--;
     72                         DFS(i,j);
     73                         ans++;
     74                     }
     75                 }
     76             }
     77         }
     78         printf("%d
    ",ans);
     79     }
     80     ///////////////////////////////////////////////////////////////////////
     81     return 0;
     82 }
     83 
     84 ///////////////////////////////////////////////////////////////////////////
     85 /*
     86 Testcase:
     87 Input:
     88 1 1
     89 *
     90 3 5
     91 *@*@*
     92 **@**
     93 *@*@*
     94 1 8
     95 @@****@*
     96 5 5
     97 ****@
     98 *@@*@
     99 *@**@
    100 @@@*@
    101 @@**@
    102 0 0
    103 Output:
    104 0
    105 1
    106 2
    107 2
    108 */
    109 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    (Go)03.go类型
    (Go)02.go 安装delve调试工具测试
    (Go)01.Windows 安装 Go语言开发环境以及使用
    etcd创建集群并增加节点
    libhiredis.so.0.13 => not found 缺少
    Linux查找并杀死僵尸进程
    k8s istio 配置请求的路由规则
    k8s 安装并试用Istio service mesh
    k8s Job、Cronjob 的使用
    k8s Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3264220.html
Copyright © 2011-2022 走看看