In this problem, we will define a graph called star graph, and the question is to find the minimum distance between two given nodes in the star graph.

Given an integer nnn, an n−dimensionaln-dimensionaln−dimensional star graph, also referred to as SnS_{n}S​n​​, is an undirected graph consisting of n!n!n! nodes (or vertices) and ((n−1) ∗ n!)/2((n-1)\ *\ n!)/2((n−1) ∗ n!)/2 edges. Each node is uniquely assigned a label x1 x2 ... xnx_{1}\ x_{2}\ ...\ x_{n}x​1​​ x​2​​ ... x​n​​ which is any permutation of the n digits 1,2,3,...,n{1, 2, 3, ..., n}1,2,3,...,n. For instance, an S4S_{4}S​4​​ has the following 24 nodes 1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321{1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321}1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321. For each node with label x1 x2x3 x4 ... xnx_{1}\ x_{2} x_{3}\ x_{4}\ ...\ x_{n}x​1​​ x​2​​x​3​​ x​4​​ ... x​n​​, it has n−1n-1n−1 edges connecting to nodes x2 x1 x3 x4 ... xnx_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x​2​​ x​1​​ x​3​​ x​4​​ ... x​n​​, x3 x2 x1 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x​3​​ x​2​​ x​1​​ x​4​​ ... x​n​​, x4 x2 x3 x1 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x​4​​ x​2​​ x​3​​ x​1​​ ... x​n​​, ..., and xn x2 x3 x4 ... x1x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}x​n​​ x​2​​ x​3​​ x​4​​ ... x​1​​. That is, the n−1n-1n−1 adjacent nodes are obtained by swapping the first symbol and the d−thd-thd−th symbol of x1 x2 x3 x4 ... xnx_{1}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{n}x​1​​ x​2​​ x​3​​ x​4​​ ... x​n​​, for d=2,...,nd = 2, ..., nd=2,...,n. For instance, in S4S_{4}S​4​​, node 123412341234 has 333 edges connecting to nodes 213421342134, 321432143214, and 423142314231. The following figure shows how S4S_{4}S​4​​ looks (note that the symbols aaa, bbb, ccc, and ddd are not nodes; we only use them to show the connectivity between nodes; this is for the clarity of the figure).

In this problem, you are given the following inputs:

  • nnn: the dimension of the star graph. We assume that nnn ranges from 444 to 999.
  • Two nodes x1x_{1}x​1​​ x2x_{2}x​2​​ x3x_{3}x​3​​ ... xnx_{n}x​n​​ and y1y_{1}y​1​​ y2y_{2}y​2​​ y3 ... yny_{3}\ ...\ y_{n}y​3​​ ... y​n​​ in SnS_{n}S​n​​.

You have to calculate the distance between these two nodes (which is an integer).

Input Format

nnn (dimension of the star graph)

A list of 555 pairs of nodes.

Output Format

A list of 555 values, each representing the distance of a pair of nodes.

样例输入

4
1234 4231
1234 3124
2341 1324
3214 4213
3214 2143

样例输出

1
2
2
1
3 题目读完以后就是看看一个n位数能不能够通过题目中要求的变化变化到另一个n位数,求变化的最小次数。
用bfs搜索即可。
题目中要求的变化是指,将第一位数与之后的n-1位数分别交换得到的新数。
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
int n,t;
char ch1[],ch2[];
map<string,bool> mp;
struct node
{
string s;
int step;
};
int bfs()
{
queue<node> Q;
mp.clear();
node st;
st.s=ch1;
st.step=;
Q.push(st);
while(!Q.empty())
{
node u=Q.front();
if (u.s==ch2) return u.step;
Q.pop();
for(int i=; i<n; i++)
{
swap(u.s[],u.s[i]);
if (mp[u.s]==)
{
swap(u.s[],u.s[i]);
continue;
}
mp[u.s]=;
node ed;
ed.s=u.s;
ed.step=u.step+;
Q.push(ed);
swap(u.s[],u.s[i]);
}
}
}
int main()
{
scanf("%d",&n);
for(int t=;t<=;t++)
{
scanf("%s %s",&ch1,&ch2);
printf("%d\n",bfs());
}
return ;
}

2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)的更多相关文章

  1. 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 ...

  2. 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)

    You are given a list of train stations, say from the station 111 to the station 100100100. The passe ...

  3. 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)

    There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...

  4. 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)

    Let SSS be a sequence of integers s1s_{1}s​1​​, s2s_{2}s​2​​, ........., sns_{n}s​n​​ Each integer i ...

  5. 2017ICPC北京赛区网络赛 Minimum(数学+线段树)

    描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...

  6. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

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

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

  8. 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)

    描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...

  9. 2017 ACM-ICPC 南宁区比赛 Minimum Distance in a Star Graph

    2017-09-25 19:58:04 writer:pprp 题意看上去很难很难,但是耐心看看还是能看懂的,给你n位数字 你可以交换第一位和之后的某一位,问你采用最少的步数可以交换成目标 有五组数据 ...

随机推荐

  1. 【Java】【7】枚举类

    用处:规范了参数的形式,更简洁易懂 实例: //消息类型 public enum MessageTypeEnum { AdminReward(1, "官方消息"), StoreRe ...

  2. python-flask-路由匹配源码分析

    @app.route('/') def hello_world(): return 'Hello World!' 第1步: class Flask(_PackageBoundObject): def ...

  3. IntelliJ IDEA2017创建web工程并实现远程部署tomca【转载】

    [IntelliJ IDEA2017创建web工程并实现远程部署tomcat] 作者:https://segmentfault.com/a/1190000012762629 将应用打成war包方式 步 ...

  4. iOS性能优化总结

    iOS性能优化总结.关于 iOS 性能优化梳理: 基本工具.业务优化.内存优化.卡顿优化.布局优化.电量优化. 安装包瘦身.启动优化.网络优化等. 关于iOS 性能优化梳理: 基本工具.业务优化.内存 ...

  5. MP3文件结构解析(超详细)

    转自:http://blog.csdn.net/u010650845/article/details/53520426 MP3文件结构解析(超详细) 1. MP3文件结构解析 1.1. 概述 1.1. ...

  6. 移动端常用的 meta设置

    <meta charset="utf-8"> <meta name="viewport" content="width=device ...

  7. Java Date日期,一年中的第多少天,星期的简称,当月多少天,当年多少月

    import java.util.*; public class DateDemo { public static void main(String args[]) { Date date=new D ...

  8. learning shell display alert function

    [Purpose]        Shell print function base on err info wrn ext output level   [Eevironment]        U ...

  9. day3-PyCharm 断点 调试模式

    上篇学习了Python的工具选择,PyCharm的基本设置,简单的了解了下PyCharm的调试模式,今天主要学习下PyCharm的调式模式,在以后的开发中是一个非常重要的工具. [运行]和[调试]前的 ...

  10. 常见的SQLALCHEMY列类型

    常见的SQLALCHEMY列类型.配置选项和关系选项   类型名称    python类型    描述 Integer int 常规整形,通常为32位 SmallInteger    int 短整形, ...