Advertisement
Emiliatan

a634

Apr 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. /* a634            */
  2. /* AC (3ms, 376KB) */
  3. #include <bits/stdc++.h>
  4. #define in(x,y) ((x) >= 1 && (x) <= 8 && (y) >= 1 && (y) <= 8)
  5. #define abs(x) ((x) < 0 ? (~(x) + 1) : (x))
  6.  
  7. using namespace std;
  8.  
  9. int place_[9][9];
  10. short si, sj, ei, ej, ansP = 0, offseti[] = {-1, -2, 1, 2}, offsetj[] = {-2, -1, 2, 1}, offset[2][4] = {{-2, -1, 1, 2},{-1, 1, -2, 2}};
  11. set<string> ans;
  12.  
  13. inline short _dehash(int n)
  14. {
  15.     return n + 'a' - 1;
  16. }
  17. void dfs(int nowi, int nowj, stack<pair<int, int> > path)
  18. {
  19.     if(nowi == si && nowj == sj)
  20.     {
  21.         string s = "Solution:";
  22.         while(path.size())
  23.         {
  24.             s.push_back(' ');
  25.             s.push_back((char)_dehash(path.top().first));
  26.             s.push_back((char)('0' + path.top().second));
  27.             path.pop();
  28.         }
  29.         ans.insert(s);
  30.         return;
  31.     }
  32.     short nexi, nexj;
  33.     for(int indexi0 = 0; indexi0 < 4; ++indexi0)
  34.     {
  35.         short flag = abs(offset[0][indexi0]);
  36.         for(int indexj1 = (flag == 2 ? 0 : 2); indexj1 < (flag == 2 ? 2 : 4); ++indexj1)
  37.         {
  38.             nexi = nowi + offset[0][indexi0];
  39.             nexj = nowj + offset[1][indexj1];
  40.             if(in(nexi, nexj) && place_[nexi][nexj] == place_[nowi][nowj] - 1)
  41.             {
  42.                 if(place_[nexi][nexj] == 0 && nexi != si && nexj != sj) continue;
  43.                 assert(nexi >= 1 && nexi <= 8 && nexj >= 1 && nexj <= 8);
  44.                 path.emplace(make_pair(nexi, nexj));
  45.                 dfs(nexi, nexj, path);
  46.                 path.pop();
  47.             }
  48.         }
  49.     }
  50. }
  51. void bfs()
  52. {
  53.     queue<pair<int, int> > qu;
  54.     stack<pair<int, int> > tmp;
  55.     qu.emplace(make_pair(si, sj));
  56.     place_[si][sj] = 0;
  57.     while(qu.size())
  58.     {
  59.         int i = qu.front().first, j = qu.front().second; qu.pop();
  60.         if(i == ei && j == ej) break;
  61.         for(int indexi = 0; indexi < 4; ++indexi)
  62.             for(int indexj = (indexi & 1); indexj < 4; indexj += 2)
  63.             {
  64.                 int nexi = i + offseti[indexi], nexj = j + offsetj[indexj];
  65.                 if((nexi != si || nexj != sj) && in(nexi, nexj) && place_[nexi][nexj] == 0)
  66.                 {
  67.                     assert(nexi >= 1 && nexi <= 8 && nexj >= 1 && nexj <= 8);
  68.                     qu.emplace(make_pair(nexi, nexj));
  69.                     place_[nexi][nexj] = place_[i][j] + 1;
  70.                 }
  71.             }
  72.     }
  73.     printf("The shortest solution is %d move(s).\n", place_[ei][ej]);
  74.     tmp.emplace(ei, ej);
  75.     dfs(ei, ej, tmp);
  76.     for(auto it : ans)
  77.         cout<< it << '\n';
  78.    
  79. }
  80.  
  81. int main()
  82. {
  83.     char s[10];
  84.     while(~scanf("%s", s))
  85.     {
  86.         ans.clear();
  87.         memset(place_, 0, sizeof(place_));
  88.         ansP = 0;
  89.         si = s[0] - 'a' + 1;
  90.         sj = s[1] - '0';
  91.         scanf("%s", s);
  92.         ei = s[0] - 'a' + 1;
  93.         ej = s[1] - '0';
  94.         assert(si >= 1 && si <= 8 && sj >= 1 && sj <= 8);
  95.         assert(ei >= 1 && ei <= 8 && ej >= 1 && ej <= 8);
  96.         while(scanf("%s", s) && s[0] != 'x')
  97.         {
  98.             assert((s[0] >= 'a' && s[0] <= 'h'));
  99.             assert((s[1] >= '1' && s[1] <= '8'));
  100.             place_[s[0] - 'a' + 1][s[1] - '0'] = -1;
  101.         }
  102.         if((si == ei && sj == ej)) printf("The shortest solution is 0 move(s).\nSolution: %c%d\n", _dehash(si), sj);
  103.         else bfs();
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement