2017 ACM-ICPC 南宁区比赛 Minimum Distance in a Star Graph
2024-08-24 15:21:33
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 ;
}
现阶段掌握搜索还不是太好,希望以后可以尽快掌握
最新文章
- Vue1.0 的技术栈
- JAVA面向对象
- 用BlazeMeter录制JMeter测试脚本
- Python 之 装饰器的写法
- [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数
- linux应用与发展(上)
- URAL 1076 Trash Trash(最大权匹配)
- 【BZOJ】【1798】【AHOI2009】Seq维护序列
- MYSQL 缓存详解 [myownstars] 经典博客
- HDOJ(HDU) 2178 猜数字(题意有点难理解、、、)
- vs2013+opencv2.4.11+Qt5.5.1配置
- java web中filter分析
- 【Visual C++】游戏编程学习笔记之四:透明动画实现
- 在Winform系统界面中对进展阶段的动态展示和处理
- CocosCraetor中图像资源Texture和SpriteFrame的区别
- Transparent PageRoute in Flutter for displaying a (semi-) transparent page
- JavaScript 版本的 RSA加密库文件
- 减少apk包大小的一种思路
- phpStudy apache 启动不了
- eclipse配置google代码风格