fork(1) download
  1. #include<stdio.h>
  2. #include <queue>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct POS{
  8. int row_no;
  9. int col_no;
  10. };
  11.  
  12. queue<POS> Q;
  13.  
  14. POS get_POS(int row,int col){
  15. POS p;
  16. p.row_no = row;
  17. p.col_no = col;
  18. return p;
  19. }
  20.  
  21. void FILL_PIXELS(char filename[10]){
  22. FILE *p = fopen(filename,"r");
  23. if(p==NULL)
  24. exit(1);
  25. //while()
  26. int rows=0,cols=0;
  27. fscanf(p,"%d %d\n",&rows,&cols);
  28. char array[rows][cols+1];
  29. //fflush(stdin);
  30. int i=0;
  31. for(;i<rows-1;i++){
  32. fgets(array[i],cols+2,p);//fgets(array[i],cols,stdin);
  33. //printf("%3d %s",i,array[i]);
  34. }
  35. fgets(array[i],cols+1,p);
  36. //printf("%s\n",array[i]);
  37. //char queue[rows*cols];
  38. fclose(p);
  39. int no_of_fills=0;
  40. for(int i=0;i<rows;i++){
  41. //printf("%d\n",i);
  42. for(int j=0;j<cols;j++){
  43. if(array[i][j] == '0'){
  44. no_of_fills++;
  45. POS p;
  46. p.row_no = i;
  47. p.col_no = j;
  48. //array[i][j] = '1';
  49. Q.push(p);
  50. array[i][j] = '1';
  51. while(!Q.empty()){
  52. POS p = Q.front();
  53. Q.pop();
  54. int x = p.row_no;
  55. int y = p.col_no;
  56. //array[x][y] = '1';
  57. //printf("%3d,%3d\n",x,y);
  58. if(x>0 && y>0 && array[x-1][y-1] == '0'){
  59. Q.push(get_POS(x-1,y-1));
  60. array[x-1][y-1] = '1';
  61. }
  62. if(x>0 && array[x-1][y]=='0'){
  63. Q.push(get_POS(x-1,y));
  64. array[x-1][y] = '1';
  65. }
  66. if(x>0 && y<(cols-1) && array[x-1][y+1]=='0'){
  67. Q.push(get_POS(x-1,y+1));
  68. array[x-1][y+1] = '1';
  69. }
  70. if(y>0 && array[x][y-1]=='0'){
  71. Q.push(get_POS(x,y-1));
  72. array[x][y-1] = '1';
  73. }
  74. if(y<(cols-1) && array[x][y+1]=='0'){
  75. Q.push(get_POS(x,y+1));
  76. array[x][y+1] = '1';
  77. }
  78. if(y>0 && x<(rows-1) && array[x+1][y-1]=='0'){
  79. Q.push(get_POS(x+1,y-1));
  80. array[x+1][y-1] = '1';
  81. }
  82. if(x<(rows-1) && array[x+1][y]=='0'){
  83. Q.push(get_POS(x+1,y));
  84. array[x+1][y] = '1';
  85. }
  86. if(x<(rows-1) && y<(cols-1) && array[x+1][y+1]=='0'){
  87. Q.push(get_POS(x+1,y+1));
  88. array[x+1][y+1] = '1';
  89. }
  90. }
  91. }
  92. }
  93. }
  94. /*p = fopen("a.out","w");
  95. fprintf(p,"%d",no_of_fills);
  96. fclose(p);*/
  97. printf("%d\n",no_of_fills);
  98. }
  99.  
  100.  
  101. int main(int argc,char **argv){
  102. FILL_PIXELS(argv[1]);
  103. }
Runtime error #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
Standard output is empty