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位数字 你可以交换第一位和之后的某一位,问你采用最少的步数可以交换成目标 有五组数据 ...
随机推荐
- 关于POD和非POD类型中,list initialization和constructor initialization(未解决)
如果你的成员是POD类型的,那么list initialization和constructor initialization没有任何区别 #include<iostream> using ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- Linux上部署多个tomcat端口设置
在Linux上部署多个tomcat主要是防止端口冲突的问题, tomcat服务器需配置三个端口才能启动,安装时默认启用了这三个端口,当要运行多个tomcat服务时需要修改这三个端口,不能相同.端口一: ...
- Spring Boot项目打包部署到外部Tomcat
1.生成war包 1)修改POM文件,将打包类型改为war:<packaging>war</packaging> <packaging>war</packag ...
- poj-2514-模拟
http://poj.org/problem?id=2514 Ridiculous Addition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- zzw原创_LIKE与regexp_like中的_及转义符
1.select table_name from user_tables where table_name like 'MENU%';查出以下表MENUMENUGGG_131MENU_132MENU ...
- git设置远程同步分支
git push --set-upstream origin yourBranchName
- 【转】jquery.validate.js表单验证
一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...
- OllyDbg安装教程
1.下载 http://tools.pediy.com/windows/debuggers.htm 我们这里选择OllyDbg1.10下载 2.安装 解压下载的压缩包直接双击启动即可使用 3.插件安装 ...
- VSS迁移详细教程
本文默认迁移机和目标机已是安装好VSS服务,如果没装好参见VSS+SourceAnywhere for VSS搭建版本控制系统教程 如果你只想以最快的速度迁移库而并不关心VSS的一些操作使用,那么可直 ...