diff --git a/11513.cpp b/11513.cpp new file mode 100644 index 0000000..456c0a3 --- /dev/null +++ b/11513.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +using namespace std; + +int nums[9]; +map visited; +map result; + +void precalculate() { + pair tmp; + queue< pair > q; + + tmp.first = "123456789"; + tmp.second = ""; + visited[tmp.first] = true; + + q.push(tmp); + + while (!q.empty()) { + tmp = q.front(); + q.pop(); + + result[tmp.first] = tmp.second; + + for (int i = 0; i < 3; i++) { + swap(tmp.first[3+i], tmp.first[6+i]); + swap(tmp.first[0+i], tmp.first[3+i]); + + if (visited.find(tmp.first) == visited.end()) { + visited[tmp.first] = true; + q.push(make_pair(tmp.first, (string("V") + (i == 0 ? "1" : i == 1 ? "2" : "3")) + tmp.second)); + } + + swap(tmp.first[0+i], tmp.first[3+i]); + swap(tmp.first[3+i], tmp.first[6+i]); + } + + for (int i = 0; i < 3; i++) { + swap(tmp.first[3*i+0], tmp.first[3*i+1]); + swap(tmp.first[3*i+1], tmp.first[3*i+2]); + + if (visited.find(tmp.first) == visited.end()) { + visited[tmp.first] = true; + q.push(make_pair(tmp.first, string("H") + (i == 0 ? "1" : i == 1 ? "2" : "3") + tmp.second)); + } + + swap(tmp.first[3*i+1], tmp.first[3*i+2]); + swap(tmp.first[3*i+0], tmp.first[3*i+1]); + } + } +} + +int main(void) { + stringstream ss; + + precalculate(); + + while (cin >> nums[0]) { + if (nums[0] == 0) + break; + + for (int i = 1; i < 9; i++) + cin >> nums[i]; + + for (int i = 0; i < 9; i++) + ss << nums[i]; + + string tmp = ss.str(); + ss.str(""); + ss.clear(); + + if (result.find(tmp) == result.end()) + cout << "Not solvable" << endl; + else + cout << result[tmp].size() / 2 << " " << result[tmp] << endl; + } + + return 0; +}