2017-09-25 19:58:04

writer:pprp

题意看上去很难很难,但是耐心看看还是能看懂的,给你n位数字

你可以交换第一位和之后的某一位,问你采用最少的步数可以交换成目标

有五组数据

用BFS进行搜索,并进行剪枝,已经搜索过的点不再搜索

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <queue> using namespace std; string sa, sb; int n; struct node
{
string str;
int times;
node()
{
str = "";
times = ;
}
}; int bfs()
{
queue<node> q;
set<string> ss;
node now , nex;
now.str = sa;
q.push(now);
ss.insert(now.str);
while(!q.empty())
{
now = q.front();
q.pop();
if(now.str == sb)
return now.times;
for(int i = ; i < n;i++)
{
nex.str = now.str;
nex.times = now.times+;
nex.str[i] = now.str[];
nex.str[] = now.str[i];
if(ss.count(nex.str) == )
{
ss.insert(nex.str);
q.push(nex);
}
else
continue;
}
}
} int main()
{
cin >> n;
for(int i = ; i < ;i++)
{
cin >> sa >> sb;
cout << bfs() << endl;
} return ;
}

现阶段掌握搜索还不是太好,希望以后可以尽快掌握

最新文章

  1. Vue1.0 的技术栈
  2. JAVA面向对象
  3. 用BlazeMeter录制JMeter测试脚本
  4. Python 之 装饰器的写法
  5. [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数
  6. linux应用与发展(上)
  7. URAL 1076 Trash Trash(最大权匹配)
  8. 【BZOJ】【1798】【AHOI2009】Seq维护序列
  9. MYSQL 缓存详解 [myownstars] 经典博客
  10. HDOJ(HDU) 2178 猜数字(题意有点难理解、、、)
  11. vs2013+opencv2.4.11+Qt5.5.1配置
  12. java web中filter分析
  13. 【Visual C++】游戏编程学习笔记之四:透明动画实现
  14. 在Winform系统界面中对进展阶段的动态展示和处理
  15. CocosCraetor中图像资源Texture和SpriteFrame的区别
  16. Transparent PageRoute in Flutter for displaying a (semi-) transparent page
  17. JavaScript 版本的 RSA加密库文件
  18. 减少apk包大小的一种思路
  19. phpStudy apache 启动不了
  20. eclipse配置google代码风格

热门文章

  1. 【react map函数】
  2. Jmeter(三)断言和关联
  3. AngularJS 笔记系列(三)模块和作用域
  4. .net:Code First 创建或更新数据库
  5. 什么是Java中的原子操作( atomic operations)
  6. input 虚拟键盘
  7. 20145316许心远《Java学习笔记(第8版)》课程总结
  8. LNMP环境简单教程
  9. 【c++ primer, 5e】【try语句块】
  10. Redis学习笔记之Redis中5种数据结构的使用场景介绍