Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void fill(int **a, int w, int h, int c, int y, int x){
  5.     if(y > 0 && a[y-1][x]==0){
  6.         a[y-1][x]=c;
  7.         fill(a,w,h,c,y-1,x);
  8.     }
  9.     if(y < h - 1 && a[y+1][x]==0){
  10.          a[y+1][x]=c;
  11.          fill(a,w,h,c,y+1,x);
  12.     }
  13.     if(x>0 && a[y][x-1]==0){
  14.         a[y][x-1]=c;   
  15.         fill(a,w,h,c,y,x-1);
  16.     }
  17.     if(x < w - 1 && a[y][x+1]==0){
  18.         a[y][x+1]=c;
  19.         fill(a,w,h,c,y,x+1);
  20.     }  
  21. }
  22.  
  23. int main() {
  24.  
  25.     int w,h,n;
  26.     scanf("%d %d \n %d",&w,&h,&n);
  27.    
  28.     int **field = (int**)calloc(h,sizeof(int*));
  29.  
  30.     for(int i = 0; i < h; i++){
  31.         field[i]=(int*)calloc(w,sizeof(int));
  32.     }
  33.  
  34.     for(int i = 0; i < n; i++){
  35.         char c;
  36.         int x,y,len;
  37.         scanf(" %c %d %d %d",&c,&x,&y,&len);
  38.        
  39.         if(c=='n'){
  40.             for(int j = 0; j < len; j++){
  41.                 field[y++][x] = -1;
  42.             }
  43.         }else{
  44.             for(int j = 0; j < len; j++){
  45.                 field[y][x++] = -1;
  46.             }
  47.         }
  48.     }
  49.  
  50.     int c = 1;
  51.     for(int i = 0; i < h; i++){
  52.         for(int j = 0; j < w; j++){
  53.             if(field[i][j]==0){
  54.                 fill(field,w,h,c,i,j);
  55.                 c++;
  56.             }
  57.         }
  58.     }
  59.  
  60.     printf("%d\n",c-1);
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement