Advertisement
Dalacul

Untitled

Apr 9th, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1. include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #define INF 0x7FFFFFFF
  6. #define cINF 0x7F
  7.  
  8. //Left child, Right sibling
  9. typedef struct MultiWayIntTree {
  10. int value, nrChild, alpha, beta;
  11. struct MultiWayIntTree *left, *right, *parent;
  12. } mw_ITree;
  13.  
  14. typedef struct MultiWayIntTreeQueue {
  15. mw_ITree *tree;
  16. struct MultiWayIntTreeQueue *next;
  17. } mw_ITQueue;
  18.  
  19. typedef struct MultiWayMatrixTree {
  20. char matrix[20][20];
  21. struct MultiWayMatrixTree *left, *right;
  22. } mw_MTree;
  23.  
  24. bool same(char *string1, char *string2)
  25. {
  26. return !strcmp(string1, string2);
  27. }
  28.  
  29. int Minimax(mw_ITree *tree, bool mM);
  30.  
  31. void freeIT(mw_ITree *node)
  32. {
  33. if(node == NULL) {
  34. return;
  35. }
  36. mw_ITree *p, *p_next;
  37. p = node->left;
  38. while(p != NULL) {
  39. p_next = p->right;
  40. freeIT(p);
  41. p = p_next;
  42. }
  43. free(node);
  44. }
  45.  
  46. void insert_MTree(mw_MTree *parent, mw_MTree *child)
  47. {
  48. child->left = NULL; //copilul nu are alti copii... e prea mic
  49. child->right = NULL;//copilul inca nu stie ca are si alti frati
  50. if(parent->left == NULL) {
  51. parent->left = child;
  52. return;
  53. }
  54. child->right = parent->left;
  55. parent->left = child;
  56. }
  57.  
  58. char RB_to_nr(char RB)
  59. {
  60. //Blue = 0, Red = 1
  61. if(RB == 'B')
  62. return 0;
  63. if(RB == 'R')
  64. return 1;
  65. //RB == '-'
  66. return 8;
  67. }
  68.  
  69. char nr_to_RB(char nr)
  70. {
  71. if(nr == 1)
  72. return 'R';
  73. if(nr == 0)
  74. return 'B';
  75. //nr == 8;
  76. return '-';
  77. }
  78.  
  79. void printTabs(FILE *fout, int tabs_count)
  80. {
  81. int i;
  82. for(i = 0; i < tabs_count; ++i)
  83. fprintf(fout, "\t");
  84. }
  85.  
  86. void printMatrix(FILE *fout, int N, int M, char matrix[20][20], int tabs_count)
  87. {
  88. int i, j;
  89. int M1 = M - 1;
  90. for(i = 0; i < N; ++i) {
  91. printTabs(fout, tabs_count);
  92. for(j = 0; j < M1; ++j) {
  93. fprintf(fout, "%c ", nr_to_RB(matrix[i][j]));
  94. }
  95. fprintf(fout, "%c", nr_to_RB(matrix[i][M1]));
  96. fprintf(fout, "\n");
  97. }
  98. fprintf(fout, "\n");
  99. }
  100.  
  101. /*void printTree(FILE *fout, mw_ITree *tree, int tabs_count)
  102. {
  103. if(tree == NULL)
  104. return;
  105. mw_ITree *p;
  106. for(p = tree->left; p != NULL; p = p->right) {
  107. if(p->value == INF || p->value == -INF)
  108. //fprintf(fout, "inf | ");
  109. continue;
  110. printTabs(fout, tabs_count);
  111. //TODO: JUST THE p->value!!!
  112. fprintf(fout, "%d\n", p->value);
  113. /*
  114. if(p->alpha == -INF)
  115. fprintf(fout, "-inf | ");
  116. else
  117. fprintf(fout, "%d | ", p->alpha);
  118. if(p->beta == INF)
  119. fprintf(fout, "inf\n");
  120. else
  121. fprintf(fout, "%d\n", p->beta);
  122.  
  123. printTree(fout, p, tabs_count + 1);
  124. }
  125. free(p);
  126. }*/
  127.  
  128. void printTree(FILE *fout, mw_ITree *tree, int tabs_count)
  129. {
  130. if(tree == NULL)
  131. return;
  132. mw_ITree *p;
  133. for(p = tree->left; p != NULL; p = p->right) {
  134. if(p->value == INF || p->value == -INF)
  135. continue;
  136. printTabs(fout, tabs_count);
  137. fprintf(fout, "%d\n", p->value);
  138. printTree(fout, p, tabs_count + 1);
  139. }
  140. free(p);
  141. }
  142.  
  143. bool is04(int x)
  144. {
  145. if(x == 0 || x == 4)
  146. return 1;
  147. return 0;
  148. }
  149.  
  150. bool isEndOfTheGame(int N, int M, int i, int j, char mtrx[20][20])
  151. {
  152. int H1, H2, H3, H4;
  153. int V1, V2, V3, V4;
  154. int R1, R2, R3, R4;
  155. int L1, L2, L3, L4;
  156. H1 = H2 = H3 = H4 = V1 = V2 = V3 = V4 = cINF;
  157. R1 = R2 = R3 = R4 = L1 = L2 = L3 = L4 = cINF;
  158. //Calculul sumelor pe orizontala
  159. if(j < M - 3)
  160. H1 = mtrx[i][j] + mtrx[i][j + 1] + mtrx[i][j + 2] + mtrx[i][j + 3];
  161. if(0 < j && j < M - 2)
  162. H2 = mtrx[i][j - 1] + mtrx[i][j] + mtrx[i][j + 1] + mtrx[i][j + 2];
  163. if(1 < j && j < M - 1)
  164. H3 = mtrx[i][j - 2] + mtrx[i][j - 1] + mtrx[i][j] + mtrx[i][j + 1];
  165. if(2 < j)
  166. H4 = mtrx[i][j - 3] + mtrx[i][j - 2] + mtrx[i][j - 1] + mtrx[i][j];
  167. //Calculul sumelor pe verticala
  168. if(i < N - 3)
  169. V1 = mtrx[i][j] + mtrx[i + 1][j] + mtrx[i + 2][j] + mtrx[i + 3][j];
  170. if(0 < i && i < N - 2)
  171. V2 = mtrx[i - 1][j] + mtrx[i][j] + mtrx[i + 1][j] + mtrx[i + 2][j];
  172. if(1 < i && i < N - 1)
  173. V3 = mtrx[i - 2][j] + mtrx[i - 1][j] + mtrx[i][j] + mtrx[i + 1][j];
  174. if(2 < i)
  175. V4 = mtrx[i - 3][j] + mtrx[i - 2][j] + mtrx[i - 1][j] + mtrx[i][j];
  176. //Calculul sumelor pe diagonala paralela cu diagonala principala
  177. if(i < N - 3 && j < M - 3)
  178. R1 = mtrx[i][j] + mtrx[i + 1][j + 1] + mtrx[i + 2][j + 2] + mtrx[i + 3][j + 3];
  179. if(0 < i && i < N - 2 && 0 < j && j < M - 2)
  180. R2 = mtrx[i - 1][j - 1] + mtrx[i][j] + mtrx[i + 1][j + 1] + mtrx[i + 2][j + 2];
  181. if(1 < i && i < N - 1 && 0 < j && j < M - 1)
  182. R3 = mtrx[i - 2][j - 2] + mtrx[i - 1][j - 1] + mtrx[i][j] + mtrx[i + 1][j + 1];
  183. if(2 < i && 2 < j)
  184. R4 = mtrx[i - 3][j - 3] + mtrx[i - 2][j - 2] + mtrx[i - 1][j - 1] + mtrx[i][j];
  185. //Calculul sumelor pe diagonala parelala cu diagonala secundara
  186. if(i < N - 3 && 2 < j)
  187. L1 = mtrx[i + 3][j - 3] + mtrx[i + 2][j - 2] + mtrx[i + 1][j - 1] + mtrx[i][j];
  188. if(0 < i && i < N - 2 && 1 < j && j < M - 1)
  189. L2 = mtrx[i + 2][j - 2] + mtrx[i + 1][j - 1] + mtrx[i][j] + mtrx[i - 1][j + 1];
  190. if(1 < i && i < N - 1 && 0 < j && j < M - 2)
  191. L3 = mtrx[i + 1][j - 1] + mtrx[i][j] + mtrx[i - 1][j + 1] + mtrx[i - 2][j + 2];
  192. if(2 < i && j < M - 3)
  193. L4 = mtrx[i][j] + mtrx[i - 1][j + 1] + mtrx[i - 2][j + 2] + mtrx[i - 3][j + 3];
  194. //Verificarea sumelor
  195. if(is04(H1) || is04(H2) || is04(H3) || is04(H4) ||
  196. is04(V1) || is04(V2) || is04(V3) || is04(V4) ||
  197. is04(R1) || is04(R2) || is04(R3) || is04(R4) ||
  198. is04(L1) || is04(L2) || is04(L3) || is04(L4))
  199. return 1;
  200. //Daca nu se termina prin castig jocul,
  201. // se termina cand nu mai exista spatii.
  202. for(i = 0; i < N; ++i)
  203. for(j = 0; j < M; ++j)
  204. if(mtrx[i][j] == 8)//cod personal pentru '-'
  205. return 0;
  206. return 1;
  207. }
  208.  
  209. void place_chip(FILE *fout, int N, int M, mw_MTree *tree, char chip, int depth)
  210. {
  211. int i, j;
  212. for(j = 0 ; j < M; ++j) {//TODO: maybe? modify swap fors
  213. for(i = N - 1; i >= 0; --i) {
  214. if(tree->matrix[i][j] == 8) {//cod personal pentru '-'
  215. mw_MTree *p = malloc(sizeof(mw_MTree));
  216. int ki, kj;
  217. for(ki = 0; ki < N; ++ki) {
  218. for(kj = 0; kj < M; ++kj)
  219. p->matrix[ki][kj] = tree->matrix[ki][kj];
  220. }
  221. p->matrix[i][j] = chip;
  222. printMatrix(fout, N, M, p->matrix, depth + 1);
  223. if(!isEndOfTheGame(N, M, i, j, p->matrix)) {
  224. insert_MTree(tree, p);
  225. place_chip(fout, N, M, p, chip ^ 1, depth + 1);
  226. //jetonul urmator pus va fi pus (chip xor 1)
  227. }
  228. free(p);
  229. break;
  230. }
  231. }
  232. }
  233. }
  234.  
  235. int maxValue(mw_ITree *tree)
  236. {
  237. //nu ar trebui ca tree sa fie NULL
  238. //tree are cel putin un fiu, caci altfel nu se apela functia
  239. //calculeaza maximul valorii copiilor lui tree
  240. mw_ITree *p;
  241. int maxx = -INF;
  242. for(p = tree->left; p != NULL; p = p->right) {
  243. if(p->value == INF)
  244. p->value = Minimax(p, 0);
  245. if(p->value > maxx)
  246. maxx = p->value;
  247. }
  248. return maxx;
  249. }
  250.  
  251. int minValue(mw_ITree *tree)
  252. {
  253. //nu ar trebui ca tree sa fie NULL
  254. //tree are cel putin un fiu, caci altfel nu se apela functia
  255. //calculeaza minimul valorii copiilor lui tree
  256. mw_ITree *p;
  257. int minn = INF;
  258. for(p = tree->left; p != NULL; p = p->right) {
  259. if(p->value == INF)
  260. p->value = Minimax(p, 1);
  261. if(p->value < minn)
  262. minn = p->value;
  263. }
  264. return minn;
  265. }
  266.  
  267. int Minimax(mw_ITree *tree, bool mM)
  268. {
  269. //mM: 0 - tura lui Min, 1 - tura lui Max
  270. if(tree == NULL)
  271. return 0;
  272. //daca nu mai are copii, atunci el e o frunza
  273. // si valoarea lui maxima/minima e tree->value
  274. if(tree->left == NULL)//nu are copii
  275. return tree->value;
  276. if(mM)
  277. return maxValue(tree);
  278. else
  279. return minValue(tree);
  280. }
  281.  
  282. void Task1(FILE *fin, FILE *fout)
  283. {
  284. int N, M, i, j;
  285. char RB, chip;
  286. fscanf(fin, "%d %d %c\n", &N, &M, &RB);
  287. chip = RB_to_nr(RB);
  288. mw_MTree *tree = malloc(sizeof(mw_MTree));
  289. tree->left = NULL;
  290. tree->right = NULL;
  291. for(i = 0; i < N; ++i) {
  292. for(j = 0; j < M; ++j) {
  293. char RB;
  294. fscanf(fin, "%c ", &(RB));
  295. tree->matrix[i][j] = RB_to_nr(RB);
  296. }
  297. }
  298. printMatrix(fout, N, M, tree->matrix, 0);
  299. place_chip(fout, N, M, tree, chip, 0);
  300. free(tree);
  301. }
  302.  
  303. void insert_ITree(mw_ITree *parent, mw_ITree *child)
  304. {
  305. child->left = NULL; //copilul nu are alti copii... e prea mic
  306. child->right = NULL;//copilul inca nu are si frati mai mici
  307. child->parent = parent;
  308. if(parent->left == NULL) {
  309. parent->left = child;
  310. return;
  311. }
  312. mw_ITree *p, *p_next;
  313. for(p = parent->left; p->right != NULL;) {
  314. p_next = p->right;
  315. p = p_next;
  316. }
  317. p->right = child;
  318. }
  319.  
  320. void insert_ITQueue(mw_ITQueue **parent, mw_ITQueue *child)
  321. {
  322. child->next = NULL;
  323. if((*parent) == NULL) {
  324. (*parent) = child;
  325. return;
  326. }
  327. mw_ITQueue *p, *p_next;
  328. for(p = (*parent); p->next != NULL;) {
  329. p_next = p->next;
  330. p = p_next;
  331. }
  332. p->next = child;
  333. }
  334.  
  335. mw_ITree *pop(mw_ITQueue **Q)
  336. {
  337. if((*Q) == NULL)
  338. return NULL;
  339. mw_ITree *tree;
  340. mw_ITQueue *temp;
  341. tree = (*Q)->tree;
  342. temp = (*Q);
  343. (*Q) = (*Q)->next;
  344. free(temp);
  345. return tree;
  346. }
  347.  
  348. void pop_all(mw_ITQueue **dest, mw_ITQueue **src)
  349. {
  350. while((*src)) {
  351. mw_ITQueue *p = malloc(sizeof(mw_ITQueue));
  352. mw_ITree *tree;
  353. mw_ITQueue *temp;
  354. p->next = NULL;
  355. tree = (*src)->tree;
  356. temp = (*src);
  357. (*src) = (*src)->next;
  358. free(temp);
  359. p->tree = tree;
  360. insert_ITQueue(&(*dest), p);
  361. }
  362. }
  363.  
  364. void Task2(FILE *fin, FILE *fout)
  365. {
  366. //TODO: maybe? set alpha, beta = 0, just in case
  367. mw_ITree *root = malloc(sizeof(mw_ITree));
  368. mw_ITree *crt;
  369. mw_ITQueue *old_level = malloc(sizeof(mw_ITQueue));
  370. mw_ITQueue *crt_level;
  371. int L, i, j, k, sum, nr_CrtLvl_Children;
  372. fscanf(fin, "%d\n(%d)\n", &L, &(nr_CrtLvl_Children));
  373. root->nrChild = nr_CrtLvl_Children;
  374. root->value = INF;
  375. root->left = NULL;
  376. root->right = NULL;
  377. old_level->tree = root;
  378. old_level->next = NULL;
  379. crt_level = NULL;
  380. for(i = 1; i < L; ++i) {
  381. sum = 0;
  382. k = 0;
  383. crt = pop(&old_level);
  384. for(j = 0; j < nr_CrtLvl_Children; ++j) {
  385. int x;
  386. char prt, prt2;
  387. mw_ITree *nodeT = malloc(sizeof(mw_ITree));
  388. mw_ITQueue *nodeQ = malloc(sizeof(mw_ITQueue));
  389. nodeT->left = NULL;
  390. nodeT->right = NULL;
  391. nodeQ->next = NULL;
  392. nodeQ->tree = NULL;
  393. fscanf(fin, "%c%d%c ", &prt, &x, &prt2);
  394. if(prt == '(') {
  395. sum += x;
  396. nodeT->value = INF;
  397. nodeT->nrChild = x;
  398. nodeQ->tree = nodeT;
  399. insert_ITree(crt, nodeT);
  400. insert_ITQueue(&crt_level, nodeQ);
  401. //free(nodeQ);
  402. }
  403. if(prt == '[') {
  404. nodeT->value = x;
  405. nodeT->nrChild = 0;
  406. nodeQ->tree = nodeT;
  407. insert_ITree(crt, nodeT);
  408. free(nodeQ);
  409. }
  410. if(++k == crt->nrChild) {
  411. k = 0;
  412. crt = pop(&old_level);
  413. }
  414. }
  415. nr_CrtLvl_Children = sum;
  416. //old_level is freed and NULL now
  417. pop_all(&old_level, &crt_level);
  418. //old_level = crt_level
  419. //crt_level is freed and NULL now
  420. }
  421. root->value = Minimax(root, 1);
  422. fprintf(fout, "%d\n", root->value);
  423. printTree(fout, root, 1);
  424. free(crt);
  425. free(crt_level);
  426. freeIT(root);
  427. }
  428.  
  429. bool full_child(mw_ITree *node)
  430. {
  431. int count = 0;
  432. mw_ITree *p;
  433. for(p = node->left; p != NULL; p = p->right)
  434. if(p->value != INF)
  435. ++count;
  436. else
  437. break;
  438. if(count == node->nrChild)
  439. return true;
  440. return false;
  441. }
  442.  
  443. void AB_copy(mw_ITree *q, mw_ITree *node)
  444. {
  445. // q = node, just for alpha, beta
  446. q->alpha = node->alpha;
  447. q->beta = node->beta;
  448. }
  449.  
  450. void AB_copy_leftside(mw_ITree *q, mw_ITree *node)
  451. {
  452. if(q->left == NULL)
  453. return;
  454. AB_copy(q, node);
  455. AB_copy_leftside(q->left, node);
  456. }
  457. /*
  458. void AB_copy_downside(mw_ITree *q, mw_ITree *node)
  459. {
  460. if(q->left == NULL)
  461. return;
  462. if(q->alpha != -INF || q->beta != INF)
  463. return;
  464. AB_copy(q, node);
  465. mw_ITree *p;
  466. for(p = node->left; p != NULL; p = p->right)
  467. AB_copy_downside(q->left, node);
  468. }*/
  469.  
  470. void AB_pruning(mw_ITree *node, bool mM)
  471. {
  472. if(node == NULL)
  473. return;
  474. mw_ITree *q;
  475. for(q = node->left; q != NULL; q = q->right) {
  476. if(node->alpha >= node->beta) {
  477. q->value = INF;
  478. continue;
  479. }
  480. //can be better
  481. if(q->left != NULL && q != node->left) {
  482. AB_copy(q, node);
  483. AB_copy_leftside(q, node);
  484. }
  485. if(q->value == INF || q->value == -INF)
  486. AB_pruning(q, mM ^ 1);
  487. if(mM) {
  488. if(q->alpha > node->alpha)
  489. node->alpha = q->alpha;
  490. if(q->beta != INF && q->beta > node->alpha)
  491. node->alpha = q->beta;
  492. if(q->value > node->value)
  493. node->value = q->value;
  494. } else {
  495. if(q->alpha != -INF && q->alpha < node->beta)
  496. node->beta = q->alpha;
  497. if(q->beta < node->beta)
  498. node->beta = q->beta;
  499. if(q->value < node->value)
  500. node->value = q->value;
  501. }
  502. }
  503. //fprintf(stdout, "%d | %d | %d\n", root->value, root->alpha, root->beta);
  504. //printTree(stdout, root, 1);
  505. //printf("\n\n");
  506. }
  507.  
  508. void Task3(FILE *fin, FILE *fout)
  509. {
  510. mw_ITree *root = malloc(sizeof(mw_ITree));
  511. mw_ITree *crt;
  512. mw_ITQueue *old_level = malloc(sizeof(mw_ITQueue));
  513. mw_ITQueue *crt_level;
  514. int L, i, j, k, sum, nr_CrtLvl_Children;
  515. fscanf(fin, "%d\n(%d)\n", &L, &(nr_CrtLvl_Children));
  516. root->nrChild = nr_CrtLvl_Children;
  517. root->value = -INF;
  518. root->alpha = -INF;
  519. root->beta = INF;
  520. root->left = NULL;
  521. root->right = NULL;
  522. old_level->tree = root;
  523. old_level->next = NULL;
  524. crt_level = NULL;
  525. for(i = 1; i < L; ++i) {
  526. sum = 0;
  527. k = 0;
  528. crt = pop(&old_level);
  529. for(j = 0; j < nr_CrtLvl_Children; ++j) {
  530. int x;
  531. char prt, prt2;
  532. mw_ITree *nodeT = malloc(sizeof(mw_ITree));
  533. mw_ITQueue *nodeQ = malloc(sizeof(mw_ITQueue));
  534. nodeT->left = NULL;
  535. nodeT->right = NULL;
  536. nodeQ->next = NULL;
  537. nodeQ->tree = NULL;
  538. fscanf(fin, "%c%d%c ", &prt, &x, &prt2);
  539. if(prt == '(') {
  540. sum += x;
  541. nodeT->alpha = -INF;
  542. nodeT->beta = INF;
  543. if(i & 1) //impar
  544. nodeT->value = INF;
  545. else //par
  546. nodeT->value = -INF;
  547. nodeT->nrChild = x;
  548. nodeQ->tree = nodeT;
  549. insert_ITree(crt, nodeT);
  550. insert_ITQueue(&crt_level, nodeQ);
  551. //free(nodeQ);
  552. }
  553. if(prt == '[') {
  554. nodeT->value = x;
  555. nodeT->nrChild = 0;
  556. nodeT->alpha = x;
  557. nodeT->beta = x;
  558. nodeQ->tree = nodeT;
  559. insert_ITree(crt, nodeT);
  560. free(nodeQ);
  561. }
  562. if(++k == crt->nrChild) {
  563. k = 0;
  564. crt = pop(&old_level);
  565. }
  566. }
  567. nr_CrtLvl_Children = sum;
  568. //old_level is freed and NULL now
  569. pop_all(&old_level, &crt_level);
  570. //old_level = crt_level
  571. //crt_level is freed and NULL now
  572. }
  573. //fprintf(fout, "%d\n", root->value);
  574. // printTree(fout, root, 1);
  575. //
  576. AB_pruning(root, 1);
  577. //
  578. //fprintf(fout, "%d | %d | %d\n", root->value, root->alpha, root->beta);
  579. fprintf(fout, "%d\n", root->value);
  580. printTree(fout, root, 1);
  581. free(crt);
  582. free(crt_level);
  583. freeIT(root);
  584. }
  585.  
  586. void TaskBonus()
  587. {
  588. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement