(蓝桥杯)第八届A组C/C++跳蚱蜢
2024-08-31 01:49:55
#include<iostream>
#include<memory.h>
#include<stack>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
#include<sstream>
#include<set>
#include<queue>
//青蛙跳格子,我采用裸广搜的方法,几秒可以出答案,但是有时间限制就不行了
//将青蛙跳看作是,圆盘跳动,这样就只有一个变量在变化了
//将圆盘看成是0,初始序列用012345678表示,在广搜的时候用set判一下重
using namespace std;
struct node
{
string str;
int pos;
int step;
node(string str,int pos,int step):str(str),pos(pos),step(step){}
};
int N=9;
set<string> visited;
queue<node> q;
void insertq(node no,int i)
{
string s=no.str;
swap(s[no.pos],s[(no.pos+i+9)%9]);
if(visited.count(s)==0)
{
visited.insert(s);
node n(s,(no.pos+i+9)%9,no.step+1);
q.push(n);
}
}
int main()
{
node first("012345678",0,0);
q.push(first);
while(!q.empty())
{
node temp = q.front();
if(temp.str=="087654321")
{
cout<<temp.step;
break;
}
else
{
insertq(temp,1);
insertq(temp,-1);
insertq(temp,2);
insertq(temp,-2);
q.pop();
}
}
}
输出为20
最新文章
- 使用roslyn代替MSBuild完成解决方案编译
- KSFramework常见问题:Excel如何进行SVN协作、差异比较?
- wordpress自动清理评论回收站
- wxPython简单入门
- 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
- mysql中的comment用法
- Android 系统的四层结构
- C++ 数组的地址问题学习随笔
- 破解win2008r2服务器域用户名
- C#开发人员能够可视化操作windows服务
- ReviewBoard安装和配置说明
- Keil STM32调试,使用ST-Link下载程序时提示“flash timeout.reset the target and try it again”
- 《连载 | 物联网框架ServerSuperIO教程》- 16.OPC Server的使用步骤。附:3.3 发布与版本更新说明。
- 以打印日志为荣之logging模块详细使用
- 【CentOS】阿里云CentOS安装php环境
- linux下安装配置jdk(解压版)
- 第二次JAVA作业
- Android Studio学习之build.gradle文件
- 【Angular】——TypeScript之胖箭头(=>;)函数
- 用mobiscroll.js的treelist实现弹出下拉效果
热门文章
- 49.Ext.form.TextField()基本用法
- JAVA基础(多线程Thread和Runnable的使用区别(转载)
- phpci发送邮件
- [Swift通天遁地]三、手势与图表-(5)创建带有标题、图例、坐标轴的柱形图表
- HttpFileCollection 类使用
- Dex文件方法数超过65536怎么破?
- 【洛谷4219】[BJOI2014]大融合(线段树分治)
- UIPickerView的应用
- TextOut与DrawText的区别
- [Windows Server 2012] PHPWind安全设置