2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
来源:互联网 发布:linux 文件不保存退出 编辑:程序博客网 时间:2024/06/17 03:12
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
题目来源
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
题意:N维空间最短路……
解题思路:由于权值是1,直接暴力宽搜即可……
#include<iostream>#include<algorithm>#include<math.h>#include<string>#include<queue>#include<map>using namespace std;typedef long long int ll;struct point{string str;int step;point(string a="",int b=0){str=a;step=b;}};int n;map<string,bool> vis;int bfs(string startpoint,string endpoint){queue<point> que;que.push(point(startpoint,0));vis[startpoint]=1;while(!que.empty()){point tp=que.front();que.pop();if(tp.str==endpoint)return tp.step;int t=tp.step;string s=tp.str;string temp=s;for(int j=1;j<s.size();j++){swap(temp[0],temp[j]);if(vis[temp]==0){que.push(point(temp,t+1));vis[temp]=1; } temp=s;}}}int main(){char a[20],b[20];while(~scanf("%d",&n)){for(int i=0;i<5;i++){vis.clear();scanf("%s%s",a,b);printf("%d\n",bfs(a,b));}}return 0;}
- 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 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(南宁网络赛)
- [计蒜客16959] Colored Graph [2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J]
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A. Weather Patterns(阅读题)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A. Weather Patterns
- 关于redis的搭建
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem (LIS)
- Android编译Lame的全平台so库方案2,并实现转码mp3
- ES6(三: 变量的解构赋值)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L题 非递减权值最大
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- Python爬虫入门-fiddler抓取手机新闻评论
- MAC下升级MySQL数据库
- 关于python文件 I/O 操作 与异常
- 卷积神经网络详解
- 动态规划——开餐馆
- 大数据的大价值:大数据五大成功案例深度解析
- java中几个概念的总结
- 包