Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include<vector>
  2. #include<iostream>
  3. #include<utility>
  4. #include<math.h>
  5. #include<set>
  6. #include<memory>
  7. #include<chrono>
  8. #include <ctime>
  9. using namespace std;
  10.  
  11. using MATRIX = vector<vector<int>>;
  12. using P = pair<size_t,size_t>;
  13. using COORDINATES = multiset<pair<int,P>>;
  14. //set vagy multiset.
  15. using SEARCHRES = set<pair<P,P>>;
  16. enum class DIRECTION {LEFT,RIGHT,UP,DOWN,NONE};
  17. static int steps=0;
  18. void makesums(MATRIX& mat ,vector<int>& column,vector<int>& row,int n,int m)
  19. {
  20. for(int i=0;i<n;++i)
  21. {
  22. int sum=0;
  23. for(int j=0;j<m;++j)
  24. {
  25. sum+=mat[i][j];
  26.  
  27. }
  28. row[i]=sum;
  29. }
  30. for(int i=0;i<m;++i)
  31. {
  32. int sum=0;
  33. for(int j=0;j<n;++j)
  34. {
  35. sum+=mat[j][i];
  36.  
  37. }
  38. column[i]=sum;
  39. }
  40. }
  41. void move(int i,int j,MATRIX& mat ,vector<int>& column,vector<int>& row,int n,int m)
  42. {
  43. //nem biztos hogy kell a localcopy ha paraméternek jön...
  44. vector<int> actrow=row;
  45. vector<int> actcol=column;
  46. actrow[i]-=mat[i][j];
  47. actcol[j]-=mat[i][j];
  48. DIRECTION dir=DIRECTION::NONE;
  49. int sumleft=0;
  50. for(int l=0;l<i;++l)
  51. {
  52. sumleft+=actrow[l];
  53. }
  54.  
  55. if(sumleft<0)
  56. {
  57. dir=DIRECTION::LEFT;
  58. }
  59.  
  60. int sumright=0;
  61.  
  62. for(int r=i+1;r<m ;++r)
  63. {
  64. sumright+=actrow[r];
  65. }
  66.  
  67. if(sumright<sumleft)
  68. {
  69. dir=DIRECTION::RIGHT;
  70. }
  71.  
  72.  
  73. int sumup=0;
  74. for(int u=0;u<j;++u)
  75. {
  76. sumup+=actcol[u];
  77. }
  78.  
  79. if(sumup<sumright)
  80. {
  81. dir=DIRECTION::UP;
  82. }
  83.  
  84. int sumdown=0;
  85. for(int d=j+1;d<n;++d)
  86. {
  87. sumdown+=actcol[d];
  88. }
  89.  
  90. if(sumdown<sumup)
  91. {
  92. dir=DIRECTION::DOWN;
  93. }
  94. /*
  95. cout<<endl;
  96. cout<<sumleft<<endl;
  97. cout<<sumright<<endl;
  98. cout<<sumup<<endl;
  99. cout<<sumdown<<endl;
  100. cout<<endl;
  101. */
  102.  
  103. switch (dir)
  104. {
  105. case DIRECTION::LEFT :
  106.  
  107. mat[i][j]-=1;
  108. //mat[i][j-1]+=1000;
  109. ++steps;
  110. break;
  111. case DIRECTION::RIGHT :
  112.  
  113. mat[i][j]-=1;
  114. //mat[i][j+1]+=1000;
  115. ++steps;
  116. break;
  117. case DIRECTION::UP :
  118. mat[i][j]-=1;
  119. mat[i-1][j]+=1;
  120. ++steps;
  121. break;
  122. case DIRECTION::DOWN :
  123. mat[i][j]-=1;
  124. //mat[i][j]+=1000;
  125. ++steps;
  126. break;
  127. case DIRECTION::NONE :
  128. break;
  129. }
  130. /*
  131. cout<<sumleft<<endl;
  132. cout<<sumright<<endl;
  133. cout<<sumup<<endl;
  134. cout<<sumdown<<endl;
  135. cout<<endl;
  136. */
  137.  
  138. }
  139. int main()
  140. {
  141. auto start = std::chrono::system_clock::now();
  142. int n,m;
  143. cin>>n;
  144. cin>>m;
  145. MATRIX mat=vector<vector<int>>(m);
  146. vector<int> column=vector<int>(n);
  147. vector<int> row=vector<int>(m);
  148. for(int i=0;i<m;++i)
  149. {
  150. mat[i].resize(n);
  151. for(int j=0;j<m;++j)
  152. {
  153.  
  154. int value;
  155. cin>>value;
  156. mat[i][j]=value;
  157. }
  158. cout<<endl;
  159. }
  160. makesums(mat, column, row,n,m);
  161.  
  162.  
  163. for(int i=0;i<n;++i)
  164. {
  165. for(int j=0;j<m;++j)
  166. {
  167. if(mat[i][j]>0)
  168. {
  169. move(i,j,mat, column, row,n,m);
  170. }
  171.  
  172. }
  173. }
  174.  
  175.  
  176.  
  177. for(int i=0;i<n;++i)
  178. {
  179. for(int j=0;j<m;++j)
  180. {
  181. cout<<mat[i][j]<<" ";
  182. }
  183. cout<<endl;
  184. }
  185.  
  186.  
  187. /*
  188. for(int i=0;i<n;++i)
  189. {
  190. cout<<" "<<(row[i])<<endl;
  191. }
  192. cout<<endl;
  193. for(int i=0;i<m;++i)
  194. {
  195. cout<<(column[i])<<" ";
  196. }
  197. */
  198. cout<<steps;
  199. auto end = std::chrono::system_clock::now();
  200.  
  201. std::chrono::duration<double> elapsed_seconds = end-start;
  202. std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  203.  
  204. //std::cout << "elapsed time: " << elapsed_seconds.count() <<endl;
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement