• Source
    1. #include<bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. char str[1003][1003];
    6. int len,counter;
    7.  
    8. bool check[103][103];
    9.  
    10. void dfs(int i,int j)
    11. {
    12. if(i<0||j<0||i>=len||str[i][j]=='L'||str[i][j]=='\0')
    13. {
    14. return ;
    15. }
    16.  
    17. if(check[i][j]==true)
    18. {
    19. return ;
    20. }
    21.  
    22. if(str[i][j]=='W' && check[i][j]==false)
    23. {
    24. counter++;
    25. check[i][j] = true;
    26. dfs(i+1,j);
    27. dfs(i-1,j);
    28. dfs(i,j+1);
    29. dfs(i,j-1);
    30. dfs(i-1,j+1);
    31. dfs(i-1,j-1);
    32. dfs(i+1,j-1);
    33. dfs(i+1,j+1);
    34. }
    35. }
    36.  
    37. int main()
    38. {
    39. int test,i,j,a,b,x;
    40.  
    41. scanf("%d",&test);
    42.  
    43. getchar();
    44. getchar();
    45.  
    46. for(x=1; x<=test; x++)
    47. {
    48. if(x>1)printf("\n");
    49.  
    50. j=0;
    51.  
    52. bool flag = true;
    53.  
    54. while(gets(str[j]))
    55. {
    56.  
    57. if(str[j][0]=='\0')break;
    58.  
    59. if(str[j][0]>='0' && str[j][0]<='9')
    60. {
    61. if(flag)
    62. {
    63. flag = false;
    64. len = j ;
    65. }
    66.  
    67. sscanf(str[j],"%d %d",&a,&b);
    68.  
    69. counter=0;
    70.  
    71. dfs(a-1,b-1);
    72.  
    73. printf("%d\n",counter);
    74.  
    75. memset(check,false,sizeof check);
    76. }
    77.  
    78. j++;
    79. }
    80.  
    81. memset(str,'\0',sizeof str);
    82.  
    83. }
    84.  
    85. return 0 ;
    86. }
    87.