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 ;
}

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

2017 ACM-ICPC 南宁区比赛 Minimum Distance in a Star Graph的更多相关文章

  1. 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles

    2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...

  2. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

  3. 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

  4. 2017南宁网络赛 Problem J Minimum Distance in a Star Graph ( 模拟 )

    题意 : 乱七八糟说了一大堆,实际上就是问你从一个序列到另个序列最少经过多少步的变化,每一次变化只能取序列的任意一个元素去和首元素互换 分析 : 由于只能和第一个元素去互换这种操作,所以没啥最优的特别 ...

  5. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  6. 2017 ACM/ICPC Shenyang Online SPFA+无向图最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  7. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  8. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  9. 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest

    2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...

随机推荐

  1. kafka Detailed Replication Design V3

    参考,https://cwiki.apache.org/confluence/display/KAFKA/kafka+Detailed+Replication+Design+V3 Major chan ...

  2. html知识代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. CS224n(一)

    个人博客地址: https://yifdu.github.io/2018/10/30/CS224n%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/#more

  4. 『HTML5挑战经典』是英雄就下100层-开源讲座(二)危险!英雄

    本篇为<『HTML5挑战经典』是英雄就下100层-开源讲座>第二篇,需要用到开源引擎lufylegend,可以到这里下载: 下载地址:http://lufylegend.googlecod ...

  5. cookie和session的自我介绍

    Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...

  6. PHP用"字符串和变量"组成变量

    理论很简单,将字符串和变量组合在一起形成对另一个变量的操作.同样也可以是数组 例:   $FFabcd = '组合变量'; $a = 'abcd'; $ay = array('FF','abcd'); ...

  7. Excel文本型数据转为数值型的方法

    操作步骤非常简单,适用于所有版本的Excel. 在任意一个空白单元格中输入数值1,然后选中该单元格,执行复制操作: 选中需要转换的单元格(或区域),执行“编辑.选择性粘贴”命令,打开“选择性粘贴”对话 ...

  8. Linux 最常用的20条命令

    1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如:   cd /root/Docements # 切 ...

  9. 常用的系统架构 web服务器之iis,apache,tomcat三者之间的比较

    常用的系统架构是: Linux + Apache + PHP + MySQL Linux + Apache + Java (WebSphere) + Oracle Windows Server 200 ...

  10. [转载] 活跃在github上的国内前端大牛

    出处:http://blog.csdn.net/yaoxtao/article/details/38518933 20位活跃在Github上的国内技术大牛 本文列举了20位在Github上非常活跃的国 ...