2 条题解

  • 0
    @ 2024-12-9 11:34:59
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1E9 + 7;
    string target = "12345678x";
    int d[][2] = {1, 0, 0, -1, 0, 1, -1, 0};
    string pr = "dlru";
    
    void bfs(string s) {
        map<string, pair<string, int>> mp;
        queue<string> q;
        q.push(s), mp[s] = {"", 1};
        while (q.size()) {
            auto u = q.front(); q.pop();
            if (u == target) {
                cout << mp[u].first << "\n"; return;
            }
            int p = u.find('x'), x = p / 3, y = p % 3;
            for (int i = 0; i < 4; i++) {
                int a = x + d[i][0], b = y + d[i][1];
                if (a < 0 || a >= 3 || b < 0 || b >= 3) continue;
                string v(u); swap(v[p], v[a * 3 + b]);
                if (mp.count(v)) continue;
                q.push(v), mp[v] = {mp[u].first + pr[i], mp[u].second + 1};
            }
        }
        cout<<"unsolvable\n";
    }
    int main(){
        string s;
        while(getline(cin, s)){
            int id = s.find(" ");
            while (id != -1) {
                s.erase(id, 1);
                id = s.find(" ");
            }
            // cout<<s<<endl;
            bfs(s);
        }
    }
    
    • 0
      @ 2024-12-9 9:12:36

      #include <bits/stdc++.h>
      using namespace std;
      #define PIS pair<int, string>
      typedef long long LL;
      const int N = 1e6 + 10;
      string target = "12345678x";
      
      int h(string s) {
          int res = 0;
          for (int i = 0; i < s.size(); i++)
              if (s[i] != 'x') {
                  int t = s[i] - '1';
                  res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);
              }
          return res;
      }
      string bfs_Astar(string s) {
          int cnt = 0;
          for (int i = 0; i < s.size(); i++)
              for (int j = i + 1; j < s.size(); j++)
                  if (s[i] != 'x' && s[j] != 'x' && s[i] > s[j])
                      cnt++;
          if (cnt % 2)
              return "unsolvable";
              
          int d[][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
          string op = "udlr";
          unordered_map<string, PIS> path;
          unordered_map<string, int> dist;
          priority_queue<PIS, vector<PIS>, greater<PIS>> q;
          q.push({h(s), s}), path[s] = {0, ""}, dist[s] = 0;
          while (q.size()) {
              auto it = q.top();  q.pop();
              int w = it.first;
              string u = it.second;
              if (u == target)
                  return path[u].second;
      
              int p = u.find('x');
              for (int i = 0; i < 4; i++) {
                  int x = p / 3 + d[i][0], y = p % 3 + d[i][1];
                  if (x < 0 || x >= 3 || y < 0 || y >= 3)
                      continue;
                  string v = u;
                  swap(v[p], v[x * 3 + y]);
                  if (!path.count(v)) {
                      dist[v] = dist[u] + 1;
                      q.push({dist[v] + h(v), v});
                      path[v] = {path[u].first + 1, path[u].second + op[i]};
                  }
              }
          }
          return "unsolvable";
      }
      // x 1 2 3 6 4 8 5 7
      // rrddlulurdrulldrrdllurrd
      
      int main() {
          string s;
          while (getline(cin, s)) {
              while (s.find(" ") != string::npos)
                  s.erase(s.find(" "), 1);
              // cout << s << endl;
              // if(s.size()<5) break;
              // cout << bfs(s) << endl;
              cout << bfs_Astar(s) << endl;
          }
          return 0;
      }
      
      • 1

      信息

      ID
      527
      时间
      2000ms
      内存
      32MiB
      难度
      5
      标签
      递交数
      159
      已通过
      60
      上传者