Problem Description
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 nn, an n-dimensionaln−dimensional star graph, also referred to as Sn, is an undirected graph consisting of n! nodes (or vertices) and
edges. Each node is uniquely assigned a label
which is any permutation of the n digits 1,2,3,...,n. For instance, an S4 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}
For each node with label
, it has n−1 edges connecting to nodes
,
,
, ..., and
. That is, the n−1 adjacent nodes are obtained by swapping the first symbol and the d−th symbol of
, for d = 2, ..., n.
For instance, in S4, node 12341234 has 33 edges connecting to nodes 2134, 3214, and 4231. The following figure shows how S4 looks (note that the symbols aa, bb, cc, and dd 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:
- nn: the dimension of the star graph. We assume that nn ranges from 4 to 9.
- Two nodes x1 x2 x3 ... xn and y1 y2 y3 ... yn in Sn.
You have to calculate the distance between these two nodes (which is an integer).
Input
n (dimension of the star graph)
A list of 5 pairs of nodes.
Output
A list of 55 values, each representing the distance of a pair of nodes.
Sample Input
4
1234 4231
1234 3124
2341 1324
3214 4213
3214 2143Sample Output
1
2
2
1
3
题意:现在有一个 n 维的图,图中有 n! 个点,且每个点都由一个 n 维矢量来定位,且相邻的点的连边规则是将当前点 n 维矢量的第一个分量与其后的 n-1 个分量依次交换,例如:1234->2134、3214、4231
现在给出维度 n 以及 5 组样例,每组样例给出两个点,问两个点的最短路
思路:简单来说,就是给出两个数串,每次只能将一个串的首字符与后面的字符进行交换,问最小交换几次能使得两个数串相同,因此按照规则进行 bfs 即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct Node{
string str;
int step;
Node(){}
Node(string str,int step):str(str),step(step){}
};
string str1,str2;
map<string,bool> vis;
int bfs(int n,int step){
vis.clear();
queue<Node> Q;
Q.push(Node(str1,0));
while(!Q.empty()){
Node now=Q.front();
Q.pop();
if(now.str==str2)
return now.step;
for(int i=1;i<=n-1;i++){
swap(now.str[0],now.str[i]);//交换第0个和第i个字符
if(vis[now.str])//若已用过
swap(now.str[0],now.str[i]);//交换回去
else{
vis[now.str]=true;
Q.push(Node(now.str,now.step+1));
swap(now.str[0],now.str[i]);//交换回去,以便进行下一轮交换
}
}
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=5;i++){
cin>>str1>>str2;
int res=bfs(n,0);
printf("%d\n",res);
}
return 0;
}