2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
来源:互联网 发布:千语是什么软件 编辑:程序博客网 时间:2024/06/05 06:15
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 n, an n−dimensional star graph, also referred to as Sn, is an undirected graph consisting of n! nodes (or vertices) and ((n−1) ∗ n!)/2 edges. Each node is uniquely assigned a label x1 x2 ... xnwhich 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 x1 x2x3 x4 ... xn, it has n−1 edges connecting to nodes x2 x1 x3 x4 ... xn, x3 x2 x1 x4 ... xn, x4 x2 x3 x1 ... xn, ..., and xn x2 x3 x4 ... x1. That is, the n−1 adjacent nodes are obtained by swapping the first symbol and the d−th symbol of x1 x2 x3 x4 ... xn, for d=2,...,n. For instance, in S4, node 1234 has 3 edges connecting to nodes 2134, 3214, and 4231. The following figure shows how S4 looks (note that the symbols a, b, c, and d 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:
- n: the dimension of the star graph. We assume that n 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 Format
n (dimension of the star graph)
A list of 5 pairs of nodes.
Output Format
A list of 5 values, each representing the distance of a pair of nodes.
样例输入
41234 42311234 31242341 13243214 42133214 2143
样例输出
12213
题解:
题意:
给一个全排序,每次允许第一个数字和后面的随便一个数字交换,让你求可以达到目标串的最小交换次数
题解:
一开始从后往前放wa了几发,后来发现9的阶乘是36w左右,满足bfs的时间复杂度,然后就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+1using 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;}
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(J.Minimum Distance in a Star Graph)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J.Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
- Minimum Distance in a Star Graph BFS/康托展开 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC南宁网络赛: J. Minimum Distance in a Star Graph(BFS)
- 2017 icpc 南宁赛区 J. Minimum Distance in a Star Graph(bfs)
- 2017南宁网络赛J.Minimum Distance in a Star Graph(bfs求最短路)
- 计蒜客 Minimum Distance in a Star Graph 2017icpc南宁赛区 字符串bfs
- Minimum Distance in a Star Graph(南宁网络赛)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- [计蒜客16959] Colored Graph [2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J]
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A. Weather Patterns(阅读题)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A. Weather Patterns
- python正则表达式
- 深度学习资源帖
- Django+Karlooper+iOS重构学校的学生成绩管理系统(四)iOS客户端开发
- 关于有环链表的求入环节点的思路
- Tensorflow中的mnist例子
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- fcntl.h头文件和unistd.h头文件
- 第十一节包装类及相关面试题
- java基础篇:子类实例化的执行顺序(静态,构造方法,成员方法,成员属性)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛Train Seats Reservation(树状数组的运用)
- 反爬虫措施
- SecureCRT如何安装MySQL,步骤
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛-- B. Train Seats Reservation-(区间更新,单点查询思想)
- jQuery学习笔记(一)_简介