Advertisement
artemgf

Число ходов для шахматных фигур

Jan 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 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. int main()
  220. {
  221.     files;
  222.  
  223.     string s;
  224.     cin >> s;
  225.     string h;
  226.     cin >> h;
  227.     ll x = h[0] - 'a' + 1;
  228.     ll y = h[1] - '0';
  229.     for (int i = 1; i <= 8; i++)
  230.     {
  231.         for (int j = 1; j <= 8; j++)
  232.         {
  233.             pole[i][j] = INT_MAX;
  234.         }
  235.     }
  236.     pole[x][y] = 0;
  237.     if (s == "queen")
  238.     {
  239.         qbfs(x, y);
  240.     }
  241.     else
  242.         if (s == "bishop")
  243.         {
  244.             slbfs(x, y);
  245.         }
  246.         else
  247.         {
  248.             kobfs(x, y);
  249.         }
  250.     for (int i = 8; i >= 1; i--)
  251.     {
  252.         for (int j = 1; j <= 8; j++)
  253.         {
  254.             if (pole[j][i] == INT_MAX)
  255.                 cout << "*";
  256.             else
  257.                 cout << pole[j][i];
  258.         }
  259.         cout << endl;
  260.     }
  261.     return 0;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement