a53

tsunami

a53
Oct 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /* 100 puncte
  2. Implementare -Dan Pracsiu
  3. Complexitate O(mxn)
  4. */
  5. #include<fstream>
  6. #define InFile "tsunami.in"
  7. #define OutFile "tsunami.out"
  8. using namespace std;
  9. struct coord
  10. {
  11. short x, y;
  12. };
  13. short a[1009][1009], b[1009][1009],L,C,h;
  14. coord q[1000001];
  15. int top,sol;
  16.  
  17. int main()
  18. {
  19. int i,j;
  20. ifstream fin(InFile);
  21. fin>>L>>C>>h;
  22. for(i=1;i<=L;++i)
  23. for(j=1;j<=C;++j)
  24. fin>>a[i][j];
  25. fin.close();
  26. for(i=0;i<=C+1;++i) /// Bordare
  27. a[0][i]=a[L +1][i]=1001;
  28. for(i=0;i<=L+1;++i)
  29. a[i][0]=a[i][C+1]=1001;
  30. sol=0; ///init fill
  31. top=-1;
  32. for(i=1;i<=C;++i)
  33. if(a[1][i]==0)
  34. b[1][i]=1,q[++top].x=1,q[top].y=i;
  35. for(i=1;i<=C;++i)
  36. if(a[L][i]==0)
  37. b[L][i]=1,q[++top].x=L,q[top].y=i;
  38. for(i=1;i<=L;++i)
  39. if(a[i][1]==0)
  40. b[i][1]=1,q[++top].x=i,q[top].y=1;
  41. for(i=1;i<=L;++i)
  42. if (a[i][C]==0)
  43. b[i][C]=1,q[++top].x=i,q[top].y=C;
  44. while(top>=0) /// fill
  45. {
  46. i=q[top].x;
  47. j=q[top].y;
  48. --top;
  49. if(b[i-1][j]!=1&&a[i-1][j]<h)
  50. {
  51. b[i-1][j]=1;
  52. ++top;
  53. q[top].x=i-1;
  54. q[top].y=j;
  55. if(a[i-1][j]>0)
  56. ++sol;
  57. }
  58. if(b[i+1][j]!=1&&a[i+1][j]<h)
  59. {
  60. b[i+1][j]=1;
  61. ++top;
  62. q[top].x =i+1;
  63. q[top].y=j;
  64. if(a[i+1][j]>0)
  65. ++sol;
  66. }
  67. if(b[i][j-1]!=1&&a[i][j-1]<h)
  68. {
  69. b[i][j-1]=1;
  70. ++top;
  71. q[top].x=i;
  72. q[top].y=j-1;
  73. if(a[i][j-1]>0)
  74. ++sol;
  75. }
  76. if(b[i][j+1]!=1&&a[i][j+1]<h)
  77. {
  78. b[i][j+1]=1;
  79. ++top;
  80. q[top].x=i;
  81. q[top].y=j+1;
  82. if(a[i][j+1]>0)
  83. ++sol;
  84. }
  85. }
  86. ofstream fout(OutFile);
  87. fout<<sol<<'\n';
  88. fout.close();
  89. return 0;
  90. }
Add Comment
Please, Sign In to add comment