wojiaocbj

dfs

May 15th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. /*
  2.  Author: 曹北健(37509)
  3.  Result: AC Submission_id: 4439921
  4.  Created at: Sun May 15 2022 15:15:51 GMT+0800 (China Standard Time)
  5.  Problem: 5779  Time: 6 Memory: 1784
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #pragma warning(disable:4996)
  15. int n,m;
  16. char map[114][514] = {0};
  17. void dfs(int x,int y){
  18.     if(map[x][y] == '*')return;
  19.     else{
  20.         map[x][y] = '*';
  21.         int dx,dy;
  22.         for(dx = -1;dx <= 1;dx++){
  23.             for(dy = -1;dy <= 1;dy++){
  24.                 if(x + dx >= 0 && x + dx < n && y + dy >= 0 && y + dy < m){
  25.                     dfs(x + dx,y + dy);
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
  31. int main(){
  32.     int i,j,ans = 0;
  33.     scanf("%d%d",&n,&m);
  34.     for(i = 0;i < n;i++){
  35.         scanf("%s",map[i]);
  36.     }
  37.     for(i = 0;i < n;i++){
  38.         for(j = 0;j < m;j++){
  39.             if(map[i][j] == 'W'){
  40.                 dfs(i,j);ans++;
  41.             }
  42.         }
  43.     }
  44.     printf("%d\n",ans);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment