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.

样例输入

  1. 4
  2. 1234 4231
  3. 1234 3124
  4. 2341 1324
  5. 3214 4213
  6. 3214 2143

样例输出

  1. 1
  2. 2
  3. 2
  4. 1
  5. 3
  6.  
  7. vis标记一次就够了
  1. #include<cstdio>
  2. #include<queue>
  3. #include<map>
  4. #include<string>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<algorithm>
  8. using namespace std;
  9. string s,t,v;
  10. int n;
  11. struct node
  12. {
  13. string s;
  14. int step;
  15. node(string str,int n)
  16. {
  17. s=str,step=n;
  18. }
  19. node()
  20. {
  21. s="";
  22. step=;
  23. }
  24. };
  25. int bfs()
  26. {
  27. queue<node>Q;
  28. map<string,bool>vis;
  29. Q.push(node(s,));
  30. vis[s]=;
  31. while(!Q.empty())
  32. {
  33. node u=Q.front();
  34. Q.pop();
  35. vis[u.s]=;
  36. if(u.s==t) return u.step;
  37. for(int i=; i<n; ++i)
  38. {
  39. v=u.s;
  40. swap(v[],v[i]);
  41. if(!vis[v])
  42. {
  43. node tc=node(v,u.step+);
  44. vis[v]=;
  45. Q.push(tc);
  46. }
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. scanf("%d",&n);
  53. for(int i=; i<=; ++i)
  54. {
  55. cin>>s>>t;
  56. if(s==t)
  57. {
  58. puts("");
  59. continue;
  60. }
  61. printf("%d\n",bfs());
  62. }
  63. }
  1. #include<cstdio>
  2. #include<queue>
  3. #include<map>
  4. #include<string>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<algorithm>
  8. using namespace std;
  9. string s,t,u,v;
  10. int n;
  11. int bfs()
  12. {
  13. map<string,int>mp;
  14. queue<string>Q; //不能用queue<char*>;
  15. map<string,bool>vis;
  16. Q.push(s);
  17. mp[s]=;
  18. vis[s]=;
  19. while(!Q.empty())
  20. {
  21. u=Q.front();
  22. Q.pop();
  23. if(u==t) return mp[t];
  24. for(int i=; i<n; ++i)
  25. {
  26. v=u;
  27. swap(v[],v[i]);
  28. mp[v]=mp[u]+;
  29. if(!vis[v])
  30. {
  31. mp[v]=mp[u]+;
  32. vis[v]=;
  33. Q.push(v);
  34. }
  35. }
  36. }
  37. }
  38. int main()
  39. {
  40. scanf("%d",&n);
  41. for(int i=; i<=; ++i)
  42. {
  43. cin>>s>>t;
  44. if(s==t)
  45. {
  46. puts("");
  47. continue;
  48. }
  49. printf("%d\n",bfs());
  50. }
  51. }

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. Vue中的父子传值问题

    个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! 好久没更博了,感觉下班后的时间莫名其妙就没有了,有了,了... 趁着端午放 ...

  2. Eclipse新建类的时候如何自动添加注释(作者,时间,版本等信息)

    为什么80%的码农都做不了架构师?>>>   方法一:Eclipse中设置在创建新类时自动生成注释 windows–>preference  Java–>Code Sty ...

  3. 8种MySQL分页方法总结

    这篇文章主要介绍了8种MySQL分页方法总结,小编现在才知道,MySQL分页竟然有8种实现方法,本文就一一讲解了这些方法,需要的朋友可以参考下 MySQL的分页似乎一直是个问题,有什么优化方法吗?网上 ...

  4. .user.ini 无法修改/删除 怎么办?

    首先 了解chattr命令: Linux chattr命令用于改变文件属性. 这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式: a:让文件或目录仅供附加用途.b:不更 ...

  5. 数据结构--顺序栈--C++实现

    #include <iostream> #define MaxSize 5000 using namespace std; template <typename T> clas ...

  6. unittest(discover 批量执行用例)

    import unittest dir = "D:\\work_doc\\pycharm2\\python_Basics" #自动化用例所存放的路径 suit = unittest ...

  7. predixy源码学习

    Predixy是一个代理,代理本质上就是用来转发请求的.其主要功能就是接收客户端的请求,然后把客户端请求转发给redis服务端,在redis服务端处理完消息请求后,接收它的响应,并把这个响应返回给客户 ...

  8. undef用法

    #undef的语法 定义:#undef 标识符,用来将前面定义的宏标识符取消定义. 整理了如下几种#undef的常见用法. 1. 防止宏定义冲突在一个程序块中用完宏定义后,为防止后面标识符冲突需要取消 ...

  9. 题目分享Y

    题意:给出一个n个点n条边的图且不一定连通(原题面为每个节点出度为1),相邻节点不能同时被选,每个节点有其对应价值,求最多能获得多少价值?n<=1e6,val[i]<=1e6 分析:很容易 ...

  10. kafka简介及集群部署

    消息队列概念:(Message queue): “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象. “消息队列”是在消息的传输过程中保存消 ...