Advertisement
artemgf

Шах

Sep 1st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.62 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. #define mh() make_heap()
  24. #define poph() pop_heap()
  25. #define pushh() push_heap()
  26. #define sor(n) n.begin(), n.end()
  27. #define rsor(n) n.rbegin(), n.rend()
  28. #define mp make_pair
  29. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  30. #define p(T) pair<T,T>
  31. #define znac(l) abs(l)/l
  32. const ll ok = ll(1e9 + 7);
  33.  
  34. ll pole[9][9];
  35. void qbfs(ll x, ll y)
  36. {
  37.     queue<p(ll)>next;
  38.     next.push({ x, y });
  39.     while (!next.empty())
  40.     {
  41.         p(ll) v = next.front();
  42.         next.pop();
  43.         x = v.first;
  44.         y = v.second;
  45.         for (int i = 1; i <= 8; i++)
  46.         {
  47.             if (pole[x][y] + 1 < pole[i][y])
  48.             {
  49.                 next.push({ i,y });
  50.                 pole[i][y] = pole[x][y] + 1;
  51.             }
  52.         }
  53.         for (int i = 1; i <= 8; i++)
  54.         {
  55.             if (pole[x][y] + 1 < pole[x][i])
  56.             {
  57.                 next.push({ x,i });
  58.                 pole[x][i] = pole[x][y] + 1;
  59.             }
  60.         }
  61.         int j;
  62.         for(int i=x, j=y;i<=8&& j <= 8;i++, j++)
  63.             if (pole[x][y] + 1 < pole[i][j])
  64.             {
  65.                 next.push({ i,j });
  66.                 pole[i][j] = pole[x][y] + 1;
  67.             }
  68.         for (int i = x, j = y; i >=0&&j >=0; i--, j--)
  69.             if (pole[x][y] + 1 < pole[i][j])
  70.             {
  71.                 next.push({ i,j });
  72.                 pole[i][j] = pole[x][y] + 1;
  73.             }
  74.         for (int i = x, j = y; i >=0&&j <= 8; i--, j++)
  75.             if (pole[x][y] + 1 < pole[i][j])
  76.             {
  77.                 next.push({ i,j });
  78.                 pole[i][j] = pole[x][y] + 1;
  79.             }
  80.         for (int i = x, j = y; i <= 8&&j >=0; i++, j--)
  81.             if (pole[x][y] + 1 < pole[i][j])
  82.             {
  83.                 next.push({ i,j });
  84.                 pole[i][j] = pole[x][y] + 1;
  85.             }
  86.     }
  87. }
  88.  
  89. void slbfs(ll x, ll y)
  90. {
  91.     queue<p(ll)>next;
  92.     next.push({ x, y });
  93.     while (!next.empty())
  94.     {
  95.         p(ll) v = next.front();
  96.         next.pop();
  97.         x = v.first;
  98.         y = v.second;
  99.         int j;
  100.         for (int i = x, j = y; i <= 8&&j <= 8; i++, j++)
  101.             if (pole[x][y] + 1 < pole[i][j])
  102.             {
  103.                 next.push({ i,j });
  104.                 pole[i][j] = pole[x][y] + 1;
  105.             }
  106.         for (int i = x, j = y; i >= 0&&j >= 0; i--, j--)
  107.             if (pole[x][y] + 1 < pole[i][j])
  108.             {
  109.                 next.push({ i,j });
  110.                 pole[i][j] = pole[x][y] + 1;
  111.             }
  112.         for (int i = x, j = y; i >= 0&&j <= 8; i--, j++)
  113.             if (pole[x][y] + 1 < pole[i][j])
  114.             {
  115.                 next.push({ i,j });
  116.                 pole[i][j] = pole[x][y] + 1;
  117.             }
  118.         for (int i = x, j = y; i <= 8&&j >= 0; i++, j--)
  119.             if (pole[x][y] + 1 < pole[i][j])
  120.             {
  121.                 next.push({ i,j });
  122.                 pole[i][j] = pole[x][y] + 1;
  123.             }
  124.     }
  125. }
  126.  
  127. void kobfs(ll x, ll y)
  128. {
  129.     queue<p(ll)>next;
  130.     next.push({ x, y });
  131.     while (!next.empty())
  132.     {
  133.         p(ll) v = next.front();
  134.         next.pop();
  135.         x = v.first;
  136.         y = v.second;
  137.         for (int i = 1; i <= 8; i++)
  138.         {
  139.             if (pole[x][y] + 1 < pole[i][y])
  140.             {
  141.                 next.push({ i,y });
  142.                 pole[i][y] = pole[x][y] + 1;
  143.             }
  144.         }
  145.         for (int i = 1; i <= 8; i++)
  146.         {
  147.             if (pole[x][y] + 1 < pole[x][i])
  148.             {
  149.                 next.push({ x,i });
  150.                 pole[x][i] = pole[x][y] + 1;
  151.             }
  152.         }
  153.         /*if (x + 2 <= 8 && y + 1 <= 8)
  154.         {
  155.             if (pole[x + 2][y + 1] > pole[x][y] + 1)
  156.             {
  157.                 next.push({ x + 2,y + 1 });
  158.                 pole[x + 2][y + 1] = pole[x][y] + 1;
  159.             }
  160.         }
  161.         if (x + 2 <= 8 && y -1 >=0)
  162.         {
  163.             if (pole[x + 2][y - 1] > pole[x][y] + 1)
  164.             {
  165.                 next.push({ x + 2,y - 1 });
  166.                 pole[x + 2][y - 1] = pole[x][y] + 1;
  167.             }
  168.         }
  169.         if (x - 2 >=0 && y + 1 <= 8)
  170.         {
  171.             if (pole[x - 2][y + 1] > pole[x][y] + 1)
  172.             {
  173.                 next.push({ x - 2,y + 1 });
  174.                 pole[x - 2][y + 1] = pole[x][y] + 1;
  175.             }
  176.         }
  177.         if (x - 2 >=0 && y - 1 >=0)
  178.         {
  179.             if (pole[x - 2][y - 1] > pole[x][y] + 1)
  180.             {
  181.                 next.push({ x - 2,y - 1 });
  182.                 pole[x - 2][y - 1] = pole[x][y] + 1;
  183.             }
  184.         }
  185.         if (y + 2 <= 8 && x + 1 <= 8)
  186.         {
  187.             if (pole[x + 1][y + 2] > pole[x][y] + 1)
  188.             {
  189.                 next.push({ x + 1,y + 2 });
  190.                 pole[x + 1][y + 2] = pole[x][y] + 1;
  191.             }
  192.         }
  193.         if (y + 2 <= 8 && x - 1 >= 0)
  194.         {
  195.             if (pole[x - 1][y + 2] > pole[x][y] + 1)
  196.             {
  197.                 next.push({ x - 1,y + 2 });
  198.                 pole[x - 1][y + 2] = pole[x][y] + 1;
  199.             }
  200.         }
  201.         if (y - 2 >=0 && x + 1 <= 8)
  202.         {
  203.             if (pole[x + 1][y - 2] > pole[x][y] + 1)
  204.             {
  205.                 next.push({ x + 1,y - 2 });
  206.                 pole[x + 1][y - 2] = pole[x][y] + 1;
  207.             }
  208.         }
  209.         if (y - 2 >=0 && x - 1 >= 0)
  210.         {
  211.             if (pole[x - 1][y - 2] > pole[x][y] + 1)
  212.             {
  213.                 next.push({ x - 1,y - 2 });
  214.                 pole[x - 1][y - 2] = pole[x][y] + 1;
  215.             }
  216.         }*/
  217.     }
  218. }
  219.  
  220. void kbfs(ll x, ll y)
  221. {
  222.     queue<p(ll)>next;
  223.     next.push({ x, y });
  224.     while (!next.empty())
  225.     {
  226.         p(ll) v = next.front();
  227.         next.pop();
  228.         x = v.first;
  229.         y = v.second;
  230. if (x + 2 <= 8 && y + 1 <= 8)
  231.         {
  232.             if (pole[x + 2][y + 1] > pole[x][y] + 1)
  233.             {
  234.                 next.push({ x + 2,y + 1 });
  235.                 pole[x + 2][y + 1] = pole[x][y] + 1;
  236.             }
  237.         }
  238.         if (x + 2 <= 8 && y -1 >=0)
  239.         {
  240.             if (pole[x + 2][y - 1] > pole[x][y] + 1)
  241.             {
  242.                 next.push({ x + 2,y - 1 });
  243.                 pole[x + 2][y - 1] = pole[x][y] + 1;
  244.             }
  245.         }
  246.         if (x - 2 >=0 && y + 1 <= 8)
  247.         {
  248.             if (pole[x - 2][y + 1] > pole[x][y] + 1)
  249.             {
  250.                 next.push({ x - 2,y + 1 });
  251.                 pole[x - 2][y + 1] = pole[x][y] + 1;
  252.             }
  253.         }
  254.         if (x - 2 >=0 && y - 1 >=0)
  255.         {
  256.             if (pole[x - 2][y - 1] > pole[x][y] + 1)
  257.             {
  258.                 next.push({ x - 2,y - 1 });
  259.                 pole[x - 2][y - 1] = pole[x][y] + 1;
  260.             }
  261.         }
  262.         if (y + 2 <= 8 && x + 1 <= 8)
  263.         {
  264.             if (pole[x + 1][y + 2] > pole[x][y] + 1)
  265.             {
  266.                 next.push({ x + 1,y + 2 });
  267.                 pole[x + 1][y + 2] = pole[x][y] + 1;
  268.             }
  269.         }
  270.         if (y + 2 <= 8 && x - 1 >= 0)
  271.         {
  272.             if (pole[x - 1][y + 2] > pole[x][y] + 1)
  273.             {
  274.                 next.push({ x - 1,y + 2 });
  275.                 pole[x - 1][y + 2] = pole[x][y] + 1;
  276.             }
  277.         }
  278.         if (y - 2 >=0 && x + 1 <= 8)
  279.         {
  280.             if (pole[x + 1][y - 2] > pole[x][y] + 1)
  281.             {
  282.                 next.push({ x + 1,y - 2 });
  283.                 pole[x + 1][y - 2] = pole[x][y] + 1;
  284.             }
  285.         }
  286.         if (y - 2 >=0 && x - 1 >= 0)
  287.         {
  288.             if (pole[x - 1][y - 2] > pole[x][y] + 1)
  289.             {
  290.                 next.push({ x - 1,y - 2 });
  291.                 pole[x - 1][y - 2] = pole[x][y] + 1;
  292.             }
  293.         }
  294. }
  295. }
  296. int main()
  297. {
  298.     //files;
  299.  
  300.     string s;
  301.     cin >> s;
  302.     string h;
  303.     cin >> h;
  304.     ll x = h[0] - 'a' + 1;
  305.     ll y = h[1] - '0';
  306.     for (int i = 1; i <= 8; i++)
  307.     {
  308.         for (int j = 1; j <= 8; j++)
  309.         {
  310.             pole[i][j] = INT_MAX;
  311.         }
  312.     }
  313.     pole[x][y] = 0;
  314.     if (s == "queen")
  315.     {
  316.         qbfs(x, y);
  317.     }
  318.     else
  319.         if (s == "bishop")
  320.         {
  321.             slbfs(x, y);
  322.         }
  323.         else
  324.         {
  325.             if(s=="hours")
  326.                 kbfs(x,y);
  327. else
  328.             kobfs(x, y);
  329.         }
  330.     for (int i = 8; i >= 1; i--)
  331.     {
  332.         for (int j = 1; j <= 8; j++)
  333.         {
  334.             if (pole[j][i] == INT_MAX)
  335.                 cout << "*";
  336.             else
  337.                 cout << pole[j][i];
  338.         }
  339.         cout << endl;
  340.     }
  341.     return 0;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement