2017 icpc 南宁赛区 J. 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 distance between two given nodes in the star graph.

Given an integer nn, an n-dimensionalndimensional star graph, also referred to as S_{n}Sn, is an undirected graph consisting of n!n! nodes (or vertices) and ((n-1)\ *\ n!)/2((n1)  n!)/2 edges. Each node is uniquely assigned a label x_{1}\ x_{2}\ ...\ x_{n}x1 x2 ... xnwhich is any permutation of the n digits {1, 2, 3, ..., n}1,2,3,...,n. For instance, an S_{4}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}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 x_{1}\ x_{2} x_{3}\ x_{4}\ ...\ x_{n}x1 x2x3 x4 ... xn, it has n-1n1 edges connecting to nodes x_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x4 x2 x3 x1 ... xn, ..., and x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}xn x2 x3 x4 ... x1. That is, the n-1n1 adjacent nodes are obtained by swapping the first symbol and the d-thdth symbol of x_{1}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{n}x1 x2 x3 x4 ... xn, for d = 2, ..., nd=2,...,n. For instance, in S_{4}S4, node 12341234 has 33 edges connecting to nodes 2134213432143214, and 42314231. The following figure shows how S_{4}S4 looks (note that the symbols aabbcc, 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 44 to 99.
  • Two nodes x_{1}x1 x_{2}x2 x_{3}x3 ... x_{n}xn and y_{1}y1 y_{2}y2 y_{3}\ ...\ y_{n}y3 ... yn in S_{n}Sn.

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

Input Format

nn (dimension of the star graph)

A list of 55 pairs of nodes.

Output Format

A list of 55 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
题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

【题解】

这题我们的做法比较奇葩,不知道别的队怎么写的,我们试了好几发模拟都没过,就用BFS来写,经过不懈努力,终于搞定它了。


【AC代码】

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 1008611111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
struct node
{
    ll v;
    int step;
};
queue<node>q;
map<ll,int>p;
int a[15];
int b[15];
int main()
{
    int test,n,i,j;
    ll tar,cur;
    node now,next;
    scanf("%d",&n);
    test=5;
    while(test--)
    {
        scanf("%lld%lld",&tar,&cur);
        p.clear();
        if(tar==cur)
        {
            printf("0\n");
            continue;
        }
        while(!q.empty())
            q.pop();
        now.v=cur;
        now.step=0;
        p[now.v]=1;
        q.push(now);
        int t;
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            for(i=0;i<n;i++)
            {
                a[n-i-1]=now.v%10;
                b[n-i-1]=a[n-i-1];
                now.v/=10;
            }
            for(i=1;i<n;i++)
            {
                swap(b[0],b[i]);
                next.v=0;
                for(j=0;j<n;j++)
                {
                    next.v=next.v*10+b[j];
                    b[j]=a[j];
                }
                if(!p[next.v])
                {
                    p[next.v]=1;
                    next.step=now.step+1;
                    if(next.v==tar)
                    {
                        t=next.step;
                        goto loop;
                    }
                    q.push(next);
                }
            }
        }
        loop:;
        printf("%d\n",t);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值