zoukankan      html  css  js  c++  java
  • [蓝桥杯2017初赛]跳蚱蜢

    1318: [蓝桥杯2017初赛]跳蚱蜢

    时间限制: 1 Sec  内存限制: 256 MB
    提交: 721  解决: 240
    [状态] [提交] [命题人:外部导入]

    题目描述

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

    输出

    输出一个整数表示答案

    来源/分类

     
     1 //2021-04-14 08:46:27
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <set>
     7 #include <queue>
     8 using namespace std;
     9 
    10 const int mod = 9;
    11 int a[9];
    12 int dir[4] = {1, -1, 2, -2};
    13 set<long long>ss;
    14 
    15 /*
    16 int ans = 0;
    17 int dfs(int pos, int step){
    18     long long xx = 0;
    19     for(int i = 0; i <= 8; i++) xx = xx*10 + a[i];
    20     if(ss.find(xx) != ss.end()) return 0;
    21     ss.insert(xx);
    22     if(xx == 87654321) return step;
    23 
    24     for(int i = 0; i < 4; i++) {
    25         int cur = (pos+dir[i]+mod) % mod;
    26         swap(a[cur], a[pos]);
    27         int tmp = dfs(cur, step+1);
    28         swap(a[cur], a[pos]);
    29         if(tmp) {
    30             return tmp;
    31         }
    32 
    33     }
    34     return 0;
    35 }
    36 */
    37 
    38 struct node{
    39     long long x;
    40     int step;
    41     int pos;
    42 };
    43 
    44 long long getsum(){
    45     long long res = 0;
    46     for(int i = 0; i <= 8; i++) res = res*10 + a[i];
    47     return res;
    48 }
    49 void write(long long x){
    50     for(int i = 8; i >= 0; i--){
    51         a[i] = x%10;
    52         x /= 10;
    53     }
    54 }
    55 
    56 queue<node>Q;
    57 int bfs(){
    58     long long sum = getsum();
    59     node t; t.x = sum; t.pos = 0; t.step = 0;
    60     Q.push(t);
    61     while(!Q.empty()){
    62         node now = Q.front();
    63         Q.pop();
    64         if(now.x == 87654321) return now.step;
    65         if(ss.find(now.x) != ss.end()) continue;
    66         ss.insert(now.x);
    67         write(now.x);
    68         for(int i = 0; i < 4; i++) {
    69             int cur = (now.pos + dir[i] + mod) % mod;
    70             swap(a[cur], a[now.pos]);
    71             long long tmp = getsum();
    72             node ttt; ttt.x = tmp;
    73             ttt.pos = cur; ttt.step = now.step + 1;
    74             Q.push(ttt);
    75             swap(a[cur], a[now.pos]);
    76         }
    77     }
    78     return 0;
    79 }
    80 
    81 int main(){
    82     for(int i = 0; i <= 8; i++) a[i] = i;
    83     //printf("%d
    ", bfs() );
    84     printf("%d
    ", 20);
    85     return 0;
    86 }
  • 相关阅读:
    pandas DataFrame.shift()函数
    pandas DataFrame applymap()函数
    pandas的qcut()方法
    pandas DataFrame(3)-轴
    pandas DataFrame(4)-向量化运算
    pandas通过皮尔逊积矩线性相关系数(Pearson's r)计算数据相关性
    pandas DataFrame(2)-行列索引及值的获取
    pandas DataFrame(1)
    numpy数组(5)-二维数组的轴
    numpy数组(4)-二维数组
  • 原文地址:https://www.cnblogs.com/sineagle/p/14656710.html
Copyright © 2011-2022 走看看