2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(J.Minimum Distance in a Star Graph)
来源:互联网 发布:sqlmap dbms mssql 编辑:程序博客网 时间:2024/05/29 06:58
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 ... xn 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 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 S4looks (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
题目大意:题目蛮长的,其实没什么内容,只看图和输入输出,这是与八数码类似的路径题,路径是每次可以有n-1种交换,第一个数与后面某位交换,照着之前那篇八数码的改了一下,就过了。。。
#include<iostream>#include<cstring>#include<cstdio>using namespace std;typedef int State[9];const int maxstate=1000000;State st[maxstate],goal;int dist[maxstate];int vis[362880],fact[9];int n;void init_lookup_table(){ fact[0]=1; for(int i=1;i<=9;i++) fact[i]=fact[i-1]*i;}int try_to_insert(int s){ int code =0; for(int i=0;i<n;i++){ int cnt=0; for(int j=i+1;j<n;j++) if(st[s][j]<st[s][i]) cnt++; code +=fact[n-1-i]*cnt; } if(vis[code]) return 0; return vis[code] =1;}int bfs(){ int front=1,rear=2; while(front<rear){ State& s=st[front]; if(memcmp(goal,s,sizeof(s))==0) return front; for(int d=1;d<n;d++)//第零位与后面的每一位一次交换 { State& t=st[rear]; memcpy (&t,&s,sizeof(s)); t[d]=s[0]; t[0]=s[d]; dist[rear]=dist[front]+1; if(try_to_insert(rear)) rear++;//判重与记录 } front++; } return 0;}int main(){ init_lookup_table(); while(~scanf("%d",&n)){ char str[9]; for(int l=0;l<5;l++){ memset(st,0,sizeof(st)); memset(vis,0,sizeof(vis)); memset(dist,0,sizeof(dist)); scanf("%s",str); for(int i=0;i<n;i++) st[1][i]=int(str[i]-'1'); scanf("%s",str); for(int i=0;i<n;i++) goal[i]=int(str[i]-'1'); int ans=bfs(); printf("%d\n",dist[ans]); } } 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)
- Minimum Distance in a Star Graph(南宁网络赛)
- 2017南宁网络赛J.Minimum Distance in a Star Graph(bfs求最短路)
- 计蒜客 Minimum Distance in a Star Graph 2017icpc南宁赛区 字符串bfs
- 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
- Java后台框架篇--Struts2.0与OGNL表达式
- 在Linux系统中不能访问外网的问题
- GetMemory函数详解
- [LeetCode]5. Longest Palindromic Substring
- 读书笔记_《统计陷阱》达莱尔.哈夫
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(J.Minimum Distance in a Star Graph)
- java基本数据类型介绍及其使用注意点
- oracle之 RAC 11G ASM下控制文件多路复用
- VB实现右键扩展菜单以及按钮触发扩展菜单功能
- js获取屏幕宽和高
- Java后台框架篇--Struts2.0之结果视图类型
- TCL脚本语言详解
- 第一阶段:HTML5开发前准备
- 欢迎使用CSDN-markdown编辑器