fork(1) download
  1. #include <cstdio>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int n, wyn;
  8.  
  9. char T[1007][1007];
  10.  
  11. int ix;
  12. int X[10000000];
  13. int Y[10000000];
  14.  
  15. void rek(int x, int y)
  16. {
  17. if (T[x][y] != '@') return;
  18.  
  19. if (x>0 && T[x-1][y] == '.') {wyn--; T[x-1][y] = '@'; X[ix]=x-1; Y[ix++]=y;}
  20. if (x<n-1 && T[x+1][y] == '.') {wyn--; T[x+1][y] = '@'; X[ix]=x+1; Y[ix++]=y;}
  21. if (y>0 && T[x][y-1] == '.') {wyn--; T[x][y-1] = '@'; X[ix]=x; Y[ix++]=y-1;}
  22. if (y<n-1 && T[x][y+1] == '.') {wyn--; T[x][y+1] = '@'; X[ix]=x; Y[ix++]=y+1;}
  23.  
  24. if (x>0 && y>0 && T[x-1][y-1] == '.') {wyn--; T[x-1][y-1] = '@'; X[ix]=x-1; Y[ix++]=y-1;}
  25. if (x<n-1 && y<n-1 && T[x+1][y+1] == '.') {wyn--; T[x+1][y+1] = '@'; X[ix]=x+1; Y[ix++]=y+1;}
  26. if (y>0 && x < n-1 && T[x+1][y-1] == '.') {wyn--; T[x+1][y-1] = '@'; X[ix]=x+1; Y[ix++]=y-1;}
  27. if (y<n-1 && x>0 && T[x-1][y+1] == '.') {wyn--; T[x-1][y+1] = '@'; X[ix]=x-1; Y[ix++]=y+1;}
  28.  
  29.  
  30. }
  31.  
  32. int main()
  33. {
  34. int t;
  35. scanf("%d\n", &t);
  36. wyn = 0;
  37. for(int tx = 0; tx < t; tx++)
  38. {
  39. ix=0;
  40. scanf("%d\n", &n);
  41. wyn = n*n;
  42.  
  43. char a;
  44. for(int i = 0; i < n; i++)
  45. {
  46. for(int j = 0; j < n; j++)
  47. {
  48. a = getchar();
  49. T[i][j] = a;
  50. }
  51. char foo = getchar();
  52. }
  53.  
  54. /*
  55. for(int i = 0; i < n; i++)
  56. {
  57. for(int j = 0; j < n; j++)
  58. printf("%c", T[i][j]);
  59. printf("\n");
  60. }*/
  61.  
  62. //lewa
  63. for(int i = 0; i < n; i++) if (T[0][i] == '.') {wyn--; T[0][i] = '@'; X[ix]=0; Y[ix++]=i;}
  64.  
  65. //prawa
  66. for(int i = 0; i < n; i++) if (T[n-1][i] == '.') {wyn--; T[n-1][i] = '@'; X[ix]=n-1; Y[ix++]=i;}
  67.  
  68. //dol
  69. for(int i = 0; i < n; i++) if (T[i][0] == '.') {wyn--; T[i][0] = '@'; X[ix]=i; Y[ix++]=0;}
  70.  
  71. //gora
  72. for(int i = 0; i < n; i++) if (T[i][n-1] == '.') {wyn--; T[i][n-1] = '@'; X[ix]=i; Y[ix++]=n-1;}
  73.  
  74. for(int i = 0; i < ix; i++) rek(X[i], Y[i]);
  75.  
  76. /*
  77. for(int i = 0; i < n; i++)
  78. {
  79. for(int j = 0; j < n; j++)
  80. printf("%c", T[i][j]);
  81. printf("\n");
  82. }*/
  83. printf("%d\n", wyn);
  84. }
  85. return 0;
  86. }
stdin
2
7
.......
#######
#.....#
#.###.#
###.###
.......
.......
3
###
#.#
###
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:51: warning: unused variable ‘foo’
prog.cpp:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:40: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
27
9