[C++] poj 2386 →→→→→进入此内容的聊天室

来自 , 2020-02-10, 写在 C++, 查看 109 次.
URL http://www.code666.cn/view/29530de2
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<cctype>
  6. #include<stack>
  7. #include<queue>
  8. #include<algorithm>
  9. #include<iostream>
  10. using namespace std;
  11. #define MAXN 100+5
  12. int N,M;
  13. char str[MAXN][MAXN];
  14. int pos[9][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,0},{0,1},{1,-1},{1,0},{1,1}};
  15. void dfs(int x,int y)
  16. {
  17.     str[x][y]='.';
  18.     int i,j;
  19.     for(i=0;i<9;i++)
  20.     {
  21.         x+=pos[i][0];
  22.         y+=pos[i][1];
  23.         if(x>=0&&x<N&&y>=0&&y<M&&str[x][y]=='W') dfs(x,y);
  24.         x-=pos[i][0];
  25.         y-=pos[i][1];
  26.     }
  27.     return ;
  28. }
  29. int main()
  30. {
  31.     while(scanf("%d%d",&N,&M)!=EOF)
  32.     {
  33.         int i,j;
  34.         for(i=0;i<N;i++)
  35.         {
  36.             scanf("%s",str[i]);
  37.         }
  38.         int ans=0;
  39.         for(i=0;i<N;i++)
  40.         {
  41.             for(j=0;j<M;j++)
  42.             {
  43.                 if(str[i][j]=='W')
  44.                 {
  45.                     dfs(i,j);
  46.                     ans++;
  47.                 }
  48.             }
  49.         }
  50.         printf("%d\n",ans);
  51.     }
  52.     return 0;
  53. }
  54.  

回复 "poj 2386"

这儿你可以回复上面这条便签

captcha