2017 JUST Programming Contest 3.0 I. Move Between Numbers
2.0 s
256 MB
standard input
standard output
You are given n magical numbers a1, a2, ..., an,
such that the length of each of these numbers is 20 digits.
You can move from the ith number
to the jth number,
if the number of common digits between ai and aj is exactly 17
digits.
The number of common digits between two numbers x and y is
computed is follow:
.
Where countXi is
the frequency of the ith digit
in the number x, and countYi is
the frequency of the ith digit
in the number y.
You are given two integers s and e,
your task is to find the minimum numbers of moves you need to do, in order to finish at number aestarting
from number as.
The first line contains an integer T (1 ≤ T ≤ 250),
where T is the number of test cases.
The first line of each test case contains three integers n, s,
and e (1 ≤ n ≤ 250) (1 ≤ s, e ≤ n),
where n is the number of magical numbers, s is
the index of the number to start from it, and e is the index of the number to finish at it.
Then n lines follow, giving the magical numbers. All numbers consisting of digits, and with length of 20 digits. Leading zeros are
allowed.
For each test case, print a single line containing the minimum numbers of moves you need to do, in order to finish at number ae starting
from number as.
If there is no answer, print -1.
1
5 1 5
11111191111191111911
11181111111111818111
11811171817171181111
11111116161111611181
11751717818314111118
3
In the first test case, you can move from a1 to a2,
from a2 to a3,
and from a3 to a5.
So, the minimum number of moves is 3 moves.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = 255;
int n;
int e[maxn][maxn];
const int inf = 99999999;
void initial() {
int i, j;
for (i = 1; i < maxn; ++i) {
for (j = 1; j < maxn; ++j) {
if (i == j) {
e[i][j] = 0;
}
else {
e[i][j] = inf;
}
}
}
}
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
if (e[i][j] > e[i][k] + e[k][j]) {//如果当前i-->j的距离大于i-->k--->j的距离之和
e[i][j] = e[i][k] + e[k][j];//更新从i--->j的最短路径
}
}
}
}
}
int num[maxn][12];
char s[25];
void init(int i)
{
for(int k=0;k<20;k++)
num[i][s[k]-'0']++;
}
int main()
{
// freopen("in.txt","r",stdin);
int T,x,y,i,j,k;
scanf("%d",&T);
while(T--)
{
initial();
memset(num,0,sizeof(num));
scanf("%d%d%d",&n,&x,&y);
for(i=1;i<=n;i++)
{
scanf("%s",&s);
init(i);
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
int com=0;
for(k=0;k<=9;k++)
{
com+=min(num[i][k],num[j][k]);
}
if(com==17)
{//加边
e[i][j] = 1;
e[j][i] = 1;
// cout<<i<<" "<<j<<endl;
}
}
floyd();
if(e[x][y]==inf)
printf("-1\n");
else
printf("%d\n",e[x][y]);
//通过x,y来计算最短路径
}
}
2017 JUST Programming Contest 3.0 I. Move Between Numbers的更多相关文章
- 2017 JUST Programming Contest 3.0 D. Dice Game
D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...
- 2017 JUST Programming Contest 2.0 题解
[题目链接] A - On The Way to Lucky Plaza 首先,$n>m$或$k>m$或$k>n$就无解. 设$p = \frac{A}{B}$,$ans = C_{ ...
- gym101532 2017 JUST Programming Contest 4.0
台州学院ICPC赛前训练5 人生第一次ak,而且ak得还蛮快的,感谢队友带我飞 A 直接用claris的模板啊,他模板确实比较强大,其实就是因为更新的很快 #include<bits/stdc+ ...
- 2017 JUST Programming Contest 3.0 B. Linear Algebra Test
B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...
- 2017 JUST Programming Contest 3.0 H. Eyad and Math
H. Eyad and Math time limit per test 2.0 s memory limit per test 256 MB input standard input output ...
- 2017 JUST Programming Contest 3.0 K. Malek and Summer Semester
K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard i ...
- 2017 JUST Programming Contest 3.0 E. The Architect Omar
E. The Architect Omar time limit per test 1.0 s memory limit per test 256 MB input standard input ou ...
- gym101343 2017 JUST Programming Contest 2.0
A.On The Way to Lucky Plaza (数论)题意:m个店 每个店可以买一个小球的概率为p 求恰好在第m个店买到k个小球的概率 题解:求在前m-1个店买k-1个球再*p ...
- 2017 JUST Programming Contest 2.0
B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...
随机推荐
- 【APUE】进程间通信之共享存储(mmap函数)
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...
- Registration system
Registration system 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 A new e-mail service "Berlandesk&q ...
- 如何将Python的py程序打包成跨平台的exe文件
在编写了自己的第一个可以爬写网页源代码的程序之后,发现如果在没有安装了pythonLDLE程序的计算机上根本就跑不出来.所以开始寻找可以将程序打包成跨平台运行的exe文件. 经过自己费力的谷歌没有一个 ...
- mysql中“Table ‘’ is read only”的解决办法
之前是在linux下面直接Copy的data下面整个数据库文件夹,在phpMyAdmin里面重新赋予新用户相应权限后,drupal成功连接上数据库.但出现N多行错误提示,都是跟Cache相关的表是‘R ...
- 2016/05/15 ThinkPHP3.2.2 表单自动验证实例 验证规则的数组 直接写在相应的控制器里
使用TP 3.2框架 验证规则也可以写到模型里,但感觉有些麻烦, 一是有时候不同页面验证的方式会不一样, 二是看到这个 Add 事件里的代码,就清楚要接收什么数据,如何验证数据能够在第一眼有个大 ...
- Codeforces 768 E. Game of Stones 博弈DP
E. Game of Stones Sam has been teaching Jon the Game of Stones to sharpen his mind and help him de ...
- python中一切皆是对象,对象都是在堆上存放的,一切都是指针
1 由于对象都是在堆上存放的,所以,返回值可以任意返回. 这样看来,闭包里面的外部函数的内部变量也是对象,所以,当返回的内部函数被调用时,这个外部函数的变量就没有被释放. 这样看来,返回时,不需要考虑 ...
- [BZOJ2144]国家集训队 跳跳棋
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- Deep Learning 30: 卷积理解
一.深度卷积神经网络学习笔记(一): 1. 这篇文章以贾清扬的ppt说明了卷积的实质,更说明了卷积输出图像大小应该为: 假设输入图像尺寸为W,卷积核尺寸为F,步幅(stride)为S(卷积核移动的步幅 ...
- 获取WiFi MAC地址总结【转】
本文转载自:http://blog.csdn.net/crazyman2010/article/details/50464256 今天对MAC地址的获取做了一些学习,目前网上获取MAC地址的方法主要如 ...