Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 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 = set<pair<int,P>>;
  14. //set vagy multiset.
  15. using SEARCHRES = set<pair<P,P>>;
  16. enum class LOGIC
  17. {
  18. EQ=0,
  19. BIGGER = 1,
  20. SLOWER =2
  21. };
  22. struct MAP
  23. {
  24. MATRIX mat;
  25. COORDINATES coords;
  26. COORDINATES negativecoords;
  27. SEARCHRES c;
  28. size_t N;
  29. size_t M;
  30. MAP(size_t n,size_t m)
  31. {
  32. N=n;
  33. M=m;
  34. mat = vector<vector<int>>(N);
  35. for (size_t i = 0 ; i < n ; ++i )
  36. {
  37. mat[i].resize(M);
  38. }
  39.  
  40. }
  41. bool isValidCoord(size_t p_i,size_t p_j)
  42. {
  43. return (p_i >= 0 && p_i < N && p_j >= 0 && p_j < M);
  44. }
  45. void print()
  46. {
  47. cout<<endl;
  48. for(auto vec : mat)
  49. {
  50. for(auto elem : vec)
  51. {
  52. cout<<elem<<" ";
  53. }
  54. cout<<endl;
  55. }
  56. cout<<endl;
  57. }
  58. void insert(size_t i, size_t j, int value)
  59. {
  60. if(i >= N || i < 0 || j >= M || j < 0)
  61. {
  62. cout<<"insert failed with ("<<i<<" "<<j<<") coordinates"<<endl;
  63. }
  64. else
  65. {
  66.  
  67. mat[i][j]=value;
  68. if(value>0)
  69. {
  70. coords.insert(pair<int,P>(value,P(i,j)));
  71. }
  72. else if(value!=0)
  73. {
  74. negativecoords.insert(pair<int,P>(value,P(i,j)));
  75. }
  76.  
  77. }
  78. }
  79. size_t getDistance(size_t x,size_t y, size_t o_x,size_t o_y)
  80. {
  81. int fst = o_x-x;
  82. int snd = o_y-y;
  83. return abs(fst)+abs(snd);
  84. }
  85. int calculate()
  86. {
  87. int sum=0;
  88. for(size_t i=0; i<N; ++i)
  89. {
  90. for(size_t j=0; j<M; ++j)
  91. {
  92. int elem=mat[i][j];
  93. if(elem>0)
  94. {
  95. //itt kell okosítani mehet sorba de a legoptimálisabbat találja meg majd a push negative.
  96.  
  97. while(mat[i][j])
  98. {
  99. sum+=pushToNegative(i,j,mat[i][j]);
  100. }
  101. }
  102. }
  103.  
  104. }
  105. return sum;
  106. }
  107. int pushToNegative(size_t p_i,size_t p_j,int value)
  108. {
  109. size_t steps=0;
  110. for(size_t i=0; i<N; ++i)
  111. {
  112.  
  113. for(size_t j=0; j<M; ++j)
  114. {
  115. if(p_i==i &&p_j==j)
  116. {
  117. continue;
  118. }
  119.  
  120. int elem=mat[i][j];
  121. int absol=abs(mat[i][j]);
  122.  
  123. if(elem<0)
  124. {
  125. if(absol==value)
  126. {
  127. steps=absol*getDistance(i,j,p_i,p_j);
  128. mat[i][j]=0;
  129. mat[p_i][p_j]=0;
  130. }
  131. else if(absol>value)
  132. {
  133. mat[p_i][p_j]=0;
  134. mat[i][j]=elem+value;
  135. steps=value*getDistance(i,j,p_i,p_j);
  136. }
  137. else
  138. {
  139. mat[p_i][p_j]=value+elem;
  140. mat[i][j]=0;
  141. steps=absol*getDistance(i,j,p_i,p_j);
  142. }
  143. return steps;
  144.  
  145. }
  146. }
  147.  
  148. }
  149. }
  150. //search
  151. void search(size_t& p_i,size_t& p_j,int elem)
  152. {
  153. //?bool
  154. search1(p_i,p_j,elem);
  155. if(c.size()!=0)
  156. {
  157. return;
  158. }
  159. return;
  160. }
  161.  
  162. void search1(size_t& p_i,size_t& p_j,int elem)
  163. {
  164. //jobbra
  165. if(isValidCoord(p_i+1,p_j) && mat[p_i+1][p_j]<0)
  166. {
  167. int value = mat[p_i+1][p_j];
  168. int absol = abs(value);
  169. if(absol>=elem)
  170. {
  171. c.insert(pair<P,P>(P(1,elem),P(p_i+1,p_j)));
  172. }
  173. }
  174. ////balra
  175. if(isValidCoord(p_i-1,p_j) && mat[p_i-1][p_j]<0)
  176. {
  177. int value = mat[p_i-1][p_j];
  178. int absol = abs(value);
  179. if(absol>=elem)
  180. {
  181. c.insert(pair<P,P>(P(1,elem),P(p_i-1,p_j)));
  182. }
  183. }
  184. //fel
  185. if(isValidCoord(p_i,p_j-1) && mat[p_i][p_j-1]<0)
  186. {
  187. int value = mat[p_i][p_j-1];
  188. int absol = abs(value);
  189. if(absol>=elem)
  190. {
  191. c.insert(pair<P,P>(P(1,elem),P(p_i,p_j-1)));
  192. }
  193.  
  194. }
  195. //le
  196. if(isValidCoord(p_i,p_j+1) && mat[p_i][p_j+1]<0)
  197. {
  198.  
  199. int value = mat[p_i][p_j+1];
  200. int absol = abs(value);
  201. if(absol>=elem)
  202. {
  203. c.insert(pair<P,P>(P(1,elem),P(p_i,p_j+1)));
  204. }
  205. }
  206.  
  207. }
  208.  
  209. int searchMinimums()
  210. {
  211. int sum=0;
  212. for(size_t i=0; i<N; ++i)
  213. {
  214. for(size_t j=0; j<M; ++j)
  215. {
  216. int elem=mat[i][j];
  217. if(elem>0)
  218. {
  219. search(i,j,elem);
  220. }
  221. }
  222.  
  223. }
  224. return sum;
  225. }
  226. };
  227.  
  228.  
  229. int main()
  230. {
  231. auto start = std::chrono::system_clock::now();
  232. int n,m;
  233. cin>>n;
  234. cin>>m;
  235. MAP ma(n,m);
  236. for(int i=0;i<n;++i)
  237. {
  238. for(int j=0;j<m;++j)
  239. {
  240. int value;
  241. cin>>value;
  242. ma.insert(i,j,value);
  243. }
  244. }
  245. cout<<endl;
  246.  
  247. for(auto p : ma.c)
  248. {
  249. cout<<p.first.first<<endl;
  250. }
  251. ma.searchMinimums();
  252. cout<<endl;
  253. ma.print();
  254. //cout<<ma.calculate();
  255.  
  256. /*cout<<endl;
  257. for(auto p : ma.coords)
  258. {
  259. cout<<p.first<<endl;
  260. }
  261. cout<<endl;
  262. for(auto p : ma.negativecoords)
  263. {
  264. cout<<p.first<<endl;
  265. }
  266. */
  267. cout<<endl;
  268.  
  269. for(auto p : ma.c)
  270. {
  271. cout<<p.first.first<<" "<< p.first.second<<" " << p.second.first<<" "<<p.second.second<<endl;
  272. }
  273.  
  274. auto end = std::chrono::system_clock::now();
  275.  
  276. std::chrono::duration<double> elapsed_seconds = end-start;
  277. std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  278.  
  279. std::cout << "elapsed time: " << elapsed_seconds.count() <<endl;
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement