2017ICPC南宁赛区网络赛 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 nnn, an n−dimensionaln-dimensionaln−dimensional star graph, also referred to as SnS_{n}Sn, is an undirected graph consisting of n!n!n! nodes (or vertices) and ((n−1) ∗ n!)/2((n-1)\ *\ n!)/2((n−1) ∗ n!)/2 edges. Each node is uniquely assigned a label x1 x2 ... xnx_{1}\ x_{2}\ ...\ x_{n}x1 x2 ... xn which is any permutation of the n digits 1,2,3,...,n{1, 2, 3, ..., n}1,2,3,...,n. For instance, an S4S_{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}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 ... xnx_{1}\ x_{2} x_{3}\ x_{4}\ ...\ x_{n}x1 x2x3 x4 ... xn, it has n−1n-1n−1 edges connecting to nodes x2 x1 x3 x4 ... xnx_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... xn, x3 x2 x1 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xn, x4 x2 x3 x1 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x4 x2 x3 x1 ... xn, ..., and xn x2 x3 x4 ... x1x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}xn x2 x3 x4 ... x1. That is, the n−1n-1n−1 adjacent nodes are obtained by swapping the first symbol and the d−thd-thd−th symbol of x1 x2 x3 x4 ... xnx_{1}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{n}x1 x2 x3 x4 ... xn, for d=2,...,nd = 2, ..., nd=2,...,n. For instance, in S4S_{4}S4, node 123412341234 has 333 edges connecting to nodes 213421342134, 321432143214, and 423142314231. The following figure shows how S4S_{4}S4 looks (note that the symbols aaa, bbb, ccc, and ddd 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:
- nnn: the dimension of the star graph. We assume that nnn ranges from 444 to 999.
- Two nodes x1x_{1}x1 x2x_{2}x2 x3x_{3}x3 ... xnx_{n}xn and y1y_{1}y1 y2y_{2}y2 y3 ... yny_{3}\ ...\ y_{n}y3 ... yn in SnS_{n}Sn.
You have to calculate the distance between these two nodes (which is an integer).
Input Format
nnn (dimension of the star graph)
A list of 555 pairs of nodes.
Output Format
A list of 555 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 题目读完以后就是看看一个n位数能不能够通过题目中要求的变化变化到另一个n位数,求变化的最小次数。
用bfs搜索即可。
题目中要求的变化是指,将第一位数与之后的n-1位数分别交换得到的新数。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
int n,t;
char ch1[],ch2[];
map<string,bool> mp;
struct node
{
string s;
int step;
};
int bfs()
{
queue<node> Q;
mp.clear();
node st;
st.s=ch1;
st.step=;
Q.push(st);
while(!Q.empty())
{
node u=Q.front();
if (u.s==ch2) return u.step;
Q.pop();
for(int i=; i<n; i++)
{
swap(u.s[],u.s[i]);
if (mp[u.s]==)
{
swap(u.s[],u.s[i]);
continue;
}
mp[u.s]=;
node ed;
ed.s=u.s;
ed.step=u.step+;
Q.push(ed);
swap(u.s[],u.s[i]);
}
}
}
int main()
{
scanf("%d",&n);
for(int t=;t<=;t++)
{
scanf("%s %s",&ch1,&ch2);
printf("%d\n",bfs());
}
return ;
}
2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)的更多相关文章
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)
You are given a list of train stations, say from the station 111 to the station 100100100. The passe ...
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...
- 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)
Let SSS be a sequence of integers s1s_{1}s1, s2s_{2}s2, ........., sns_{n}sn Each integer i ...
- 2017ICPC北京赛区网络赛 Minimum(数学+线段树)
描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- 2017南宁网络赛 Problem J Minimum Distance in a Star Graph ( 模拟 )
题意 : 乱七八糟说了一大堆,实际上就是问你从一个序列到另个序列最少经过多少步的变化,每一次变化只能取序列的任意一个元素去和首元素互换 分析 : 由于只能和第一个元素去互换这种操作,所以没啥最优的特别 ...
- 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)
描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...
- 2017 ACM-ICPC 南宁区比赛 Minimum Distance in a Star Graph
2017-09-25 19:58:04 writer:pprp 题意看上去很难很难,但是耐心看看还是能看懂的,给你n位数字 你可以交换第一位和之后的某一位,问你采用最少的步数可以交换成目标 有五组数据 ...
随机推荐
- 5月21 练习AJAX的查看详细及批量删除
老师讲过之后的复习: 显示数据的代码部分: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- python assert使用说明
python assert断言的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假. assert断言语句的语法格式 判断a与1.b是否一致,msg类似备注 ...
- js中用来操作字符串的相关的方法
var str = "zhufengpeixun2015yangfanqihang"; 字符串也是存在索引和length的 str.length 获取字符串的长度(字符的个数) 第 ...
- Django之权限管理插件
一.功能分析: 一个成熟的web应用,对权限的控制.管理是不可少的:对于一个web应用来说是什么权限? 这要从web应用的使用说起,用户在浏览器输入一个url,访问server端,server端返回这 ...
- Python3 ElementTree.tostring()导致标签前辍变为ns0/ns1处理
一.说明 python中我们经常借助xml.etree.ElementTree对xml进行处理,其中ElementTree.fromstring()将字符串格式化成et对象,ElementTree.t ...
- Linux下使用systemctl命令
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...
- oracle中查询当前系统时间用到的dual是什么?
oracle我们查询当前系统时间的时候,经常用到dual,这个是什么东西呢? -- 查询系统时间 结果:2018/04/17 15:00:48 -- select sysdate from dual; ...
- matlab plot line settings
- 机器学习---笔记----numpy和math包中的常用函数
本文只是简单罗列一下再机器学习过程中遇到的常用的数学函数. 1. math.fabs(x): 返回x的绝对值.同numpy. >>> import numpy >>> ...
- Win10系列:UWP界面布局基础2
属性设置 在面向对象程序开发中,所提及的属性通常指的是对象的属性.在XAML代码中,定义元素时也可以为其设置属性,例如对于一个TextBox元素,有背景属性.宽度属性和高度属性等.为了满足实际应用的需 ...