fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. /* وتزودوا فإن خير الزاد التقوى */
  3. /* وما تدري نفس ماذا تکسب غدا وما تدري نفس باي ارض تموت */
  4. #include <bits/stdc++.h>
  5. #include<unordered_map>
  6. #include<limits>
  7. #include<iomanip>
  8. #include<fstream>
  9. using namespace std;
  10. #define endl '\n'
  11. #define ll long long
  12. #define ull unsigned long long
  13. #define dll double
  14. #define PI acos(-1)
  15. #define watch(x) cerr<<#x<<" = "<<x<<" \n"
  16. #define sz(n) n.size()
  17. #define M_Sayed fast()
  18. #define all(v) v.begin(),v.end()
  19. #define allr(v) v.rbegin(),v.rend()
  20. #define RT(v) return cout<<v,0
  21. #define MP make_pair
  22. int dx[] = { 0, -1, 0, -1, 1, 1, -1, -1,0};
  23. int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1,0};
  24. const double EPS = (1e-7);
  25. const ll mod = (1e9 + 7), OO = 1e9,N=1e5+5;
  26. ll Ceil(ll x, ll y)
  27. {
  28. return (x + y - 1) / y;
  29. }
  30. void run()
  31. {
  32. #ifndef ONLINE_JUDGE
  33. freopen("input.txt", "r", stdin);
  34. //freopen("output.txt", "w", stdout);
  35. #else
  36. //freopen("input", "r", stdin);
  37. //freopen("output", "w", stdout);
  38. #endif
  39. }
  40.  
  41. void fast()
  42. {
  43. ios::sync_with_stdio(false);
  44. cin.tie(NULL);
  45. cout.tie(NULL);
  46. }
  47. ll n,m,test=1,viss,k,x;
  48. int gr[10][10],co[10][10];
  49.  
  50. void left()
  51. {
  52. n=4;
  53. for(int i=0; i<n; i++)
  54. {
  55. int ind=0;
  56. for(int j=0; j<n; j++)
  57. {
  58. if(gr[i][j])
  59. {
  60. co[i][ind++]=gr[i][j];
  61. }
  62. }
  63. }
  64. for(int i=0; i<n; i++)for(int j=0; j<n; j++)gr[i][j]=co[i][j];
  65. for(int i=0; i<n; i++)
  66. {
  67. for(int j=0; j<n; j++)
  68. {
  69. if(gr[i][j]==gr[i][j+1])
  70. {
  71. gr[i][j]+=gr[i][j];
  72. gr[i][j+1]=0;
  73. j++;
  74. }
  75. }
  76. }
  77. ;
  78. memset(co,0,sizeof co);
  79. for(int i=0; i<n; i++)
  80. {
  81. int last=gr[i][0];
  82. int ind=0;
  83. for(int j=0; j<n; j++)
  84. {
  85. if(gr[i][j])
  86. {
  87. co[i][ind++]=gr[i][j];
  88. }
  89. }
  90. }
  91. for(int i=0; i<4; i++)
  92. {
  93. for(int j=0; j<4; j++)
  94. {
  95. cout<<co[i][j]<<" ";
  96. }
  97. cout<<endl;
  98. }
  99. }
  100. void pr()
  101. {
  102. for(int i=0; i<4; i++)
  103. {
  104. for(int j=0; j<4; j++)
  105. {
  106. cout<<co[i][j]<<" ";
  107. }
  108. cout<<endl;
  109. }
  110. }
  111. void right()
  112. {
  113. n=4;
  114. for(int i=0; i<n; i++)
  115. {
  116. int ind=3;
  117. for(int j=3; j>=0; j--)
  118. {
  119. if(gr[i][j])
  120. {
  121. co[i][ind--]=gr[i][j];
  122. }
  123. }
  124. }
  125. for(int i=0; i<n; i++)for(int j=0; j<n; j++)gr[i][j]=co[i][j];
  126. for(int i=0; i<n; i++)
  127. {
  128. for(int j=3; j>=0; j--)
  129. {
  130. if(j&&gr[i][j]==gr[i][j-1])
  131. {
  132. gr[i][j]+=gr[i][j];
  133. gr[i][j-1]=0;
  134. j--;
  135. }
  136. }
  137. }
  138. ;
  139. memset(co,0,sizeof co);
  140. for(int i=0; i<n; i++)
  141. {
  142. int last=gr[i][0];
  143. int ind=3;
  144. for(int j=3; j>=0; j--)
  145. {
  146. if(gr[i][j])
  147. {
  148. co[i][ind--]=gr[i][j];
  149. }
  150. }
  151. }
  152. for(int i=0; i<4; i++)
  153. {
  154. for(int j=0; j<4; j++)
  155. {
  156. cout<<co[i][j]<<" ";
  157. }
  158. cout<<endl;
  159. }
  160.  
  161. }
  162. void up()
  163. {
  164. n=4;
  165. for(int j=3; j>=0; j--)
  166. {
  167. int ind=3;
  168. for(int i=3; i>=0; i--)
  169. {
  170. if(gr[i][j])
  171. {
  172. co[ind--][j]=gr[i][j];
  173. }
  174. }
  175. }
  176. for(int i=0; i<n; i++)for(int j=0; j<n; j++)gr[i][j]=co[i][j];
  177. //pr();
  178. for(int j=3; j>=0; j--)
  179. for(int i=3; i>=0; i--)
  180. {
  181. {
  182. if(j&&gr[j][i]==gr[j-1][i])
  183. {
  184. gr[j][i]+=gr[j][i];
  185. gr[j-1][i]=0;
  186. j--;
  187. }
  188. }
  189. }
  190. memset(co,0,sizeof co);
  191. for(int j=0; j<n; j++)
  192. {
  193. int ind=3;
  194. for(int i=3; i>=0; i--)
  195. {
  196. if(gr[i][j])
  197. {
  198. co[ind--][j]=gr[i][j];
  199. }
  200. }
  201. }
  202. pr();
  203.  
  204. }
  205. void dw()
  206. {
  207. n=4;
  208. for(int j=3; j>=0; j--)
  209. {
  210. int ind=0;
  211. for(int i=0; i<n; i++)
  212. {
  213. if(gr[i][j])
  214. {
  215. co[ind++][j]=gr[i][j];
  216. }
  217. }
  218. }
  219. for(int i=0; i<n; i++)for(int j=0; j<n; j++)gr[i][j]=co[i][j];
  220. //pr();
  221. for(int j=0; j<n; j++)
  222. for(int i=0; i<n; i++)
  223. {
  224. {
  225. if(gr[j][i]==gr[j+1][i])
  226. {
  227. gr[j][i]+=gr[j][i];
  228. gr[j+1][i]=0;
  229. j++;
  230. }
  231. }
  232. }
  233. memset(co,0,sizeof co);
  234. for(int j=0; j<n; j++)
  235. {
  236. int ind=0;
  237. for(int i=0; i<n; i++)
  238. {
  239. if(gr[i][j])
  240. {
  241. co[ind++][j]=gr[i][j];
  242. }
  243. }
  244. }
  245. pr();
  246.  
  247. }
  248. int main()
  249. {
  250. M_Sayed;
  251. run();
  252. //freopen("task.in", "r", stdin);
  253. for(int i=0; i<4; i++)
  254. {
  255. for(int j=0; j<4; j++)
  256. {
  257. cin>>gr[i][j];
  258. }
  259. }
  260. cin>>m;
  261. n=4;
  262. if(m==0)left();
  263. if(m==2)right();
  264. if(m==3)up();
  265. if(m==1)dw();
  266.  
  267.  
  268. }
  269.  
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0