2017第八届蓝桥杯大赛软件类省赛 C/C++ 大学 A 组

题目一:迷宫

X星球的一处迷宫游乐场建在某个小山坡上。
它是由10×10相互连通的小房间组成的。

房间的地板上写着一个很大的字母。
我们假设玩家是面朝上坡的方向站立,则:
L表示走到左边的房间,
R表示走到右边的房间,
U表示走到上坡方向的房间,
D表示走到下坡方向的房间。

X星球的居民有点懒,不愿意费力思考。
他们更喜欢玩运气类的游戏。这个游戏也是如此!

开始的时候,直升机把100名玩家放入一个个小房间内。
玩家一定要按照地上的字母移动。

迷宫地图如下:

UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR

答案:31

题目二:跳蚱蜢

如图 p1.png 所示:
有9只盘子,排成1个圆圈。
其中8只盘子内装着8只蚱蜢,有一个是空盘。
我们把这些蚱蜢顺时针编 为 1~8

每只蚱蜢都可以跳到相邻的空盘中,
也可以再用点力,越过一个相邻的蚱蜢跳到空盘中。

请你计算一下,如果要使得蚱蜢们的队形改为按照逆时针排列,
并且保持空盘的位置不变(也就是1-8换位,2-7换位,…),至少要经过多少次跳跃/p>

注意:要求提交的是一个整数,请不要填写任何多余内容或说明文字。
答案:20

#include #include #include #include using namespace std;int swapp(char a[],int i,int j){    char temp=a[i];    a[i]=a[j];    a[j]=temp;    return 0;}long long int summ(char a[]){    long long temp=0,base=1;    for(int i=8;i>=0;i--){temp+=base*(a[i]-'0');base*=10;    }    return temp;}struct STA{    int lev;    int pos0;    long long int su;    char chs[13];    bool operator (const STA &b) const{return sub.su;    }};queueSTA> que;setSTA> sett;STA beg,temp;long long int aim=87654321;int main(){    beg.lev=0;    beg.pos0=0;    strcpy(beg.chs,"012345678");    beg.su=summ(beg.chs);    que.push(beg);    sett.insert(beg);    while(!que.empty()){for(int i=-2;i2;i++){    if(i==0){ continue;    }    temp=que.front();    swapp(temp.chs,temp.pos0,(temp.pos0+i+9)%9);    temp.su=summ(temp.chs);    temp.pos0=(temp.pos0+i+9)%9;    temp.lev++;    if(temp.su==aim){ printf("%dn",temp.lev); return 0;    }    if(sett.count(temp)==0){ que.push(temp); sett.insert(temp);    }}que.pop();    }	return 0;}

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月8日
下一篇 2020年11月8日

相关推荐