Sicily 1150. 简单魔板[Speical judge]

31 篇文章 0 订阅
/*
使用队列即可处理
注意!char[]转换string时需要增加一位'\0',否则将导致赋值出错,该'\0'不会被计入string的length中 
注意!这里的步数是以树的层次来决定的,不是操作的步数 
*/
/*
Run Time: 0secs
Run Memory: 312KB
*/

#include <iostream>
#include <string>
#include <set>
#include <queue>

using namespace std;

typedef struct Node{
    string node; 
    string parents;
};

int N;          //要求步数 
string dest;    //目标状态 

string act[] = {"A", "B", "C"};

//A:上下行互换
string A(string input){
    char output[9];
    output[0] = input[4];
    output[1] = input[5];
    output[2] = input[6];
    output[3] = input[7];
    output[4] = input[0];
    output[5] = input[1];
    output[6] = input[2];
    output[7] = input[3];
    output[8] = '\0';
    
    return output;
}

//B:每次以行循环右移一个
string B(string input){
    char output[9];
    output[0] = input[3];
    output[1] = input[0];
    output[2] = input[1];
    output[3] = input[2];
    output[4] = input[7];
    output[5] = input[4];
    output[6] = input[5];
    output[7] = input[6];
    output[8] = '\0';
    
    return output;
}

//C:中间四小块顺时针转一格
string C(string input){
    char output[9];
    output[0] = input[0];
    output[1] = input[5];
    output[2] = input[1];
    output[3] = input[3];
    output[4] = input[4];
    output[5] = input[6];
    output[6] = input[2];
    output[7] = input[7];
    output[8] = '\0';
    
    return output;
}

Node find(Node sourceNode){
    queue<Node> nodeQueue; //节点队列 
    set<string> used;      //使用过的状态 
    
    if(sourceNode.node == dest)
        return sourceNode; 
    nodeQueue.push(sourceNode);
    used.insert(sourceNode.node);

    while(!nodeQueue.empty()){
        Node theNode = nodeQueue.front();    //获取队列头                 
        nodeQueue.pop();
        
        if(theNode.parents.length() >= N){    //超出步数时候退出循环 
           break;
        }
        
        string bufNewNode[3];
        bufNewNode[0] = A(theNode.node); 
        bufNewNode[1] = B(theNode.node); 
        bufNewNode[2] = C(theNode.node); 
        
        for(int i=0; i<3; i++){
            Node newNode;
            newNode.node = bufNewNode[i];    
            newNode.parents = theNode.parents + act[i]; 
            
            if(newNode.node == dest)    //如果找到了目标,则返回结果 
                return newNode;
            
            if(used.count(newNode.node) == 0){  //不是目标则检查其是否已经看过,否则记录的同时放入队列 
                used.insert(newNode.node);
                nodeQueue.push(newNode);
            }
        }
    } 
    Node notFound;
    notFound.parents = "NotFound";
    
    return notFound;
}




int main()
{
    while (cin>>N && (N!=-1)){
        char buf[9];
        for(int i=0; i<8; i++)
            cin >> buf[i];
        buf[8] = '\0';
        dest = buf;

        Node bufNode;
        bufNode.node = "12348765";
        bufNode.parents = "";
        
        Node destNode = find(bufNode);
        
        if(destNode.parents == "NotFound"){
            cout << "-1" << endl;
        }else{
            cout << destNode.parents.length() << " " << destNode.parents << endl;
        }
    } 

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值