Miyago147852

UVa 10422

Jun 17th, 2022
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. #define endll "\n\n"
  4. #define IO ios_base::sync_with_stdio(0);cin.tie(0)
  5. #define ll long long
  6. #define inf 0x3f3f3f3f
  7. #define MAXN maxn
  8. #define MODN modn
  9.  
  10. using namespace std;
  11.  
  12. string ed  = "111110111100 110000100000";
  13. set<string> book;
  14. class node {
  15.     public:
  16.         node(string s="", int cnt = 0): s(s), cnt(cnt) {}
  17.         string s;
  18.         int cnt;
  19. };
  20.  
  21. int bfs(node st){
  22.     queue<node> que;
  23.     que.push(st);
  24.     while (!que.empty()){
  25.         node cur = que.front();
  26.         que.pop();
  27.         if (cur.s == ed) return cur.cnt;
  28.         if (cur.cnt>9) continue;
  29.         int canGo = cur.s.find(' '), cRow=canGo/5, cCol=canGo%5;
  30.         for (int i=-2; i<=2; i++){
  31.             for (int j=-2; j<=2; j++){
  32.                 if (abs(i)==abs(j) || !i || !j) continue;
  33.                 if (cRow+i<0 || cRow+i>4 || cCol+j<0 || cCol+j>4) continue;
  34.                 int pos = (cRow+i)*5+cCol+j;
  35.                 node tmp = cur;
  36.                 swap(tmp.s[pos], tmp.s[canGo]);
  37.                 if (book.count(tmp.s)) continue;
  38.                 book.insert(tmp.s);
  39.                 tmp.cnt++;
  40.                 que.push(tmp);
  41.                 if (tmp.s == ed) return tmp.cnt;
  42.             }
  43.         }
  44.     }
  45.     return 11;
  46. }
  47.  
  48. signed main(){
  49.     IO;
  50.     #ifdef DEBUG
  51.         freopen("p.in", "r", stdin);
  52.         freopen("p.out", "w", stdout);
  53.     #endif
  54.  
  55.     int n;
  56.     string tmp;
  57.     cin>>n; getline(cin, tmp);
  58.     while (n--) {
  59.         string s;
  60.         book.clear();
  61.         for (int i=0;i<5;i++){
  62.             getline(cin, tmp);
  63.             tmp = tmp.substr(0, 5);
  64.             s+=tmp;
  65.         }
  66.         node st(s, 0);
  67.         int res = bfs(st);
  68.         if (res<11) cout<<"Solvable in "<<res<<" move(s)."<<endl;
  69.         else cout<<"Unsolvable in less than 11 move(s)."<<endl;
  70.  
  71.     }
  72.  
  73.     return EXIT_SUCCESS;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment