#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

最新文章

  1. 使用roslyn代替MSBuild完成解决方案编译
  2. KSFramework常见问题:Excel如何进行SVN协作、差异比较?
  3. wordpress自动清理评论回收站
  4. wxPython简单入门
  5. 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
  6. mysql中的comment用法
  7. Android 系统的四层结构
  8. C++ 数组的地址问题学习随笔
  9. 破解win2008r2服务器域用户名
  10. C#开发人员能够可视化操作windows服务
  11. ReviewBoard安装和配置说明
  12. Keil STM32调试,使用ST-Link下载程序时提示“flash timeout.reset the target and try it again”
  13. 《连载 | 物联网框架ServerSuperIO教程》- 16.OPC Server的使用步骤。附:3.3 发布与版本更新说明。
  14. 以打印日志为荣之logging模块详细使用
  15. 【CentOS】阿里云CentOS安装php环境
  16. linux下安装配置jdk(解压版)
  17. 第二次JAVA作业
  18. Android Studio学习之build.gradle文件
  19. 【Angular】——TypeScript之胖箭头(=&gt;)函数
  20. 用mobiscroll.js的treelist实现弹出下拉效果

热门文章

  1. 49.Ext.form.TextField()基本用法
  2. JAVA基础(多线程Thread和Runnable的使用区别(转载)
  3. phpci发送邮件
  4. [Swift通天遁地]三、手势与图表-(5)创建带有标题、图例、坐标轴的柱形图表
  5. HttpFileCollection 类使用
  6. Dex文件方法数超过65536怎么破?
  7. 【洛谷4219】[BJOI2014]大融合(线段树分治)
  8. UIPickerView的应用
  9. TextOut与DrawText的区别
  10. [Windows Server 2012] PHPWind安全设置