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 vis标记一次就够了
#include<cstdio>
#include<queue>
#include<map>
#include<string>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string s,t,v;
int n;
struct node
{
string s;
int step;
node(string str,int n)
{
s=str,step=n;
}
node()
{
s="";
step=;
}
};
int bfs()
{
queue<node>Q;
map<string,bool>vis;
Q.push(node(s,));
vis[s]=;
while(!Q.empty())
{
node u=Q.front();
Q.pop();
vis[u.s]=;
if(u.s==t) return u.step;
for(int i=; i<n; ++i)
{
v=u.s;
swap(v[],v[i]);
if(!vis[v])
{
node tc=node(v,u.step+);
vis[v]=;
Q.push(tc);
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=; i<=; ++i)
{
cin>>s>>t;
if(s==t)
{
puts("");
continue;
}
printf("%d\n",bfs());
}
}
#include<cstdio>
#include<queue>
#include<map>
#include<string>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string s,t,u,v;
int n;
int bfs()
{
map<string,int>mp;
queue<string>Q; //不能用queue<char*>;
map<string,bool>vis;
Q.push(s);
mp[s]=;
vis[s]=;
while(!Q.empty())
{
u=Q.front();
Q.pop();
if(u==t) return mp[t];
for(int i=; i<n; ++i)
{
v=u;
swap(v[],v[i]);
mp[v]=mp[u]+;
if(!vis[v])
{
mp[v]=mp[u]+;
vis[v]=;
Q.push(v);
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=; i<=; ++i)
{
cin>>s>>t;
if(s==t)
{
puts("");
continue;
}
printf("%d\n",bfs());
}
}

2017-ACM南宁网络赛的更多相关文章

  1. 2017 icpc 南宁网络赛

    2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...

  2. hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

    原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...

  3. 2017 icpc 沈阳网络赛

    cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

  5. 2013 acm 长沙网络赛 G题 素数+枚举 Goldbach

    题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3856 先预处理求出两个素数的和与积,然后枚举n-prime和n/pr ...

  6. 2017青岛赛区网络赛 Smallest Minimum Cut 求最小割的最小割边数

    先最大流跑一遍 在残存网络上把满流边容量+1 非满流边容量设为无穷大 在进行一次最大流即可 (这里的边都不包括建图时用于反悔的反向边) #include<cstdio> #include& ...

  7. HDU 5901 Count primes (2016 acm 沈阳网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...

  8. hdu 5881 Tea (2016 acm 青岛网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others)    Me ...

  9. 2015年ACM长春网络赛(准备做掉7道:已经更新到6道)

    总结汇总:模板 int getmax_min(char s[]) {//字符串的最大表示法:返回最小数组下标 , j = , k = ; while(i < len && j & ...

  10. 2015年ACM沈阳网络赛(准备做掉4道:)

    Traversal Best Solver Minimum Cut Dividing This Product Excited Database Fang Fang Matches Puzzle Ga ...

随机推荐

  1. 自建Git服务器 - 创建属于你自己的代码仓库

    最近有线上朋友私信问我怎么搭建个人博客,也有咨询我个人项目的代码是如何保管的,还有一个朋友问我买了服务器玩了一段时间,等新鲜感过了就不知道做什么了. 关于这些问题并没有一个标准答案,每个人都有自己的使 ...

  2. HTML JavaScript 基础(上)

    一.初识JavaScript JavaScript 和 Java什么关系? 半毛线关系都没有,只是名字有点重合而已. JavaScript 和python.C#.Java.Ruby一样,都是一门独立的 ...

  3. 原生JS中获取位置的方案总结

    获取鼠标当前位置 clientY.clientX: 鼠标当前位置 相对于 浏览器可视区域顶部.浏览器可视区域左部 的位置: pageY.pageX: 鼠标当前位置 相对于 文档顶部.文档左部的位置: ...

  4. css套路学习(一)

    css3信息获取方法 文档搜索:Google css spec; Google: 关键词 MDN; css tricks成熟css代码块sinppets; Google: center css tri ...

  5. 内蒙古特检院利用物联网/RFID技术提高电梯检测水平

    随着电梯检验工作信息化进程的进一步深入,内蒙古特检院从检验工作中寻找新方法.新手段,为检验员新引入电梯检验手持终端设备,力求提高电梯检验水平,将"电梯安全惠民工程"落到实处. 电梯 ...

  6. JavaScript面向对象那些东西-继承

    继承 父类里有些属性方法 子类想把父类中的这些属性方法 继承过来给子类自己的实例也用用 ( ps: →_→ 能不能专业点 没文化真可怕 ) 一.原型链继承 // 原型链继承:把子类的原型作为父类的实例 ...

  7. 图论--最短路--SPFA

    SPFA算法(shortest path faster algorithm)算法是西南交通大学段凡丁于1994年发表的,它在Bellman-ford算法的基础上进行了改进,使其在能够处理待负权图的单元 ...

  8. 【Spark】Spark-shell案例——单词计数统计

    目录 步骤 一.准备本地文件以作测试 二.通过 --master启动本地模式 三.开发scala单词统计代码 步骤 一.准备本地文件以作测试 在第一台机器执行 mkdir -p /export/ser ...

  9. QTableWidget自定义委托

    QTableWidget单元格使用自定义的lineEdit控件导致不能选中 使用自定义委托解决 1.自定义委托 class LineEditDelegate : public QItemDelegat ...

  10. idea 2020 配置本地 Maven 仓库

    问题: 默认Maven 仓库地址在C盘,C盘是系统盘能少放东西尽量少放. 只需要简单的两步 1.File~Settings 然后搜索 maven 如下图绿框 修改成你自己的 Maven 仓库 2.Fi ...