2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
2017-09-29 21:25
465 查看
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 S_{n}Sn,
is an undirected graph consisting of n!n! nodes
(or vertices) and ((n-1)\
*\ n!)/2((n−1) ∗ 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-1n−1 edges
connecting to nodes x_{2}\
x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... xn, x_{3}\
x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xn, x_{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-1n−1 adjacent
nodes are obtained by swapping the first symbol and the d-thd−th 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 21342134, 32143214,
and 42314231.
The following figure shows how S_{4}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).
![](https://res.jisuanke.com/img/upload/20170920/b9510b5515aa0293915340ea604adac52bc73f30.png)
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).
of the star graph)
A list of 55 pairs
of nodes.
each representing the distance of a pair of nodes.
2017
ACM-ICPC 亚洲区(南宁赛区)网络赛
题意:就是先给你一个数n,接下来有 n 行,每行输入两个数位为 n 的数 s 和 t ,问 s 怎么用最小的步骤变成 t ,变化条件:s 在每一步都能变成 n - 1 个数,假设 s 的每一个数位分别是:x1, x2, x3, ......, xn,则在一个步骤中, s 的第一个数位的数能跟后面的每一个数位交换一次,例如 s 是四位数x1x2x3x4,则 s 第一步能变成:x2x1x3x4 , x3x1x2x4 , x4x1x2x3,这三个数。
题解:这道题用广搜做。
附代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC万岁!!!!!!!!!!!!!!
const int maxn = 100;
struct node
{
string book;
int s;
}star, eq;
queue<node>first;
int bfs(int n)
{
map<string, bool>maps;
first.push(star);
struct node exch, rec;
while(!first.empty())
{
rec = first.front();
first.pop();
for(int i = 0; i < n; i++)
{
exch = rec;
swap(exch.book[0], exch.book[i]);
exch.s = rec.s + 1;
if(exch.book == eq.book)
{
return exch.s;
}
if(!maps[exch.book])
{
first.push(exch);
maps[exch.book] = true; ///记得要保存每一次的变化,不然会爆内存
}
}
}
}
int main()
{
int n;
while(scanf("%d ", &n) != EOF)
{
char ch;
for(int i = 1; i <= 5; i++)
{
cin >> star.book;
cin >> eq.book;
star.s = eq.s = 0;
printf("%d\n", bfs(n));
while(!first.empty())
{
first.pop();
}
}
}
return 0;
}
/*
*/
Given an integer nn,
an n-dimensionaln−dimensional 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((n−1) ∗ 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-1n−1 edges
connecting to nodes x_{2}\
x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... xn, x_{3}\
x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xn, x_{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-1n−1 adjacent
nodes are obtained by swapping the first symbol and the d-thd−th 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 21342134, 32143214,
and 42314231.
The following figure shows how S_{4}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).
![](https://res.jisuanke.com/img/upload/20170920/b9510b5515aa0293915340ea604adac52bc73f30.png)
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 (dimensionof 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
题目来源
2017ACM-ICPC 亚洲区(南宁赛区)网络赛
题意:就是先给你一个数n,接下来有 n 行,每行输入两个数位为 n 的数 s 和 t ,问 s 怎么用最小的步骤变成 t ,变化条件:s 在每一步都能变成 n - 1 个数,假设 s 的每一个数位分别是:x1, x2, x3, ......, xn,则在一个步骤中, s 的第一个数位的数能跟后面的每一个数位交换一次,例如 s 是四位数x1x2x3x4,则 s 第一步能变成:x2x1x3x4 , x3x1x2x4 , x4x1x2x3,这三个数。
题解:这道题用广搜做。
附代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC万岁!!!!!!!!!!!!!!
const int maxn = 100;
struct node
{
string book;
int s;
}star, eq;
queue<node>first;
int bfs(int n)
{
map<string, bool>maps;
first.push(star);
struct node exch, rec;
while(!first.empty())
{
rec = first.front();
first.pop();
for(int i = 0; i < n; i++)
{
exch = rec;
swap(exch.book[0], exch.book[i]);
exch.s = rec.s + 1;
if(exch.book == eq.book)
{
return exch.s;
}
if(!maps[exch.book])
{
first.push(exch);
maps[exch.book] = true; ///记得要保存每一次的变化,不然会爆内存
}
}
}
}
int main()
{
int n;
while(scanf("%d ", &n) != EOF)
{
char ch;
for(int i = 1; i <= 5; i++)
{
cin >> star.book;
cin >> eq.book;
star.s = eq.s = 0;
printf("%d\n", bfs(n));
while(!first.empty())
{
first.pop();
}
}
}
return 0;
}
/*
*/
相关文章推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 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
- 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)
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛-L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem (LIS)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛-I-GSM Base Station Identification(线性变换)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation(贪心问题+区间覆盖)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A Weather Patterns
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L题 非递减权值最大