int main() { initSolution(); dist.resize(64 * 64, numeric_limits<int>::max()); path.resize(64 * 64); CState start = readState(); CState end = readState(); bfs(start); vector<uint> ans; for (uint v = end.toAbs(); v != startIndex; v = path[v]) ans.push_back (v); for (uint i = ans.size() - 1; i > 0; i--) { pair<int, CPoint> const diff = CState(ans[i - 1]).diff(CState(ans[i])); printf("%d %c%d\n", diff.first, coord2C(diff.second.x), diff.second.y); } postActions(); return 0; }
void bfs(CState const &s) { vector<CPoint> possible; possible.push_back(CPoint(1 ,2)); possible.push_back(CPoint(2, 1)); possible.push_back(CPoint(2, -1)); possible.push_back(CPoint(1, -2)); possible.push_back(CPoint(-1, -2)); possible.push_back(CPoint(-2, -1)); possible.push_back(CPoint(-2, 1)); possible.push_back(CPoint(-1, 2)); queue<CState> q; q.push (s); path[s] = startIndex; dist[s] = 0; while (!q.empty()) { CState const curr = q.front(); q.pop(); for (CPoint const &mov : possible) { // for the first horse CState nextSt1(curr.p1 + mov, curr.p2); if (nextSt1 && dist[curr] + 1 < dist[nextSt1]) { q.push(nextSt1); dist[nextSt1] = dist[curr] + 1; path[nextSt1] = curr.toAbs(); } // for the second one CState nextSt2(curr.p1, curr.p2 + mov); if (nextSt2 && dist[curr] + 1 < dist[nextSt2]) { q.push(nextSt2); dist[nextSt2] = dist[curr] + 1; path[nextSt2] = curr.toAbs(); } } } }
pair<int, CPoint> diff(CState const &next) const { if (toAbs() == next.toAbs()) return make_pair<int, CPoint>(-1, CPoint(1, 1)); if (p1 == next.p1) return make_pair<int, CPoint>(2, CPoint(p2)); else return make_pair<int, CPoint>(1, CPoint(p1)); }