2017 JUST Programming Contest 3.0 I. Move Between Numbers

I. Move Between Numbers
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
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.

Input

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 ns, 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.

Output

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.

Example
input
1
5 1 5
11111191111191111911
11181111111111818111
11811171817171181111
11111116161111611181
11751717818314111118
output
3
Note

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来计算最短路径

    }
}



截至目前,尚未有2024年四川省级大学生程序设计竞赛的具体题目或题解公开发布。然而,可以基于以往的比赛内容推测可能涉及的类型和主题。 通常情况下,四川省大学生程序设计竞赛(Sichuan Provincial Collegiate Programming Contest)中的A题往往是一个相对基础但具有挑战性的算法问题,旨在测试参赛者的逻辑思维能力和编程技巧。例如,在2021年的比赛中,A题“Chuanpai”的核心在于通过模拟来处理周期性变化的行为模式[^3]。 对于类似的题目,解决的关键通常是识别并利用某种规律或者周期特性来进行高效计算。以下是针对该类问题的一个通用解决方案框架: ### 解决方案框架 假设未来某道A题涉及到周期行为的变化,则可以通过如下方式实现其基本逻辑: #### 周期检测与状态更新 ```python def simulate_rounds(people, preferences): rounds = 0 while not is_stable_state(people): # 判断当前状态是否稳定 update_people_based_on_preferences(people, preferences) # 更新每个人的状态 rounds += 1 return rounds, people def is_stable_state(people): # 定义稳定性条件 pass def update_people_based_on_preferences(people, preferences): n = len(preferences) new_people = [] for i in range(n): if (preferences[i] % 2 == 0 and i % 2 == 0) or \ (preferences[i] % 2 != 0 and i % 2 != 0): new_people.append(add_dish(people[i])) else: new_people.append(eat_dish(people[i])) global people people = new_people[:] def add_dish(person): person[&#39;dishes&#39;] += 1 return person def eat_dish(person): if person[&#39;dishes&#39;] > 0: person[&#39;dishes&#39;] -= 1 return person ``` 上述代码片段展示了如何根据偏好列表`preferences`动态调整人员数组`people`的状态,并持续迭代直到达到某个稳定的终止条件为止。 尽管目前无法确切得知2024年度的确切考题细节,但从过往经验来看,比赛倾向于考察选手们对数据结构、算法优化以及边界情况处理的理解程度。 ### 结论 综上所述,虽然具体到2024年的赛事详情尚不可知,但是通过对历年真题的学习研究可以帮助预测可能出现的方向及其应对策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值