AC代码: #include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <algorithm> #include <cstdint> using namespace std; typedef pair<int, int> pii;
int R, C, w[15][15]; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; char dirc[] = {‘U’, ‘D’, ‘L’, ‘R’};
uint64_t encode(const vector<pii>& snake) { uint64_t res = 0; for (auto &pr : snake) { int x = pr.first, y = pr.second; res = (res << 8) | ((uint64_t)x << 4 | y); } return res; }
// 【修复!】检查整条蛇所有点互不重复,不是只检查头 bool valid(const vector<pii>& s) { for (size_t i = 0; i < s.size(); ++i) { for (size_t j = i + 1; j < s.size(); ++j) { if (s[i] == s[j]) return false; } } return true; }
struct Node { int cost; uint64_t key; int mask; Node(int c, uint64_t k, int m) : cost(c), key(k), mask(m) {} bool operator>(const Node& b) const { return cost > b.cost; } };
struct PreInfo { uint64_t last_key; int last_mask; char ch; };
unordered_map<uint64_t, vector<pii>> cache; vector<unordered_map<uint64_t, PreInfo>> pre;
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> R >> C; for (int i = 1; i <= R; ++i) { string s; cin >> s; for (int j = 1; j <= C; ++j) w[i][j] = s[j-1] - ‘0’; } vector<pii> init(4); for (int i = 0; i < 4; ++i) cin >> init[i].first >> init[i].second; int N; cin >> N; vector<pii> food(N); unordered_map<int, int> fid; for (int i = 0; i < N; ++i) { cin >> food[i].first >> food[i].second; fid[food[i].first * 15 + food[i].second] = i; } int full = 1 << N; pre.resize(full); vector<unordered_map<uint64_t, int>> vis(full); priority_queue<Node, vector<Node>, greater<Node>> q;
uint64_t stk = encode(init);
cache[stk] = init;
q.emplace(0, stk, 0);
vis[0][stk] = 0;
int ansc = -1;
uint64_t ans_key;
int ans_mask = full - 1;
while (!q.empty()) {
auto now = q.top(); q.pop();
int c = now.cost;
uint64_t k = now.key;
int mask = now.mask;
if (mask == ans_mask) {
ansc = c; ans_key = k; break;
}
if (vis[mask][k] < c) continue;
auto& sn = cache[k];
int hx = sn[0].first, hy = sn[0].second;
for (int d = 0; d < 4; d++) {
int nx = hx + dx[d], ny = hy + dy[d];
if (nx < 1 || nx > R || ny < 1 || ny > C || w[nx][ny] == 0) continue;
vector<pii> ns;
int nmask = mask;
int fkey = nx * 15 + ny;
if (fid.count(fkey) && !(mask & (1 << fid[fkey]))) {
ns.emplace_back(nx, ny);
for (auto &p : sn) ns.push_back(p);
nmask = mask | (1 << fid[fkey]);
} else {
ns.emplace_back(nx, ny);
for (size_t i = 0; i + 1 < sn.size(); i++) ns.push_back(sn[i]);
}
if (!valid(ns)) continue;
uint64_t nk = encode(ns);
int add = abs(w[hx][hy] - w[nx][ny]) + 1;
int nc = c + add;
if (!vis[nmask].count(nk) || nc < vis[nmask][nk]) {
vis[nmask][nk] = nc;
cache[nk] = ns;
pre[nmask][nk] = {k, mask, dirc[d]};
q.emplace(nc, nk, nmask);
}
}
}
if (ansc == -1) {
cout << "No solution.\n";
return 0;
}
string path;
uint64_t ck = ans_key;
int cm = ans_mask;
while (ck != stk || cm != 0) {
PreInfo p = pre[cm][ck];
path.push_back(p.ch);
ck = p.last_key;
cm = p.last_mask;
}
reverse(path.begin(), path.end());
cout << ansc << '\n' << path << '\n';
return 0;
}