fork(11) download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19. using namespace std;
  20. typedef long long LL;
  21. typedef double D;
  22. #define PI 3.14159265358979323846264338327950288
  23. #define FI first
  24. #define SE second
  25. #define MP make_pair
  26. #define PB push_back
  27. #define F(I,A,B) for(int I=A;I<B;I++)
  28. #define FN(I,A,B) for(int I=A;I>B;I--)
  29. #define S(V) sort(V.begin(), V.end())
  30. #define CO(A) cout << A << endl
  31. #define MAX 52
  32. int p[1001][1001];
  33. vector < pair<int,int> > ptc;
  34. pair<int,int> np;
  35. int w,h,cx,cy;
  36. bool found ;
  37. long k,marked,stones = 0;
  38. int main(){
  39. F(i,0,1001) F(j,0,1001) p[i][j] = 0;
  40. int t, cc = 1;
  41. cin >> t;
  42. while (cc <=t) {
  43. cout << "Case #" << cc << ": " ;
  44. cc++;
  45. cin >> w>>h>>k;
  46. ptc.clear();
  47. if(w>h) swap(w,h);
  48. if(w<=2){
  49. cout << k << endl;
  50. continue;
  51. }
  52. F(i,0,w) F(j,0,h) p[i][j] = 0;
  53. int angle = 0;
  54. cx = w/2; cy = h/2;
  55. p[cx][cy] = 1;
  56. marked = 1;
  57. stones = 1;
  58. //cout << cx << "," << cy << endl;
  59. while (marked<k) {
  60. if (angle==4) {
  61. if(cx!=0 && cx!=w-1 && cy!=0 && cy!=h-1){
  62. stones--;
  63. p[cx][cy] = 2;
  64. }
  65. found = false;
  66. F(i,0,ptc.size()){
  67. np = ptc[i];
  68. cx = np.FI; cy = np.SE;
  69. if(cx!=0 && cx!=w-1 && cy!=0 && cy!=h-1){
  70. found = true;
  71. ptc.erase(ptc.begin() + i);
  72. break;
  73. }
  74. }
  75. if(!found){
  76. np = ptc[0];
  77. ptc.erase(ptc.begin()+0);
  78. cx = np.FI; cy = np.SE;
  79. }
  80. angle = 0;
  81. //cout << cx << "," << cy << endl;
  82. }
  83. switch (angle) {
  84. case 0:
  85. if (cx<w-1 && p[cx+1][cy]==0) {
  86. ptc.PB(MP(cx+1, cy));
  87. p[cx+1][cy] = 1;
  88. stones++;
  89. marked++;
  90. }
  91. angle = 1;
  92. break;
  93. case 1:
  94. if (cy<h-1 && p[cx][cy+1]==0) {
  95. ptc.PB(MP(cx, cy+1));
  96. p[cx][cy+1] = 1;
  97. stones++;
  98. marked++;
  99. }
  100. angle = 2;
  101. break;
  102. case 2:
  103. if (cx>0 && p[cx-1][cy]==0) {
  104. ptc.PB(MP(cx-1, cy));
  105. p[cx-1][cy] = 1;
  106. stones++;
  107. marked++;
  108. }
  109. angle = 3;
  110. break;
  111. case 3:
  112. if (cy>0 && p[cx][cy-1]==0) {
  113. ptc.PB(MP(cx, cy-1));
  114. p[cx][cy-1] = 1;
  115. stones++;
  116. marked++;
  117. }
  118. angle = 4;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. if(cx!=0 && cx!=w-1 && cy!=0 && cy!=h-1 ){
  125. if(p[cx][cy-1]!=0 && p[cx][cy+1]!=0 && p[cx-1][cy]!=0
  126. && p[cx+1][cy]!=0)
  127. stones--;
  128. }
  129. cout << stones << endl;
  130. }
  131. return 0;
  132. }
  133.  
  134.  
  135.  
  136.  
Success #stdin #stdout 0s 7256KB
stdin
Standard input is empty
stdout
Standard output is empty