#include #include #include #include #include #include #include #include #include #define myforeach(c,it,type) for(type::iterator it = c.begin(); it != c.end(); it++) using namespace std; struct idou{ int direction; int position; idou(){}; idou(int dir, int pos):direction(dir), position(pos){}; }; typedef map board_impl; typedef vector history; struct board{ history recent; board_impl b; board(){}; board(const history &h, const board_impl &b):recent(h), b(b){}; }; inline bool operator < (const board &lhs, const board &rhs) { return lhs.b < rhs.b; } board board_of_string(string s) { board_impl bi; for(int i = 0; i < s.length(); i++){ if(isdigit(s[i])){ int digit = s[i] - '0'; bi[i] = digit; } } return board(history(), bi); } void addPosition(board &b, int newPosition, int moveNumber) { if(b.b.count(newPosition) != 0){ int sum = b.b[newPosition] + moveNumber; sum %= 10; b.b[newPosition] = sum; if(sum == 0){ b.b.erase(newPosition); } }else{ b.b[newPosition] = moveNumber; } } void print_history(board &b) { myforeach(b.recent, it, history){ int dir = it -> direction; int pos = it -> position; cout << (dir == 1? "R" : "L") << (char)(pos + 'a') << endl; } } void shortest_route(board initial) { queue toBeProcessed; toBeProcessed.push(initial); set visited; while(!toBeProcessed.empty()){ board curr = toBeProcessed.front(); toBeProcessed.pop(); if(visited.count(curr.b) != 0){ continue; } visited.insert(curr.b); if(curr.b.empty()){ print_history(curr); return; } myforeach(curr.b, it, board_impl){ int movePosition = it -> first; int moveNumber = it -> second; board_impl without_it(curr.b); without_it.erase(movePosition); int newPosition; if(movePosition != 9) { newPosition = movePosition + moveNumber; if(newPosition >= 10){ newPosition = 18 - newPosition; } board rightmove(curr.recent, without_it); rightmove.recent.push_back(idou(1, movePosition)); addPosition(rightmove, newPosition, moveNumber); toBeProcessed.push(rightmove); } if(movePosition != 0) { newPosition = movePosition - moveNumber; if(newPosition < 0){ newPosition = -newPosition; } board leftmove(curr.recent, without_it); leftmove.recent.push_back(idou(-1, movePosition)); addPosition(leftmove, newPosition, moveNumber); toBeProcessed.push(leftmove); } } } cout << "Impossible" << endl; return; } void solve(string s) { board b(board_of_string(s)); int sum = 0; myforeach(b.b, it, board_impl){ sum += (it -> second); } if(sum % 10 != 0){ cout << "Impossible" << endl; cout << endl; return; } shortest_route(b); cout << endl; } #define cin is int main(void) { ifstream is("zero.txt"); int ncases; is >> ncases; while(ncases --){ string s; is >> s; solve(s); } }