fork(6) download
  1. #include<iostream>
  2. #include<stdio.h>
  3. //#include<conio.h>
  4. using namespace std;
  5. int r1,r2,c1,c2,m,n;
  6. long long int A[50][50],C[50],R[50],bob,alice;
  7. void game_alice();
  8. void game_bob();
  9. void game_bob()
  10. {
  11. //cout<<"\nInside bob bob="<<bob<<" alice="<<alice;
  12. int option,i;
  13. if(r1>r2 || c1>c2)
  14. return;
  15. else
  16. {
  17. if(R[r1]>=R[r2] && R[r1]>=C[c1] && R[r1]>=C[c2])
  18. option=1;
  19. else if(R[r2]>R[r1] && R[r2]>=C[c1] && R[r2]>=C[c2])
  20. option=2;
  21. else if(C[c1]>R[r1] && C[c1]>R[r2] && C[c1]>=C[c2])
  22. option=3;
  23. else if(C[c2]>R[r1] && C[c2]>R[r2] && C[c2]>C[c2])
  24. option=4;
  25. }
  26.  
  27. if(option==1)
  28. {
  29. bob+=R[r1]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  30.  
  31. for(i=c1;i<=c2;++i)
  32. C[i]-=A[r1][i];
  33.  
  34. ++r1;
  35. game_alice();
  36. }
  37. else if(option==2)
  38. {
  39. bob+=R[r2]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  40.  
  41. for(i=c1;i<=c2;++i)
  42. C[i]-=A[r2][i];
  43.  
  44. --r2;
  45. game_alice();
  46. }
  47. else if(option==3)
  48. {
  49. bob+=C[c1]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  50.  
  51. for(i=r1;i<=r2;++i)
  52. R[i]-=A[i][c1];
  53.  
  54. ++c1;
  55. game_alice();
  56. }
  57. else if(option==4)
  58. {
  59. bob+=C[c2]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  60.  
  61. for(i=r1;i<=r2;++i)
  62. R[i]-=A[i][c2];
  63.  
  64. --c2;
  65. game_alice();
  66. }
  67. }
  68. void game_alice()
  69. {
  70.  
  71. //cout<<"\nInside alice bob="<<bob<<" alice="<<alice;
  72. int option,i;
  73. if(r1>r2 || c1>c2)
  74. return;
  75. else
  76. {
  77. if(R[r1]<=R[r2] && R[r1]<=C[c1] && R[r1]<=C[c2])
  78. option=1;
  79. else if(R[r2]<R[r1] && R[r2]<=C[c1] && R[r2]<=C[c2])
  80. option=2;
  81. else if(C[c1]<R[r1] && C[c1]<R[r2] && C[c1]<=C[c2])
  82. option=3;
  83. else if(C[c2]<R[r1] && C[c2]<R[r2] && C[c2]<C[c1])
  84. option=4;
  85. }
  86.  
  87. if(option==1)
  88. {
  89. alice+=R[r1]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  90.  
  91. for(i=c1;i<=c2;++i)
  92. C[i]-=A[r1][i];
  93.  
  94. ++r1;
  95. game_bob();
  96. }
  97. else if(option==2)
  98. {
  99. alice+=R[r2]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  100.  
  101. for(i=c1;i<=c2;++i)
  102. C[i]-=A[r2][i];
  103.  
  104. --r2;
  105. game_bob();
  106. }
  107. else if(option==3)
  108. {
  109. alice+=C[c1]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  110.  
  111. for(i=r1;i<=r2;++i)
  112. R[i]-=A[i][c1];
  113.  
  114. ++c1;
  115. game_bob();
  116. }
  117. else if(option==4)
  118. {
  119. alice+=C[c2]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
  120.  
  121. for(i=r1;i<=r2;++i)
  122. R[i]-=A[i][c2];
  123.  
  124. --c2;
  125. game_bob();
  126. }
  127.  
  128. }
  129. int main()
  130. {
  131. int t;
  132. scanf("%d",&t);
  133. while(t--)
  134. {
  135. //scanf("%d%d",&m,&n);
  136. cin>>m>>n;
  137. int i,j;
  138. r1=0;r2=m-1;
  139. c1=0;c2=n-1;
  140. bob=0;alice=0;
  141. for(i=0;i<50;++i)
  142. R[i]=0;
  143. for(j=0;j<50;++j)
  144. C[j]=0;
  145.  
  146. for(i=0;i<m;++i)
  147. for(j=0;j<n;++j)
  148. {
  149. cin>>A[i][j];
  150. R[i]+=A[i][j];
  151. C[j]+=A[i][j];
  152. }
  153.  
  154.  
  155. //cout<<"\nalice="<<alice<<"\nbob="<<bob;
  156. game_alice();
  157. //cout<<"\nalice="<<alice<<"\nbob="<<bob;
  158. if(alice==bob)
  159. cout<<alice+bob<<endl;
  160. else if(alice>bob)
  161. cout<<alice<<endl;
  162. else cout<<bob<<endl;
  163.  
  164.  
  165. }
  166. }
Success #stdin #stdout 0.02s 2752KB
stdin
1
1 2
1 1
stdout
2