poj-2386 Lake Counting

Source: Internet
Author: User

The first deep search problem, although very simple, but very happy, refueling!!!

Topic Links:

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

The topic has been handed down several times, has been re, again wrote again finally is a

The first re is because it is s[x][y]= ' # ' placed behind DFS and will be placed in front of the original change.

Re Code, Warning:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Chars[ the][ the];6 intN,m,total,tx,ty;7 intnext[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};8 intDfsintXinty)9 {Ten          for(intI=0;i<8; i++) One         { Atx=x+next[i][0]; -ty=y+next[i][1]; -             if(tx<0|| tx>=n| | ty<0|| ty>=m) the             Continue; -             if(s[tx][ty]=='W') - DFS (tx,ty); -s[x][y]='#'; +         } -         return 0; + } A intMain () at { -      while(~SCANF ("%d%d",&n,&m)) -     { -          for(intI=0; i<n;i++) -scanf"%s", S[i]); -Total=0; in          for(intI=0; i<n;i++) -         { to              for(intj=0; j<m;j++) +             { -                 if(s[i][j]=='W') the                 { * DFS (I,J); $total++;Panax Notoginseng                 } -             } the         } +printf"%d\n", total); A     } the     return 0; +}
View Code

AC Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Chars[ the][ the];6 intn,m,total;7 intnext[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};8 intDfsintXinty)9 {Ten     intTx,ty; Ones[x][y]='#'; A      for(intt=0;t<8; t++) -     { -tx=x+next[t][0]; thety=y+next[t][1]; -         if(tx>=n| | tx<0|| ty>=m| | ty<0) -         Continue; -         if(s[tx][ty]=='W') + DFS (tx,ty); -     } +     return 0; A } at intMain () - { -      while(~SCANF ("%d%d",&n,&m)) -     { -          for(intI=0; i<n;i++) -scanf"%s", S[i]); inTotal=0; -          for(intI=0; i<n;i++) to          for(intj=0; j<m;j++) +         if(s[i][j]=='W') -         { the DFS (I,J); *total++; $         }Panax Notoginsengprintf"%d\n", total); -     } the     return 0; +}
View Code

poj-2386 Lake Counting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.