Advertisement
a53

Culegere

a53
Jul 10th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <list>
  4. #include <cassert>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <iostream>
  10. #include <map>
  11. #include <set>
  12. #include <stack>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include <queue>
  17.  
  18. using namespace std;
  19.  
  20. int GetSpaces(char *str) {
  21. int lg, i, ans = 0;
  22. lg = strlen(str);
  23. for (i = 0; i < lg; ++i) {
  24. ans += (str[i] == ' ');
  25. }
  26. return ans;
  27. }
  28.  
  29. bool HasEnter(char *str) {
  30. int lg = strlen(str);
  31. return (str[lg - 1] == '\n');
  32. }
  33.  
  34. bool OnlyNumeric(char *str) {
  35. int lg = strlen(str), i;
  36. for (i = 0; i < lg; ++i) {
  37. if (str[i] != ' ' && str[i] != '\n') {
  38. if (!isdigit(str[i])) {
  39. return false;
  40. }
  41. }
  42. }
  43. return true;
  44. }
  45.  
  46. struct Event {
  47. int x, y, code;
  48.  
  49. Event() {
  50. x = y = code = 0;
  51. }
  52.  
  53. Event(int xx, int yy, int cc) {
  54. x = xx;
  55. y = yy;
  56. code = cc;
  57. }
  58. };
  59.  
  60. #define BUFFERSIZE 500
  61. #define MAX_TIME 500000
  62.  
  63. int N, M, K, E;
  64. char buffer[BUFFERSIZE];
  65.  
  66. vector <Event> events;
  67. vector <vector <int> > mymat, workmat;
  68. vector <pair <int, int> > starttimes, endtimes;
  69. vector <pair <int, int> > directions = {{0, 0}, {-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  70.  
  71. pair <int, int> operator+(pair <int, int> a, pair <int, int> b) {
  72. pair <int, int> ans;
  73. ans.first = a.first + b.first;
  74. ans.second = a.second + b.second;
  75. return ans;
  76. }
  77.  
  78. void Normalize(pair <int, int> &a) {
  79. if (a.first == 0) a.first = N;
  80. if (a.first == N + 1) a.first = 1;
  81. if (a.second == 0) a.second = M;
  82. if (a.second == M + 1) a.second = 1;
  83. }
  84.  
  85. void WriteMat() {
  86. cout << string(5, '\n');
  87. int i, j;
  88. for (i = 1; i <= 2; ++i) {
  89. for (j = 1; j <= M; ++j) {
  90. if (mymat[i][j] == 0) {
  91. if (workmat[i][j] != 0) {
  92. putchar('0' + workmat[i][j]);
  93. }
  94. else {
  95. putchar('.');
  96. }
  97. }
  98. else {
  99. putchar('O');
  100. }
  101. }
  102. putchar('\n');
  103. }
  104. }
  105.  
  106. int main() {
  107. freopen("culegere.in", "r", stdin);
  108. freopen("culegere.out", "w", stdout);
  109.  
  110. int i;
  111.  
  112. scanf("%d %d %d %d", &N, &M, &K, &E);
  113.  
  114. assert(K < M);
  115.  
  116. events.resize(E);
  117. starttimes.resize(E);
  118. endtimes.resize(E);
  119. mymat.resize(1 + N, vector <int> (1 + M));
  120. workmat.resize(1 + N, vector <int> (1 + M));
  121.  
  122. for (i = 0; i < E; ++i) {
  123.  
  124. char t;
  125. cin >> t;
  126. assert(t == '1' || t == '2');
  127. if (t == '1') {
  128. int x, y, st, en;
  129. scanf("%d %d %d %d", &x, &y, &st, &en);
  130. events[i] = Event(x, y, 5);
  131. starttimes[i] = make_pair(st, i);
  132. endtimes[i] = make_pair(en, i);
  133. assert(en <= MAX_TIME);
  134. }
  135. else {
  136. int x, y, st, en, code;
  137. scanf("%d %d %d %d %d", &x, &y, &st, &en, &code);
  138. events[i] = Event(x, y, code);
  139. starttimes[i] = make_pair(st, i);
  140. endtimes[i] = make_pair(en, i);
  141. assert(en <= MAX_TIME);
  142. }
  143. }
  144.  
  145. sort(starttimes.begin(), starttimes.end());
  146. sort(endtimes.begin(), endtimes.end());
  147.  
  148. for (i = 1; i <= K; ++i) {
  149. mymat[1][i] = 2;
  150. }
  151.  
  152. pair <int, int> tail, head;
  153. int steptail, stephead, timenow = 0, cnt = 0;
  154. bool apple, blocked = false;
  155. tail = make_pair(1, 1);
  156. head = make_pair(1, K);
  157. steptail = 2;
  158. stephead = 2;
  159.  
  160. int sti, eni;
  161. sti = eni = 0;
  162. while ((sti < E || eni < E) && cnt < E) {
  163. ++timenow;
  164.  
  165. apple = false;
  166. while(eni < E && endtimes[eni].first == timenow) {
  167. int pos = endtimes[eni].second;
  168. cnt += workmat[events[pos].x][events[pos].y] != 0;
  169. workmat[events[pos].x][events[pos].y] = 0;
  170. ++eni;
  171. }
  172.  
  173. while(sti < E && starttimes[sti].first == timenow) {
  174. int pos = starttimes[sti].second;
  175. assert(workmat[events[pos].x][events[pos].y] == 0);
  176. assert(mymat[events[pos].x][events[pos].y] == 0);
  177. workmat[events[pos].x][events[pos].y] = events[pos].code;
  178. ++sti;
  179. }
  180.  
  181. head = head + directions[stephead];
  182. Normalize(head);
  183. if (mymat[head.first][head.second] != 0 && head != tail) {
  184. blocked = true;
  185. break;
  186. }
  187. if (workmat[head.first][head.second] != 0) {
  188. int code;
  189. code = workmat[head.first][head.second];
  190. if (code == 5) {
  191. apple = true;
  192. workmat[head.first][head.second] = 0;
  193. ++cnt;
  194. }
  195. else {
  196. assert(abs(stephead - code) != 2);
  197. stephead = code;
  198. }
  199. }
  200. mymat[head.first][head.second] = stephead;
  201.  
  202. if (!apple) {
  203. if (tail != head) {
  204. mymat[tail.first][tail.second] = 0;
  205. }
  206. tail = tail + directions[steptail];
  207. Normalize(tail);
  208. }
  209. steptail = mymat[tail.first][tail.second];
  210. }
  211.  
  212.  
  213. if (blocked) {
  214. printf("BLOCAT\n");
  215. }
  216. else {
  217. printf("LIBER\n");
  218. }
  219.  
  220. pair <int, int> lastpos = tail;
  221. while (mymat[tail.first][tail.second] < 5 && mymat[tail.first][tail.second] > 0) {
  222. steptail = mymat[tail.first][tail.second];
  223. mymat[tail.first][tail.second] = 'O';
  224. lastpos = tail;
  225. tail = tail + directions[steptail];
  226. Normalize(tail);
  227. }
  228. mymat[lastpos.first][lastpos.second] = '#';
  229.  
  230. int j;
  231. for (i = 1; i <= N; ++i) {
  232. for (j = 1; j <= M; ++j) {
  233. if (mymat[i][j] == 0) {
  234. mymat[i][j] = '.';
  235. }
  236. putchar(mymat[i][j]);
  237. }
  238. putchar('\n');
  239. }
  240.  
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement