zoukankan      html  css  js  c++  java
  • poj-2386 Lake Counting

    第一个深搜的问题,虽然很简单,但是很高兴,加油!!!

    题目链接:

    http://poj.org/problem?id=2386

    题目交了好几次,一直RE,又重新写了一遍总算是A了

    最初RE是由于是s[x][y]='#'放到了dfs的后面,以后要把最初的改变放在前面

    RE代码,引以为戒:

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 char s[105][105];
     6 int n,m,total,tx,ty;
     7 int next[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
     8 int  dfs(int x,int y)
     9 {
    10         for(int i=0;i<8;i++)
    11         {
    12             tx=x+next[i][0];
    13             ty=y+next[i][1];
    14             if(tx<0||tx>=n||ty<0||ty>=m)
    15             continue;
    16             if(s[tx][ty]=='W')
    17             dfs(tx,ty);
    18             s[x][y]='#';
    19         }
    20         return 0;
    21 }
    22 int main()
    23 {
    24     while(~scanf("%d%d",&n,&m))
    25     {
    26         for(int i=0;i<n;i++)
    27         scanf("%s",s[i]);
    28         total=0;
    29         for(int i=0;i<n;i++)
    30         {
    31             for(int j=0;j<m;j++)
    32             {
    33                 if(s[i][j]=='W')
    34                 {
    35                     dfs(i,j);
    36                     total++;
    37                 }
    38             }
    39         }
    40         printf("%d
    ",total);
    41     }
    42     return 0;
    43 }
    View Code

    AC代码:

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 char s[105][105];
     6 int n,m,total;
     7 int next[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
     8 int dfs(int x,int y)
     9 {
    10     int tx,ty;
    11     s[x][y]='#';
    12     for(int t=0;t<8;t++)
    13     {
    14         tx=x+next[t][0];
    15         ty=y+next[t][1];
    16         if(tx>=n||tx<0||ty>=m||ty<0)
    17         continue;
    18         if(s[tx][ty]=='W')
    19         dfs(tx,ty);
    20     }
    21     return 0;
    22 }
    23 int main()
    24 {
    25     while(~scanf("%d%d",&n,&m))
    26     {
    27         for(int i=0;i<n;i++)
    28         scanf("%s",s[i]);
    29         total=0;
    30         for(int i=0;i<n;i++)
    31         for(int j=0;j<m;j++)
    32         if(s[i][j]=='W')
    33         {
    34             dfs(i,j);
    35             total++;
    36         }
    37         printf("%d
    ",total);
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    在Ubuntu中通过update-alternatives切换软件版本
    SCons: 替代 make 和 makefile 及 javac 的极好用的c、c++、java 构建工具
    mongodb 的使用
    利用grub从ubuntu找回windows启动项
    How to Repair GRUB2 When Ubuntu Won’t Boot
    Redis vs Mongo vs mysql
    java script 的工具
    python 的弹框
    how to use greendao in android studio
    python yield的终极解释
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/5754422.html
Copyright © 2011-2022 走看看