fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string str[105];
  5. int row_c[105];
  6. int col_c[105];
  7. vector <int> r_cuts;
  8. int main()
  9. {
  10. // cout << "Hello World!" << endl;
  11. ios::sync_with_stdio(false);
  12. int t;
  13. cin>>t;
  14. for(int caseno=1;caseno<=t;caseno++){
  15. cout<<"Case #"<<caseno<<": ";
  16. for(int i=0;i<105;i++){
  17. row_c[i]=0;
  18. col_c[i]=0;
  19. }
  20. r_cuts.erase(r_cuts.begin(),r_cuts.end());
  21. int r,c,h,v;
  22. cin>>r>>c>>h>>v;
  23. int chips=0;
  24.  
  25. for(int i=0;i<r;i++){
  26. cin>>str[i];
  27. }
  28. for(int i=0;i<r;i++){
  29. for(int j=0;j<c;j++){
  30. if(str[i][j]=='@'){
  31. chips++;
  32. row_c[i]++;
  33. }
  34. }
  35. }
  36. // for(int i=0;i<r;i++)cout<<row_c[i]<<",,,";
  37. if(chips%((h+1)*(v+1))!=0){
  38. cout<<"IMPOSSIBLE\n";
  39. for(int i=0;i<r;i++)row_c[i]=0;
  40. continue;
  41. }
  42. int per_row=chips/(h+1);
  43. int cur=0;
  44. bool poss=true;
  45. for(int i=0;i<r;i++){
  46. cur+=row_c[i];
  47. if(cur>per_row){
  48. poss=false;
  49. break;
  50. }
  51. else if(cur==per_row){
  52. r_cuts.push_back(i);
  53. cur=0;
  54. }
  55. }
  56. // cout<<r_cuts.size()<<"\n";
  57. if(poss==false){
  58. cout<<"IMPOSSIBLE\n";
  59. for(int i=0;i<r;i++)row_c[i]=0;
  60. r_cuts.erase(r_cuts.begin(),r_cuts.end());
  61. continue;
  62. }
  63. int per_col=per_row/(v+1);
  64. for(int i=0;i<c;i++){
  65. int index=0;
  66. for(int j=0;j<r;j++){
  67. if(str[j][i]=='@'){
  68. col_c[index]++;
  69. }
  70. if(j==r_cuts[index]){
  71. index++;
  72. }
  73. }
  74. bool same=true;
  75. for(int j=0;j<index;j++){
  76. if(col_c[j]<per_col){
  77. same=false;
  78. }
  79. if(col_c[j]>per_col){
  80. poss=false;
  81. break;
  82. }
  83. }
  84. if(same){
  85. for(int j=0;j<index;j++){
  86. col_c[j]=0;
  87. }
  88. }
  89. if(!poss){
  90. break;
  91. }
  92. }
  93. if(poss==true){
  94. cout<<"POSSIBLE\n";
  95. }
  96. else{
  97. cout<<"IMPOSSIBLE\n";
  98. }
  99. }
  100. return 0;
  101. }
  102. /*
  103. 6
  104. 3 6 1 1
  105. .@@..@
  106. .....@
  107. @.@.@@
  108. 4 3 1 1
  109. @@@
  110. @.@
  111. @.@
  112. @@@
  113. 4 5 1 1
  114. .....
  115. .....
  116. .....
  117. .....
  118. 4 4 1 1
  119. ..@@
  120. ..@@
  121. @@..
  122. @@..
  123. 3 4 2 2
  124. @.@@
  125. @@.@
  126. @.@@
  127. 3 4 1 2
  128. .@.@
  129. @.@.
  130. .@.@
  131. */
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
Case #1: POSSIBLE
Case #2: POSSIBLE