http://bailian.openjudge.cn/xly2019/C/
本体无法提交测试。样例已通过,可能TLE。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<iostream> #include<cstdio> #include<queue> #include<string> using namespace std; struct queueData { int p; string s; queueData(int p,string s):p(p),s(s){} }; void bfs(int src, int dst) { queue<queueData> q; q.push(queueData(src, "")); while (!q.empty()) { queueData cur = q.front(); q.pop(); if (cur.p == dst) { cout << cur.s.size() << "\n"; cout << cur.s << "\n"; return; } q.push(queueData(cur.p * 3, cur.s + "H")); q.push(queueData(cur.p / 2, cur.s + "O")); } } int main() { int n, m; while (cin >> n >> m) { if (n == 0 && m == 0)break; else bfs(n, m); } } |