zoukankan      html  css  js  c++  java
  • 2016蓝桥杯省赛C/C++A组第二题 跳蚱蜢

    题意:有9只盘子,排成1个圆圈。  其中8只盘子内装着8只蚱蜢,有一个是空盘。 我们把这些蚱蜢顺时针编号为 1~8 每只蚱蜢都可以跳到相邻的空盘中,也可以再用点力,越过一个相邻的蚱蜢跳到空盘中。  请你计算一下,如果要使得蚱蜢们的队形改为按照逆时针排列,  并且保持空盘的位置不变(也就是1-8换位,2-7换位,...),至少要经过多少次跳跃?  注意:要求提交的是一个整数,请不要填写任何多余内容或说明文字。

    分析:结果是20。

    #include<bits/stdc++.h>
    using namespace std;
    set<string> st;
    int deal(string s){
        int len = s.size();
        for(int i = 0; i < len; ++i){
            if(s[i] == '0') return i;
        }
    }
    int bfs(){
        queue<string> q;
        queue<int> step;
        q.push("012345678");
        step.push(0);
        while(!q.empty()){
            string s = q.front();
            q.pop();
            int tmpstep = step.front();
            step.pop();
            int id = deal(s);
            string tmps = s;
            int tmpid = (id + 1) % 9;
            tmps[id] = s[tmpid];
            tmps[tmpid] = s[id];
            if(!st.count(tmps)){
                if(tmps == "087654321") return tmpstep + 1;
                st.insert(tmps);
                q.push(tmps);
                step.push(tmpstep + 1);
            }
            tmpid = (id + 2) % 9;
            tmps = s;
            tmps[id] = s[tmpid];
            tmps[tmpid] = s[id];
            if(!st.count(tmps)){
                if(tmps == "087654321") return tmpstep + 1;
                st.insert(tmps);
                q.push(tmps);
                step.push(tmpstep + 1);
            }
            tmpid = (id + 8) % 9;
            tmps = s;
            tmps[id] = s[tmpid];
            tmps[tmpid] = s[id];
            if(!st.count(tmps)){
                if(tmps == "087654321") return tmpstep + 1;
                st.insert(tmps);
                q.push(tmps);
                step.push(tmpstep + 1);
            }
            tmpid = (id + 7) % 9;
            tmps = s;
            tmps[id] = s[tmpid];
            tmps[tmpid] = s[id];
            if(!st.count(tmps)){
                if(tmps == "087654321") return tmpstep + 1;
                st.insert(tmps);
                q.push(tmps);
                step.push(tmpstep + 1);
            }
        }
        return -1;
    }
    int main(){
        printf("%d
    ", bfs());
        return 0;
    }
    

      

  • 相关阅读:
    Visual Studio 存在版本之间冲突
    Failed to load resoure:the serve responded with a status of 405 (Method Not Allowed)
    Excel 导入解析数据 NPOIExcelHelper
    WIN10 SERVICES -- 部署IIS
    高德地图API-搜索提示并定位到位置,卫星地图和标准地图的切换
    BIMFCE选择全量绘制
    JS中如何获取当前日期,并与输入日期作比较
    html中input标签放入小图标
    div跟随浏览器大小而改变
    vue-amap接入高德地图示例
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6906155.html
Copyright © 2011-2022 走看看