Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class MyStack {
  11. private:
  12.     struct help {
  13.         int value;
  14.         help *next;
  15.     };
  16.     help *head;
  17.  
  18. public:
  19.     MyStack();
  20.     ~MyStack();
  21.     MyStack(const MyStack & Stack);
  22.     void push(int value);
  23.     int pop();
  24.     void del();
  25.     bool isEmpty();
  26.     void printMyStack();
  27.     void clone(MyStack & Stack);
  28. };
  29.  
  30. MyStack::MyStack() :head(NULL) {
  31. }
  32.  
  33. MyStack::~MyStack() {
  34.     help *current = head;
  35.     while (current != NULL) {
  36.         current = head->next;
  37.         delete head;
  38.         head = current;
  39.     }
  40. }
  41.  
  42. MyStack::MyStack(const MyStack & Stack) {
  43.     head = NULL;
  44.     help *current = Stack.head;
  45.     while (current != NULL) {
  46.         push(current->value);
  47.         current = current->next;
  48.     }
  49. }
  50.  
  51. void MyStack::push(int value) {
  52.     help *current = new help;
  53.     current->value = value;
  54.     current->next = head;
  55.     head = current;
  56.  
  57. }
  58.  
  59. int MyStack::pop() {
  60.     if (head == NULL) return -1;
  61.     else {
  62.         int buf = head->value;
  63.         help *current = head;
  64.         head = head->next;
  65.         delete current;
  66.         return buf;
  67.     }
  68.  
  69. }
  70.  
  71. void MyStack::del() {
  72.     help *current = head;
  73.     current = head->next;
  74.     delete head;
  75.     head = current;
  76. }
  77.  
  78. void MyStack::printMyStack() {
  79.     while (head != NULL) {
  80.         cout << pop() << " ";
  81.     }
  82.     cout << endl;
  83. }
  84.  
  85. bool MyStack::isEmpty() {
  86.     return head ? false : true;
  87. }
  88.  
  89. void MyStack::clone(MyStack & Stack) {
  90.     while (!Stack.isEmpty()) {
  91.             Stack.pop();
  92.     }
  93.     while (head != NULL) {
  94.         Stack.push(pop());
  95.     }
  96. }
  97.  
  98. struct Cell {
  99.     int x;
  100.     int y;
  101.     int index;
  102.     int weight;
  103. };
  104.  
  105. void makeMap(int N, Cell *allCells) {
  106.     ifstream read;
  107.     read.open("map1.txt");
  108.     for (int i = 0; i < N; i++) {
  109.         for (int j = 0; j < N; j++) {
  110.             read >> allCells[i*N + j].weight;
  111.             allCells[i*N + j].x = i;
  112.             allCells[i*N + j].y = j;
  113.             if (allCells[i*N + j].weight < 0)
  114.                 allCells[i*N + j].index = -1;
  115.             else allCells[i*N + j].index = 0;
  116.         }
  117.     }
  118.     read.close();
  119. }
  120.  
  121. void printMap(int N, Cell *allCells) {
  122.     char free = 249, busy = 254;
  123.     cout << "It's a Map" << endl << endl;
  124.     for (int i = 0; i < N; i++) {
  125.         for (int j = 0; j < N; j++) {
  126.             if (allCells[i*N + j].weight >= 0) cout << setw(3) << free;
  127.             else cout << setw(3) << busy;
  128.         }
  129.         cout << endl;
  130.     }
  131.     cout << endl;
  132. }
  133.  
  134. void makeStartPoint(int N, Cell *allCels, int *start) {
  135.     int X, Y;
  136.     bool flag = true;
  137.     while (flag) {
  138.         cout << "Input x and y of the START cell" << endl;
  139.         cin >> X >> Y;
  140.         if ((allCels[(X - 1)*N + (Y - 1)].weight >= 0) && ((X - 1)*N + (Y - 1) >= 0) && ((X - 1)*N + (Y - 1) < N*N)) {
  141.             *start = (X - 1)*N + (Y - 1);
  142.             flag = false;
  143.         }
  144.         else {
  145.             cout << endl << "This cell is closed or is out of map" << endl;
  146.         }
  147.     }
  148. }
  149.  
  150. void makeFinishPoint(int N, Cell *allCels, int *finish) {
  151.     int X, Y;
  152.     bool flag = true;
  153.     while (flag) {
  154.         cout << endl << "Input x and y of the FINISH cell" << endl;
  155.         cin >> X >> Y;
  156.         if ((allCels[(X - 1)*N + (Y - 1)].weight >= 0) && ((X - 1)*N + (Y - 1) >= 0) && ((X - 1)*N + (Y - 1) < N*N)) {
  157.             *finish = (X - 1)*N + (Y - 1);
  158.             flag = false;
  159.         }
  160.         else {
  161.             cout << endl << "This cell is closed or is out of map" << endl;
  162.         }
  163.     }
  164. }
  165.  
  166. void wave(int N, struct Cell *allCells, int start, int finish) {
  167.     queue <Cell> myqueue;
  168.     allCells[start].index = 0;
  169.     myqueue.push(allCells[start]);
  170.     while (myqueue.empty() == false) {
  171.         Cell buf = myqueue.front();
  172.         int parameter = buf.x*N + buf.y;
  173.         if (parameter == finish)  break;
  174.         if ((allCells[parameter + 1].index == 0) && ((parameter + 1) < N*N) && (parameter % N != (N - 1)) && (parameter + 1 != start)) {
  175.             allCells[parameter + 1].index = buf.index + 1;
  176.             myqueue.push(allCells[parameter + 1]);
  177.         }
  178.         if ((allCells[parameter - 1].index == 0) && ((parameter - 1) >= 0) && (parameter % N != 0) && (parameter - 1 != start)) {
  179.             allCells[parameter - 1].index = buf.index + 1;
  180.             myqueue.push(allCells[parameter - 1]);
  181.         }
  182.         if ((allCells[parameter - N].index == 0) && ((parameter - N) >= 0) && (parameter - N != start)) {
  183.             allCells[parameter - N].index = buf.index + 1;
  184.             myqueue.push(allCells[parameter - N]);
  185.         }
  186.         if ((allCells[parameter + N].index == 0) && ((parameter + N) < N*N) && (parameter + N != start)) {
  187.             allCells[parameter + N].index = buf.index + 1;
  188.             myqueue.push(allCells[parameter + N]);
  189.         }
  190.         myqueue.pop();
  191.     }
  192. }
  193.  
  194. void printWave(int N, struct Cell *allCells, int start, int finish) {
  195.     char free = 249, busy = 254;
  196.     cout << "It's your wave" << endl;
  197.     for (int i = 0; i < N; i++) {
  198.         for (int j = 0; j < N; j++) {
  199.             if (allCells[i*N + j].x*N + allCells[i*N + j].y == start) cout << setw(3) << 'S';
  200.             else {
  201.                 if (allCells[i*N + j].x*N + allCells[i*N + j].y == finish) cout << setw(3) << 'F';
  202.                 else {
  203.                     if (allCells[i*N + j].index > 0) cout << setw(3) << i*N + j;
  204.                     else {
  205.                         if (allCells[i*N + j].index == 0) cout << setw(3) << free;
  206.                         else {
  207.                             cout << setw(3) << busy;
  208.                         }
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.         cout << endl;
  214.     }
  215.     cout << "=============================================" << endl;
  216.     for (int i = 0; i < N; i++) {
  217.         for (int j = 0; j < N; j++) {
  218.             if (allCells[i*N + j].x*N + allCells[i*N + j].y == start) cout << setw(3) << 'S';
  219.             else {
  220.                 if (allCells[i*N + j].x*N + allCells[i*N + j].y == finish) cout << setw(3) << 'F';
  221.                 else {
  222.                     if (allCells[i*N + j].index > 0) cout << setw(3) << allCells[i*N + j].index;
  223.                     else {
  224.                         if (allCells[i*N + j].index == 0) cout << setw(3) << free;
  225.                         else {
  226.                             cout << setw(3) << busy;
  227.                         }
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.         cout << endl;
  233.     }
  234.     cout << endl;
  235. }
  236.  
  237. void printAnswer(int N, Cell *allCells, int start, int finish) {
  238.     int current = finish;
  239.     int ans;
  240.     while (current != start) {
  241.         if (allCells[current + 1].index == allCells[current].index - 1) {
  242.             allCells[current].weight = -10;
  243.             current = current + 1;
  244.         }
  245.         else {
  246.             if ((allCells[current - 1].index == allCells[current].index - 1) && (current % 10 != 0)) {
  247.                 allCells[current].weight = -10;
  248.                 current = current - 1;
  249.             }
  250.             else {
  251.                 if (allCells[current - N].index == allCells[current].index - 1) {
  252.                     allCells[current].weight = -10;
  253.                     current = current - N;
  254.                 }
  255.                 else {
  256.                     if (allCells[current + N].index == allCells[current].index - 1) {
  257.                         allCells[current].weight = -10;
  258.                         current = current + N;
  259.                     }
  260.                 }
  261.             }
  262.         }
  263.     }
  264.     cout << "It's your way" << endl << endl;
  265.     char free = 249, busy = 254, way = 253;
  266.     for (int i = 0; i < N; i++) {
  267.         for (int j = 0; j < N; j++) {
  268.             if (allCells[j*N + i].y*N + allCells[j*N + i].x == start) cout << setw(3) << 'S';
  269.             else {
  270.                 if (allCells[j*N + i].y*N + allCells[j*N + i].x == finish) cout << setw(3) << 'F';
  271.                 else {
  272.                     if (allCells[j*N + i].weight == -10) cout << setw(3) << way;
  273.                     else {
  274.                         if (allCells[j*N + i].weight < 0) cout << setw(3) << busy;
  275.                         else cout << setw(3) << free;
  276.                     }
  277.                 }
  278.             }
  279.         }
  280.         cout << endl;
  281.     }
  282.  
  283. }
  284.  
  285. void findWay(int N, Cell *allCells, int start, int finish, MyStack& minWay) {
  286.     MyStack resultWay;
  287.     MyStack way;
  288.     MyStack nodes;
  289.  
  290.     way.push(finish);
  291.     int current = finish;
  292.  
  293.     bool flag1 = false;
  294.     bool flag2 = true;
  295.     bool flag3 = true;
  296.  
  297.     int wayWeight = allCells[finish].weight;
  298.     cout << "Way weight: " << wayWeight << endl;
  299.     int minWayWeight = 0;
  300.  
  301.     while (flag2) {
  302.         while (current != start) {
  303.             int number = 0;
  304.             int newcurrent = 0;
  305.  
  306.             if (allCells[current + 1].index == allCells[current].index - 1) { //Справа
  307.                 if (flag1) {
  308.                     flag1 = false;
  309.                 }
  310.                 else {
  311.                     number++;
  312.                     if (number == 1) {
  313.                         wayWeight = wayWeight + allCells[current + 1].weight;
  314.                         cout << wayWeight << " r; ";
  315.                         way.push(current + 1);
  316.                         newcurrent = current + 1;
  317.                     }
  318.                     else {
  319.                         nodes.push(current);
  320.                         nodes.push(wayWeight);
  321.                     }
  322.                 }
  323.             }
  324.             if (allCells[current + N].index == allCells[current].index - 1) { //Снизу
  325.                 if (flag1) {
  326.                     flag1 = false;
  327.                 }
  328.                 else {
  329.                     number++;
  330.                     if (number == 1) {
  331.                         wayWeight = wayWeight + allCells[current + N].weight;
  332.                         cout << wayWeight << " d; ";
  333.                         way.push(current + N);
  334.                         newcurrent = current + N;
  335.                     }
  336.                     else {
  337.                         nodes.push(current);
  338.                         nodes.push(wayWeight);
  339.                     }
  340.                 }
  341.             }
  342.             if ((allCells[current - 1].index == allCells[current].index - 1) && (current % 10 != 0)) { //Слева
  343.                 if (flag1) {
  344.                     flag1 = false;
  345.                 }
  346.                 else {
  347.                     number++;
  348.                     if (number == 1) {
  349.                         wayWeight = wayWeight + allCells[current - 1].weight;
  350.                         cout << wayWeight << " l; ";
  351.                         way.push(current - 1);
  352.                         newcurrent = current - 1;
  353.                     }
  354.                     else {
  355.                         nodes.push(current);
  356.                         nodes.push(wayWeight);
  357.                     }
  358.                 }
  359.             }
  360.             if (allCells[current - N].index == allCells[current].index - 1) { //Сверху
  361.                 if (flag1) {
  362.                     flag1 = false;
  363.                 }
  364.                 else {
  365.                     number++;
  366.                     if (number == 1) {
  367.                         wayWeight = wayWeight + allCells[current - N].weight;
  368.                         cout << wayWeight << " u; ";
  369.                         way.push(current - N);
  370.                         newcurrent = current - N;
  371.                     }
  372.                     else {
  373.                         nodes.push(current);
  374.                         nodes.push(wayWeight);
  375.                     }
  376.                 }
  377.             }
  378.             current = newcurrent;
  379.         }
  380.         cout << endl;
  381.  
  382.         if (flag3) {
  383.             flag3 = false;
  384.             minWayWeight = wayWeight;
  385.             cout << endl;
  386.             cout << "== == == == ==" << endl;
  387.             cout << "Minimum way now: " << minWayWeight << endl;
  388.             MyStack minWay(way);
  389.             minWay.clone(resultWay);
  390.             cout << "== == == == ==" << endl;
  391.             cout << endl;
  392.         }
  393.         else {
  394.             if (wayWeight < minWayWeight) {
  395.                 minWayWeight = wayWeight;
  396.                 cout << endl;
  397.                 cout << "== == == == ==" << endl;
  398.                 cout << "Minimum way now!!!: " << minWayWeight << endl;
  399.                 MyStack minWay(way);
  400.                 minWay.clone(resultWay);
  401.                 cout << "== == == == ==" << endl;
  402.                 cout << endl;
  403.             }
  404.         }
  405.  
  406.         if (!nodes.isEmpty()) {
  407.             cout << "Change node in way ";
  408.             wayWeight = nodes.pop();
  409.             current = nodes.pop();
  410.             int p = allCells[current].index - allCells[start].index;
  411.             for (int i = 0; i < p; i++) {
  412.                 way.del();
  413.             }
  414.             flag1 = true;
  415.         }
  416.         else {
  417.             flag2 = false;
  418.         }
  419.     }
  420.     resultWay.clone(minWay);
  421. }
  422.  
  423.  
  424. int main() {
  425.     const int N = 10;
  426.     int start, finish;
  427.     Cell *allCells = new Cell[N*N];
  428.  
  429.     makeMap(N, allCells);
  430.     printMap(N, allCells);
  431.     makeStartPoint(N, allCells, &start);
  432.     makeFinishPoint(N, allCells, &finish);
  433.     wave(N, allCells, start, finish);
  434.     printWave(N, allCells, start, finish);
  435.    
  436.     MyStack minWay;
  437.     findWay(N, allCells, start, finish, minWay);
  438.     minWay.printMyStack();
  439.     return 0;
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement