ElenaR1

drugite funkcii na horsei class horseConfig

Nov 20th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. #define MAX_SIZE 8
  8.  
  9. bool board[MAX_SIZE][MAX_SIZE] = { false };
  10.  
  11. void printBoard(int size, ostream &out)
  12. {
  13.     out << "+++++++++++++++++++\n";
  14.     for (int r = 0; r < size; r++)
  15.     {
  16.         for (int c = 0; c < size; c++)
  17.         {
  18.             if (board[r][c])
  19.                 out << "H ";
  20.             else
  21.                 out << "_ ";
  22.  
  23.         }
  24.         out << endl;
  25.     }
  26.     out << "+++++++++++++++++++ \n";
  27. }
  28.  
  29.  
  30.  
  31. #define OPER_PLACE_HORSE 0
  32. #define OPER_REMOVE_HORSE 1
  33. #define OPER_FIND_SOLUTION 2
  34.  
  35. struct Operation
  36. {
  37.     int operType;
  38.     int k;
  39.     int size;
  40.     int r;
  41.     int c;
  42.     Operation(int _t, int _k, int _s) :operType(_t), k(_k), size(_s) {}
  43.     Operation(int _t, int _k, int _s, int _r, int _c)
  44.         :operType(_t), k(_k), size(_s), r(_r), c(_c) {}
  45.     //Operation (){}
  46. };
  47. bool hasHorse(int r, int c, int size)
  48. {
  49.     return r >= 0 &&
  50.         c >= 0 &&
  51.         r < size &&
  52.         c < size &&
  53.         board[r][c];
  54. }
  55.  
  56. bool canPlaceHorse(int r, int c, int size)
  57. {
  58.     return !hasHorse(r - 2, c - 1, size) &&
  59.         !hasHorse(r - 2, c + 1, size) &&
  60.         !hasHorse(r - 1, c - 2, size) &&
  61.         !hasHorse(r - 1, c + 2, size) &&
  62.         !hasHorse(r + 1, c - 2, size) &&
  63.         !hasHorse(r + 1, c + 2, size) &&
  64.         !hasHorse(r + 2, c - 1, size) &&
  65.         !hasHorse(r + 2, c + 1, size) &&
  66.         !hasHorse(r, c, size);
  67. }
  68.  
  69. void configStack(int k, int size, ostream &out)
  70. {
  71.     stack<Operation> operations;
  72.  
  73.     operations.push(Operation(OPER_FIND_SOLUTION, k, size));
  74.  
  75.     while (!operations.empty())
  76.     {
  77.         Operation topOperation = operations.top();
  78.         int k = topOperation.k;
  79.         int size = topOperation.size;
  80.         operations.pop();
  81.  
  82.         if (k == 0)
  83.         {
  84.             printBoard(size, out);
  85.         }
  86.         else if (topOperation.operType == OPER_PLACE_HORSE)
  87.         {
  88.             board[topOperation.r][topOperation.c] = true;
  89.         }
  90.         else if (topOperation.operType == OPER_REMOVE_HORSE)
  91.         {
  92.             board[topOperation.r][topOperation.c] = false;
  93.         }
  94.         else
  95.         {
  96.             for (int r = 0; r < size; r++)
  97.             {
  98.                 for (int c = 0; c < size; c++)
  99.                 {
  100.                     if (canPlaceHorse(r, c, size))
  101.                     {
  102.                         operations.push(Operation(OPER_REMOVE_HORSE, 1, size, r, c));
  103.                         operations.push(Operation(OPER_FIND_SOLUTION, k - 1, size));
  104.                         operations.push(Operation(OPER_PLACE_HORSE, 1, size, r, c));
  105.                     }
  106.                 }
  107.             }
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.     }
  114. }
  115.  
  116.  
  117.  
  118. void config(int k, int size, ostream&out)
  119. {
  120.     if (k == 0)
  121.     {
  122.         printBoard(size, out);
  123.         return;
  124.     }
  125.  
  126.     for (int r = 0; r < size; r++)
  127.     {
  128.         for (int c = 0; c < size; c++)
  129.         {
  130.             if (canPlaceHorse(r, c, size))
  131.             {
  132.                 board[r][c] = true;
  133.                 config(k - 1, size, out);
  134.                 board[r][c] = false;
  135.             }
  136.         }
  137.     }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. int main()
  144. {
  145.     //makeConfig(cout, 4, 0, 0, 0, 3);
  146.     //makeConfig(cout, 4, 0, 3, 3, 3);
  147.     //makeConfig(cout, 4, 1, 1, 1, 3);
  148.     /*ofstream out;
  149.     out.open("data2.txt");
  150.  
  151.     configStack(2, 4, out);
  152.     out.close();
  153.     ofstream out;
  154.     out.open("data3.txt");
  155.  
  156.     config(2, 4, out);
  157.     out.close();*/
  158.     /*ofstream out;
  159.     out.open("3x3table.txt");
  160.  
  161.     configStack(2, 3, out);
  162.     out.close();*/
  163.    
  164. }
  165.  
  166.  
  167.  
  168.  
  169. class:
  170. #include <iostream>
  171. #include <stack>
  172. #include<fstream>
  173. #include<assert.h>
  174.  
  175. using namespace std;
  176.  
  177. #include <iostream>
  178. #include<iomanip>
  179.  
  180. #include <stack>
  181.     using namespace std;
  182.  
  183. #define MAX_SIZE 8
  184.  
  185. bool board[MAX_SIZE][MAX_SIZE] = { false };
  186. #define OPER_PLACE_HORSE 0
  187. #define OPER_REMOVE_HORSE 1
  188. #define OPER_FIND_SOLUTION 2
  189. struct Operation {
  190.     int operType;
  191.     int k;
  192.     int size;
  193.     int r;
  194.     int c;
  195.     Operation(int _t, int _k, int _s) :operType(_t), k(_k), size(_s) {}//k is the number of horses
  196.     Operation(int _t, int _k, int _s, int _r, int _c)
  197.         :operType(_t), k(_k), size(_s), r(_r), c(_c) {}
  198. };
  199. class HorseConfig {
  200. private:
  201.     int k;
  202.     int size;
  203. public:
  204.     HorseConfig(int kk, int s) :k(kk), size(s) {}
  205.     void printBoard()
  206.     {
  207.         cout << "+++++++++++++++++++\n";
  208.         for (int r = 0; r < size; r++)
  209.         {
  210.             for (int c = 0; c < size; c++)
  211.             {
  212.                 if (board[r][c])
  213.                     cout << "H ";
  214.                 else
  215.                     cout << "_ ";
  216.  
  217.             }
  218.             cout << endl;
  219.         }
  220.         cout << "+++++++++++++++++++\n";
  221.     }
  222.     bool hasHorse(int r, int c)
  223.     {
  224.         return r >= 0 &&
  225.             c >= 0 &&
  226.             r < size &&
  227.             c < size &&
  228.             board[r][c];//da ni e true t.e da ima neshto na board-a
  229.  
  230.     }
  231.  
  232.     bool canPlaceHorse(int r, int c)
  233.     {
  234.         return !hasHorse(r - 2, c - 1) &&
  235.             !hasHorse(r - 2, c + 1) &&
  236.             !hasHorse(r - 1, c - 2) &&
  237.             !hasHorse(r - 1, c + 2) &&
  238.             !hasHorse(r + 1, c - 2) &&
  239.             !hasHorse(r + 1, c + 2) &&
  240.             !hasHorse(r + 2, c - 1) &&
  241.             !hasHorse(r + 2, c + 1) &&
  242.             !hasHorse(r, c);
  243.     }
  244.     void configStack()
  245.     {
  246.         stack<Operation> operations;
  247.         operations.push(Operation(OPER_FIND_SOLUTION, k, size));
  248.         while (!operations.empty())
  249.         {
  250.             Operation topOperation = operations.top();
  251.             int k = topOperation.k;//tezi k i size koito sme podali
  252.                                    //int size = topOperation.size;
  253.             operations.pop();
  254.  
  255.             if (k == 0)//if there aren't any horses
  256.             {
  257.                 printBoard();
  258.             }
  259.             else if (topOperation.operType == OPER_PLACE_HORSE)
  260.             {
  261.                 board[topOperation.r][topOperation.c] = true;
  262.             }
  263.             else if (topOperation.operType == OPER_REMOVE_HORSE)
  264.             {
  265.                 board[topOperation.r][topOperation.c] = false;
  266.             }
  267.             else//if it is find solution
  268.             {
  269.                 for (size_t r = 0; r < size; r++)
  270.                 {
  271.                     for (size_t c = 0; c < size; c++)
  272.                     {
  273.                         if (canPlaceHorse(r, c))
  274.                         {
  275.                             operations.push(Operation(OPER_REMOVE_HORSE, 1, size, r, c));
  276.                             operations.push(Operation(OPER_FIND_SOLUTION, k - 1, size));
  277.                             operations.push(Operation(OPER_PLACE_HORSE, 1, size, r, c));
  278.                         }
  279.                     }
  280.                 }
  281.             }
  282.         }
  283.     }
  284.     void nextConfig(int r1, int c1, int r2, int c2)
  285.     {
  286.         assert(r1 != r2&&c1 != 2);
  287.         board[r1][c1] = true;
  288.         board[r2][c2] = true;
  289.         if (c2 + 1 < size)
  290.         {          
  291.             board[r2][c2] = false;
  292.             board[r2][c2 + 1] = true;
  293.         }
  294.         else if (r2 + 1<size)
  295.         {
  296.            
  297.             board[r2][c2] = false;
  298.             board[r2 + 1][0] = true;
  299.         }
  300.         else if (c1 + 1<size)
  301.         {
  302.             board[r1][c1] = false;
  303.             board[r2][c2] = false;
  304.             board[r1][c1 + 1] = true;
  305.             board[0][0] = true;
  306.         }
  307.         else if (r1 + 1<size)//ako e na 3ta kolona
  308.         {
  309.             board[r1][c1] = false;
  310.             board[r2][c2] = false;
  311.             board[r1 + 1][0] = true;
  312.             board[0][0] = true;
  313.         }
  314.         printBoard();
  315.         board[MAX_SIZE][MAX_SIZE] = { false };
  316.     }
  317.    
  318.     bool noMoreConfigs(int r1, int c1,int r2,int c2, int size)
  319.     {
  320.         if (r1+1 >= size&&c1+1>=size&&r2+1>=size&&c2+2>=size)
  321.         {
  322.             return true;
  323.         }
  324.         else
  325.         {
  326.             return false;
  327.         }
  328.     }
  329.    
  330. };
  331. int main()
  332. {
  333.     HorseConfig a(2, 4);
  334.     //a.configStack();
  335.    
  336.     //a.nextConfig(0, 0, 0, 3);//no ne  2 ednovremenno
  337.     //a.nextConfig(2, 1, 2, 2);
  338.     //a.nextConfig(1, 1, 1, 3);
  339.     //a.nextConfig(1, 1, 3, 3);
  340.     a.nextConfig(1, 3, 3, 3);
  341.     //a.nextConfig(1, 3, 1, 3);//dava assertion failure kakto iskame
  342.     cout << a.noMoreConfigs(1, 1, 1, 3, 4) << endl;
  343.     cout << a.noMoreConfigs(3, 3, 3, 2, 4) << endl;
  344.    
  345.    
  346.     return 0;
  347. }
Add Comment
Please, Sign In to add comment