Advertisement
Guest User

Untitled

a guest
May 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.50 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stack>
  4. #include <list>
  5. #include <string>
  6. #include <queue>
  7.  
  8. using namespace std;
  9. class Data
  10. {
  11. int i = 0;
  12. int width;
  13. int height;
  14. int column = 0;
  15. int country_num = 0;
  16. stack <int> stos;
  17. list<int> *adj_list;
  18. priority_queue<int> *pq;
  19. int *path;
  20. // values: 0 - wall, 1 - standard, 2 - border, 3 - road standard , 4 - road border;
  21. struct block
  22. {
  23. int country_id;
  24. int block_id;
  25. int status; // 0 - pusty, 1 - odwiedzony, 2 - zakonczony
  26. int level; // poziom na którym znajduje się wierzcholek
  27. bool road = false;
  28. block* top;
  29. block* right;
  30. block* bottom;
  31. block* left;
  32. block* parrent;
  33. int top_value;
  34. int right_value;
  35. int bottom_value;
  36. int left_value;
  37.  
  38. int number;
  39. }*head, *last;
  40. public:
  41. Data(int wid, int hei, int num_country)
  42. {
  43. this->width = wid;
  44. this->height = hei;
  45. this->head = nullptr;
  46. this->last = nullptr;
  47. this->country_num = num_country;
  48. path = new int[25];
  49. adj_list = new list<int>[25];
  50. }
  51. ~Data()
  52. {
  53.  
  54. }
  55. int check()
  56. {
  57. block* checker = head;
  58. block* start = head;
  59. int checked_country[25];
  60. for (int i = 0; i < country_num; i++)
  61. {
  62. checked_country[i] = 0;
  63. }
  64. while (start != nullptr)
  65. {
  66. while (checker != nullptr)
  67. {
  68. // cout << "Node:" << checker->block_id << endl;
  69. if (checker->road == false)
  70. {
  71. if ((checker->left != nullptr) && (checker->left->road == false) && (checker->left_value == 2))
  72. {
  73. if (checker->block_id != 0)
  74. return checker->block_id;
  75. return 1;
  76. }
  77. if ((checker->bottom != nullptr) && (checker->bottom->road == false) && (checker->bottom_value == 2))
  78. {
  79. if (checker->block_id != 0)
  80. return checker->block_id;
  81. return 1;
  82. }
  83. if ((checker->right != nullptr) && (checker->right->road == false) && (checker->right_value == 2))
  84. {
  85. if (checker->block_id != 0)
  86. return checker->block_id;
  87. return 1;
  88. }
  89. if ((checker->top != nullptr) && (checker->top->road == false) && (checker->top_value == 2))
  90. {
  91. if (checker->block_id != 0)
  92. return checker->block_id;
  93. return 1;
  94. }
  95.  
  96. }
  97.  
  98. if (checker->left_value == 4)
  99. {
  100. checked_country[checker->country_id]++;
  101. }
  102.  
  103. if (checker->bottom_value == 4)
  104. {
  105. checked_country[checker->country_id]++;
  106. }
  107.  
  108. if (checker->right_value == 4)
  109. {
  110. checked_country[checker->country_id]++;
  111. }
  112.  
  113. if (checker->top_value == 4)
  114. {
  115. checked_country[checker->country_id]++;
  116. }
  117. checker = checker->right;
  118. }
  119. start = start->bottom;
  120. checker = start;
  121. }
  122. // cout << "-----" << endl;
  123. for (int x = 0; x < country_num; x++)
  124. {
  125. if (checked_country[x] != 2)
  126. {
  127. if (x != 0)
  128. return x;
  129. return 999;
  130. }
  131.  
  132. }
  133. return 0;
  134.  
  135. }
  136. void clearStatus()
  137. {
  138. block *vertical = head;
  139. block *horizontal = vertical;
  140.  
  141. while (vertical != nullptr)
  142. {
  143. while (horizontal != nullptr)
  144. {
  145. horizontal->status = 0;
  146. horizontal = horizontal->right;
  147. }
  148. vertical = vertical->bottom;
  149. horizontal = vertical;
  150. }
  151. }
  152. void printStatus()
  153. {
  154. block *vertical = head;
  155. block *horizontal = vertical;
  156.  
  157. while (vertical != nullptr)
  158. {
  159. while (horizontal != nullptr)
  160. {
  161. cout << horizontal->status << " ";
  162. horizontal = horizontal->right;
  163. }
  164. cout << endl;
  165. vertical = vertical->bottom;
  166. horizontal = vertical;
  167. }
  168. }
  169. // 18.05.18r.
  170.  
  171. // z naszego grafu musimy utworzyc adjancy list
  172. // dla naszego algorytmu będziemy musieli tworzyć nowę listę dla każdego z poziomów.
  173. void makeList()
  174. {
  175. block *start = head;
  176. block *row = head;
  177. while (row != nullptr)
  178. {
  179. while (start != nullptr)
  180. {
  181. if (start->bottom != nullptr)
  182. adj_list[start->block_id].push_back(start->bottom->block_id);
  183. if (start->right != nullptr)
  184. adj_list[start->block_id].push_back(start->right->block_id);
  185. if (start->top != nullptr)
  186. adj_list[start->block_id].push_back(start->top->block_id);
  187. if (start->left != nullptr)
  188. adj_list[start->block_id].push_back(start->left->block_id);
  189.  
  190. start = start->right;
  191. }
  192. row = row->bottom;
  193. start = row;
  194. }
  195.  
  196. }
  197. // musimy przeciazyc funkcje w zależnosci od poziomu do którego możemy zejść
  198. void makeList(int depth)
  199. {
  200. block *start = head;
  201. block *row = head;
  202. while (row != nullptr)
  203. {
  204. while (start != nullptr)
  205. {
  206. if (start->bottom != nullptr && start->bottom->level <= depth)
  207. adj_list[start->block_id].push_back(start->bottom->block_id);
  208. if (start->right != nullptr && start->right->level <= depth)
  209. adj_list[start->block_id].push_back(start->right->block_id);
  210. if (start->top != nullptr && start->top->level <= depth)
  211. adj_list[start->block_id].push_back(start->top->block_id);
  212. if (start->left != nullptr && start->left->level <= depth)
  213. adj_list[start->block_id].push_back(start->left->block_id);
  214.  
  215. start = start->right;
  216. }
  217. row = row->bottom;
  218. start = row;
  219. }
  220. }
  221. void clearList()
  222. {
  223. for (int i = 0; i <= 24; i++)
  224. {
  225. adj_list[i].clear();
  226. }
  227. }
  228. void printList() // możemy sobie wyprintować te adjancy list
  229. {
  230. cout << endl;
  231. for (int u = 0; u <= 24; u++)
  232. {
  233. // cout << u << ":";
  234. list<int>::iterator i;
  235. for (i = adj_list[u].begin(); i != adj_list[u].end(); ++i)
  236. {
  237.  
  238. }
  239. // cout << *i << " ";
  240. // cout << endl;
  241. }
  242. }
  243.  
  244. /// 19.05.18
  245. void generatePath(block * box)
  246. {
  247. cout << "Znalazł";
  248. }
  249. void nextShortestPath(block *start)
  250. {
  251. start = head;
  252. block *current;
  253. block *nextStep;
  254. this->clearList();
  255. this->makeList();
  256.  
  257. bool *visited = new bool[25];
  258. int path_index = 0;
  259.  
  260. for (int i = 0; i < 25; i++)
  261. visited[i] = false;
  262.  
  263.  
  264. while (current != nullptr)
  265. {
  266. if (current->block_id == 1)
  267. this->generatePath(current);
  268. else
  269. {
  270.  
  271. list<int>::iterator i;
  272. for (i = adj_list[current->block_id].begin(); i != adj_list[current->block_id].end(); ++i)
  273. {
  274. if (!visited[*i])
  275. {
  276. if (current->bottom != nullptr && current->bottom->block_id == *i)
  277. {
  278. nextStep = current->bottom;
  279. }
  280. else if (current->right != nullptr && current->right->block_id == *i)
  281. {
  282. nextStep = current->right;
  283. }
  284. pq->push(nextStep->block_id);
  285. // current = sąsiad
  286. }
  287. }
  288. }
  289. current = nextStep;
  290. }
  291.  
  292.  
  293.  
  294. }
  295.  
  296. /// 19.05.18
  297.  
  298. void IDDFS()
  299. {
  300. int min_depth = 0;
  301. int max_depth = 8;
  302.  
  303. for (int i = min_depth; i <= max_depth; i++)
  304. {
  305. this->clearList();
  306. cout << "======= LEVEL " << i << "===========" << endl;
  307. this->iterativeDFS(0, 1, i);
  308. // tutaj trzeba będzie sprawdzać czy na danym poziomie już znaleźliśmy rozwiązanie.
  309. // !!wywołujemy checkera!! Jeżeli checker == true -> przerywamy JEŻELI CHECKER NIE ZNALAZŁ ROZWIĄZANIA TO IDZIEMY DALEJ CO OZNACZA ŻE ZWIĘKSZAMY POZIOM
  310. }
  311. }
  312. void iterativeDFS(int s, int d, int depth)
  313. {
  314. this->makeList(depth);
  315. this->printList();
  316. cout << endl;
  317.  
  318. bool *visited = new bool[25];
  319.  
  320. int path_index = 0;
  321.  
  322. for (int i = 0; i < 25; i++)
  323. visited[i] = false;
  324.  
  325. recursiveDFS(s, d, visited, path, path_index);
  326.  
  327. }
  328.  
  329. void recursiveDFS(int u, int d, bool visited[], int path[], int &path_index)
  330. {
  331. visited[u] = true;
  332. path[path_index] = u;
  333. path_index++;
  334.  
  335. if (u == d)
  336. {
  337. /*for (int i = 0; i<path_index; i++)
  338. cout << path[i] << " ";
  339. cout << endl;*/
  340. makeroute(path_index);
  341.  
  342. }
  343. else
  344. {
  345. list<int>::iterator i;
  346. for (i = adj_list[u].begin(); i != adj_list[u].end(); ++i)
  347. if (!visited[*i])
  348. recursiveDFS(*i, d, visited, path, path_index);
  349. }
  350.  
  351. path_index--;
  352. visited[u] = false;
  353.  
  354. }
  355. // 18.05.18r.
  356. void makeroute(int pa_index)
  357. { // values: 0 - wall, 1 - standard, 2 - border, 3 - road standard , 4 - road border;
  358. block* temp = head;
  359. if (temp->block_id != path[0])
  360. {
  361. cout << "Something went wrong" << endl;
  362. return;
  363. }
  364. temp->road = true;
  365. for (int i = 1; i < pa_index; i++)
  366. {
  367. if (temp->bottom != nullptr && temp->bottom->block_id == path[i])
  368. {
  369. temp = temp->bottom;
  370. temp->road = true;
  371. if (temp->top_value == 1)
  372. {
  373. temp->top_value = 3;
  374. temp->top->bottom_value = 3;
  375. }
  376. else if (temp->top_value == 2)
  377. {
  378. temp->top_value = 4;
  379. temp->top->bottom_value = 4;
  380. }
  381.  
  382. }
  383. if (temp->right != nullptr && temp->right->block_id == path[i])
  384. {
  385. temp = temp->right;
  386. temp->road = true;
  387. if (temp->left_value == 1)
  388. {
  389. temp->left_value = 3;
  390. temp->left->right_value = 3;
  391. }
  392. else if (temp->left_value == 2)
  393. {
  394. temp->left_value = 4;
  395. temp->left->right_value = 4;
  396. }
  397. }
  398. if (temp->top != nullptr && temp->top->block_id == path[i])
  399. {
  400. temp = temp->top;
  401. temp->road = true;
  402. if (temp->bottom_value == 1)
  403. {
  404. temp->bottom_value = 3;
  405. temp->bottom->top_value = 3;
  406. }
  407. else if (temp->bottom_value == 2)
  408. {
  409. temp->bottom_value = 4;
  410. temp->bottom->top_value = 4;
  411. }
  412. }
  413. if (temp->left != nullptr && temp->left->block_id == path[i])
  414. {
  415. temp = temp->left;
  416. temp->road = true;
  417. if (temp->right_value == 1)
  418. {
  419. temp->right_value = 3;
  420. temp->right->left_value = 3;
  421. }
  422. else if (temp->right_value == 2)
  423. {
  424. temp->right_value = 4;
  425. temp->right->left_value = 4;
  426. }
  427. }
  428.  
  429. }
  430. if (temp->left != nullptr && temp->left->block_id == 0)
  431. {
  432. temp->road = true;
  433. if (temp->left_value == 1)
  434. {
  435. temp->left_value = 3;
  436. temp->left->right_value = 3;
  437. }
  438. else if (temp->left_value == 2)
  439. {
  440. temp->left_value = 4;
  441. temp->left->right_value = 4;
  442. }
  443. }
  444. int work = check();
  445. if (work == 0)
  446. {
  447. cout << "Route FOUND !!!" << endl;
  448. this->printStat();
  449. }
  450. this->clearRoads();
  451.  
  452. }
  453.  
  454. void clearRoads()
  455. {
  456. block* temp = head;
  457. block* start = head;
  458. while (start != nullptr)
  459. {
  460. while (temp != nullptr)
  461. {
  462. if (temp->bottom_value == 3 || temp->bottom_value == 4)
  463. temp->bottom_value = temp->bottom_value - 2;
  464. if (temp->top_value == 3 || temp->top_value == 4)
  465. temp->top_value = temp->top_value - 2;
  466. if (temp->left_value == 3 || temp->left_value == 4)
  467. temp->left_value = temp->left_value - 2;
  468. if (temp->right_value == 3 || temp->right_value == 4)
  469. temp->right_value = temp->right_value - 2;
  470.  
  471. temp->road = false;
  472. temp = temp->right;
  473. }
  474. start = start->bottom;
  475. temp = start;
  476. }
  477. }
  478. void printStat()
  479. {
  480. block* temp = head;
  481. block* start = head;
  482. while (start != nullptr)
  483. {
  484. while (temp != nullptr)
  485. {
  486. cout << " " << temp->country_id << "C " << temp->road << " " << temp->left_value << "-" << temp->bottom_value << "-" << temp->right_value << "-" << temp->top_value;
  487. temp = temp->right;
  488. }
  489. cout << endl;
  490. start = start->bottom;
  491. temp = start;
  492. }
  493. }
  494. void printStack()
  495. {
  496. int size = stos.size();
  497.  
  498. while (stos.empty() == false)
  499. {
  500. cout << stos.top() << " <- ";
  501. stos.pop();
  502. }
  503. }
  504. void print()
  505. {
  506. /*o -+- o -+- x
  507. + --- + --- |
  508. o -+- x --- x
  509. | --- + --- |
  510. x -+- x --- x*/
  511. block* temp;
  512. block* start;
  513. temp = head;
  514. start = temp;
  515. while (start != nullptr)
  516. {
  517. while (temp != nullptr)
  518. {
  519. cout << "o ";
  520. if (temp->right_value == 2)
  521. cout << "-+- ";
  522. else if (temp->right_value == 1)
  523. cout << "--- ";
  524. else if (temp->right_value == 2)
  525. {
  526.  
  527. }
  528. temp = temp->right;
  529.  
  530. }
  531. cout << endl;
  532. temp = start;
  533. while (temp != nullptr && temp->bottom != nullptr)
  534. {
  535. if (temp->bottom_value == 1)
  536. {
  537. cout << "|";
  538. if (temp->right != nullptr)
  539. {
  540. cout << " --- ";
  541. }
  542. }
  543. if (temp->bottom_value == 2)
  544. {
  545. cout << "+";
  546. if (temp->right != nullptr)
  547. {
  548. cout << " --- ";
  549. }
  550. }
  551. temp = temp->right;
  552. }
  553.  
  554.  
  555. cout << endl;
  556.  
  557. temp = start->bottom;
  558. start = temp;
  559. }
  560.  
  561. }
  562. void add(int value, int value_2, int country, int level)
  563. {
  564. block *temp = new block;
  565.  
  566. temp->parrent = nullptr;
  567. temp->country_id = country;
  568. temp->block_id = i;
  569. i++;
  570. temp->right_value = value;
  571. temp->bottom_value = value_2;
  572. temp->bottom = nullptr;
  573. temp->right = nullptr;
  574. temp->level = level;
  575. if (head == nullptr) // we are adding head block - postiion (0,0)
  576. {
  577. temp->top_value = 0;
  578. temp->left_value = 0;
  579. temp->right_value = value;
  580. temp->bottom_value = value_2;
  581. temp->top = nullptr;
  582. temp->right = nullptr;
  583. temp->bottom = nullptr;
  584. temp->left = nullptr;
  585. last = temp;
  586. head = temp;
  587. column = 1;
  588. return;
  589. }
  590. if (column == this->width)
  591. {
  592. block *finder;
  593. finder = last;
  594. last->right_value = 0;
  595. while (finder->left != nullptr)
  596. {
  597. finder = finder->left;
  598. }
  599. finder->bottom = temp;
  600. temp->top_value = finder->bottom_value;
  601. temp->top = finder;
  602. temp->bottom = nullptr;
  603. temp->right = nullptr;
  604. temp->right_value = value;
  605. temp->bottom_value = value_2;
  606. temp->left = nullptr;
  607. temp->left_value = 0;
  608. last = temp;
  609. column = 1;
  610. finder = nullptr;
  611. }
  612. else
  613. {
  614. last->right = temp;
  615. temp->left = last;
  616.  
  617. temp->left_value = last->right_value;
  618. if (last->top != nullptr)
  619. {
  620. temp->top = last->top->right;
  621. last->top->right->bottom = temp;
  622. temp->top_value = last->top->right->bottom_value;
  623. }
  624. else
  625. {
  626. temp->top = nullptr;
  627. temp->top_value = 0;
  628. }
  629. last = temp;
  630. this->column++;
  631. return;
  632. }
  633. }
  634. void path_ex()
  635. {
  636. ///values: 0 - wall, 1 - standard, 2 - border, 3 - road standard, 4 - road border;
  637. block* roader = head;
  638. roader->road = true;
  639. roader->right_value = 3;
  640. cout << roader->block_id << endl;
  641.  
  642. roader = roader->right;
  643. roader->left_value = 3;
  644. roader->right_value = 3;
  645. roader->road = true;
  646. cout << roader->block_id << endl;
  647.  
  648. roader = roader->right;
  649. roader->left_value = 3;
  650. roader->right_value = 3;
  651. roader->road = true;
  652. cout << roader->block_id << endl;
  653.  
  654. roader = roader->right;
  655. roader->left_value = 3;
  656. roader->right_value = 3;
  657. roader->road = true;
  658. cout << roader->block_id << endl;
  659.  
  660. roader = roader->right;
  661. roader->left_value = 3;
  662. roader->bottom_value = 4;
  663. roader->road = true;
  664. cout << roader->block_id << endl;
  665.  
  666. roader = roader->bottom;
  667. roader->top_value = 4;
  668. roader->left_value = 3;
  669. roader->road = true;
  670. cout << roader->block_id << endl;
  671.  
  672. roader = roader->left;
  673. roader->right_value = 3;
  674. roader->bottom_value = 3;
  675. roader->road = true;
  676. cout << roader->block_id << endl;
  677.  
  678. roader = roader->bottom;
  679. roader->top_value = 3;
  680. roader->right_value = 4;
  681. roader->road = true;
  682. cout << roader->block_id << endl;
  683.  
  684. roader = roader->right;
  685. roader->left_value = 4;
  686. roader->bottom_value = 3;
  687. roader->road = true;
  688. cout << roader->block_id << endl;
  689.  
  690. roader = roader->bottom;
  691. roader->top_value = 3;
  692. roader->left_value = 4;
  693. roader->road = true;
  694. cout << roader->block_id << endl;
  695.  
  696. roader = roader->left;
  697. roader->right_value = 4;
  698. roader->bottom_value = 3;
  699. roader->road = true;
  700. cout << roader->block_id << endl;
  701.  
  702. roader = roader->bottom;
  703. roader->top_value = 3;
  704. roader->left_value = 3;
  705. roader->road = true;
  706. cout << roader->block_id << endl;
  707.  
  708. roader = roader->left;
  709. roader->right_value = 3;
  710. roader->left_value = 3;
  711. roader->road = true;
  712. cout << roader->block_id << endl;
  713.  
  714. roader = roader->left;
  715. roader->right_value = 3;
  716. roader->left_value = 4;
  717. roader->road = true;
  718. cout << roader->block_id << endl;
  719.  
  720. roader = roader->left;
  721. roader->right_value = 4;
  722. roader->top_value = 4;
  723. roader->road = true;
  724. cout << roader->block_id << endl;
  725.  
  726. roader = roader->top;
  727. roader->bottom_value = 4;
  728. roader->top_value = 4;
  729. roader->road = true;
  730. cout << roader->block_id << endl;
  731.  
  732. roader = roader->top;
  733. roader->bottom_value = 4;
  734. roader->right_value = 4;
  735. roader->road = true;
  736. cout << roader->block_id << endl;
  737.  
  738. roader = roader->right;
  739. roader->left_value = 4;
  740. roader->top_value = 3;
  741. roader->road = true;
  742. cout << roader->block_id << endl;
  743.  
  744. roader = roader->top;
  745. roader->bottom_value = 3;
  746. roader->left_value = 3;
  747. roader->road = true;
  748. cout << roader->block_id << endl;
  749.  
  750. roader = roader->left;
  751. roader->right_value = 3;
  752. roader->top_value = 4;
  753. roader->road = true;
  754. cout << roader->block_id << endl;
  755.  
  756. roader = roader->top;
  757. roader->bottom_value = 4;
  758. cout << roader->block_id << endl;
  759.  
  760.  
  761. }
  762. };
  763.  
  764.  
  765. int main()
  766. { // values: 0 - wall, 1 - standard, 2 - border, 3 - road;
  767. Data* ss = new Data(5, 5, 8);
  768. ss->add(1, 2, 0, 0);
  769. ss->add(1, 2, 0, 1);
  770. ss->add(1, 2, 0, 2);
  771. ss->add(1, 2, 0, 3);
  772. ss->add(0, 2, 0, 4);
  773.  
  774. ss->add(1, 2, 1, 1);
  775. ss->add(2, 1, 1, 2);
  776. ss->add(2, 1, 2, 3);
  777. ss->add(1, 1, 3, 4);
  778. ss->add(0, 2, 3, 5);
  779.  
  780. ss->add(2, 2, 4, 2);
  781. ss->add(2, 2, 1, 3);
  782. ss->add(2, 1, 2, 4);
  783. ss->add(2, 2, 3, 5);
  784. ss->add(0, 1, 5, 6);
  785.  
  786. ss->add(1, 2, 2, 3);
  787. ss->add(1, 2, 2, 4);
  788. ss->add(2, 2, 2, 5);
  789. ss->add(2, 1, 6, 6);
  790. ss->add(0, 1, 5, 7);
  791.  
  792. ss->add(2, 0, 7, 4);
  793. ss->add(1, 0, 6, 5);
  794. ss->add(1, 0, 6, 6);
  795. ss->add(2, 0, 6, 7);
  796. ss->add(0, 0, 5, 8);
  797. ss->print();
  798. //ss->printAllCycles(0,1);
  799. //ss->iterativeDFS();
  800. cout << endl;
  801.  
  802. cout << endl;
  803. ss->IDDFS();
  804. cout << " Koniec";
  805. //ss->printStatus();
  806. //ss->path_ex();
  807. //ss->check();
  808. getchar();
  809.  
  810. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement