1151. 魔板

来源:互联网 发布:电脑硬盘测评软件 编辑:程序博客网 时间:2024/05/19 19:40

用set判重会被打么……

#include <iostream>#include <queue>#include <set>using namespace std;int goal;int maxStep;queue<int> searchingList;queue<string> stepList;set<int> been;void init() {    goal = 0;    for(int i = 0; i < 8; i++) {        int bit;        cin >> bit;        goal = 10 * goal + bit;    }    while(!searchingList.empty()) {        searchingList.pop();    }    while(!stepList.empty()) {        stepList.pop();    }    searchingList.push(12348765);    stepList.push("");    been.clear();}int a(int board) {    return board % 10000 * 10000 + board / 10000;}int b(int board) {    return (board / 10000 % 10 * 1000 + board / 10000 / 10) * 10000           + (board % 10000 % 10 * 1000 + board % 10000 / 10);}int c(int board) {    return board / 10000000 * 10000000 + board / 100 % 10 * 1000000            + board / 1000000 % 10 * 100000           + board % 100000 / 1000 * 1000 + board / 10 % 10 * 100            + board / 100000 % 10 * 10 + board % 10;}void search() {    while(!searchingList.empty() && !stepList.empty()) {        int bo = searchingList.front();        string st = stepList.front();        if(st.length() > maxStep) {            cout << -1 << endl;            return;        }        else if(bo == goal) {            cout << st.length() << ' ' << st << endl;            return;        }        else if(been.find(bo) != been.end()) {        searchingList.pop();            stepList.pop();            continue;        }        been.insert(bo);        searchingList.pop();        stepList.pop();        searchingList.push(a(bo)), stepList.push(st+"A");        searchingList.push(b(bo)), stepList.push(st+"B");        searchingList.push(c(bo)), stepList.push(st+"C");    }}int main() {    while(cin >> maxStep && maxStep != -1) {        init();        search();    }    return 0;}                                 


0 0
原创粉丝点击