Advertisement
hieudoan

UVa 11953 Battleships

Aug 6th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int T,N;
  5. char grid[105][105];
  6.  
  7. int dr[]={-1,0,1,0};  //chay theo chiu kim dong ho
  8. int dc[]={0,1,0,-1};
  9.  
  10. void floodfill(int r,int c){  
  11.     if(r<0||r>=N||c<0||c>=N ||grid[r][c]=='.') return ;
  12.     grid[r][c]='.';
  13.     for(int d=0;d<4;d++)
  14.         floodfill(r+dr[d],c+dc[d]);
  15. }
  16.  
  17. int main()
  18. {
  19.     // freopen("11953 in.txt","r",stdin);
  20.     // freopen("11953 out.txt","w",stdout);
  21.  
  22.     scanf("%d",&T);
  23.     for(int t=1;t<=T;t++){
  24.         scanf("%d",&N);
  25.         for(int i=0;i<N;i++)
  26.             scanf("%s",grid[i]);
  27.  
  28.  
  29.         int ans=0;
  30.         for(int i=0;i<N;i++){
  31.             for(int j=0;j<N;j++){
  32.                 if(grid[i][j]=='x'){
  33.                     ans++;
  34.                     floodfill(i,j);
  35.                 }
  36.             }
  37.         }
  38.         printf("Case %d: %d\n",t,ans);
  39.     }
  40.  
  41.     return 0;
  42. }
  43.  
  44. //By hieudoanitus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement