首页 > 其他分享 >【ICPC】The 2021 ICPC Asia Shenyang Regional Contest J

【ICPC】The 2021 ICPC Asia Shenyang Regional Contest J

时间:2024-10-15 22:46:59浏览次数:8  
标签:std 1234 rotate 0000 Contest Shenyang ICPC she 20

Luggage Lock

#搜索 #枚举

题目描述

Eileen has a big luggage and she would pick a lot of things in the luggage every time when A-SOUL goes out for a show. However, if there are too many things in the luggage, the 4-digit password lock on the luggage will be hard to rotate.

The state of lock is the four digits on the lock. In one step, she can choose consecutive digits to rotate up by one simultaneously or down by one simultaneously. For example, she can rotate 0000 \texttt{0000} 0000 to 0111 \texttt{0111} 0111 or 0900 \texttt{0900} 0900 in one step because the rotated digits are consecutive, but she can’t rotate 0000 \texttt{0000} 0000 to 0101 \texttt{0101} 0101 in one step. Since she has little strength, she wants to rotate the lock as few times as possible.

Now the lock is at state a 0 a 1 a 2 a 3 a_0a_1a_2a_3 a0​a1​a2​a3​ and the password is b 0 b 1 b 2 b 3 b_0b_1b_2b_3 b0​b1​b2​b3​. As a fan of A-SOUL, you are asked to help Eileen find out the optimal plan to unlock but you only need to tell Eileen how many times she has to rotate.

输入格式

The first line contains one integer T T T ( 1 ≤ T ≤ 1 0 5 ) (1 \leq T \leq 10^5) (1≤T≤105), denoting the numer of test cases.

Each of the test cases contains a line containing the initial state a 0 a 1 a 2 a 3 a_0a_1a_2a_3 a0​a1​a2​a3​ and the target state b 0 b 1 b 2 b 3 b_0b_1b_2b_3 b0​b1​b2​b3​.

输出格式

For each test case, output a line containing a single integer, denoting the minimum steps needed to unlock.

样例 #1

样例输入 #1

6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468

样例输出 #1

1
1
4
5
1
4

解题思路

首先,对于只有 4 4 4位的密码,总共有 1 0 4 10^4 104种组合,因此,如果我们使用多源 b f s bfs bfs来寻找多源最短路,那么复杂度一定很大。

考虑到每次操作都是等效的,因此我们可以利用相对位置的关系,将多源转为单源。

例如 1234 − > 2267 1234->2267 1234−>2267,相当于 0000 − > 1033 0000->1033 0000−>1033,因为相对距离固定,那么操作的最小次数一定也是固定的。

所以,我们只需要预处理出 0000 0000 0000变为任何组合的最小操作次数即可。

由于每次操作可以有 20 20 20次的变换,那么搜索的总的边数为 20 n 20n 20n,我们使用 s t d : : m a p std::map std::map来判重,最终时间复杂度在 O ( ( n + 20 n ) l o g 2 n ) O((n+20n)log_2n) O((n+20n)log2​n)

代码


constexpr int dir[20][4] =
{ {1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},
{-1,0,0,0},{0,-1,0,0},{0,0,-1,0},{0,0,0,-1},
{1,1,0,0},{-1,-1,0,0},{0,-1,-1,0},{0,1,1,0},
{0,0,1,1},{0,0,-1,-1},{1,1,1,0},{-1,-1,-1,0},
{0,1,1,1},{0,-1,-1,-1},{1,1,1,1},{-1,-1,-1,-1} };

std::map<std::string, int>mp;

void bfs() {
	std::queue<std::pair<std::string, int>>q;
	q.push({ "0000",0 });
	mp["0000"] = 0;

	while (q.size()) {
		auto [s, dis] = q.front();
		q.pop();

		for (int i = 0; i < 20; ++i) {
			std::string t;

			for (int j = 0; j < 4; ++j) {
				t += (char)'0' + ((s[j] - '0') + dir[i][j] + 10) % 10;
			}

			if (!mp.count(t)) {
				q.push({ t,dis + 1 });
				mp[t] = dis + 1;
			}

		}
	}
}
void solve() {

	std::string s1, s2;
	std::cin >> s1 >> s2;

	std::string S;
	for (int i = 0; i < 4; ++i) {
		S += '0' + (s1[i] - s2[i] + 10) % 10;
	}

	int res = mp[S];
	
	std::cout << res << "\n";

}

signed main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);


	int t = 1;
	std::cin >> t;
	
	bfs();
	while (t--) {
		solve();
	}

	return 0;
}

标签:std,1234,rotate,0000,Contest,Shenyang,ICPC,she,20
From: https://blog.csdn.net/Antonio915/article/details/142907243

相关文章

  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest G
    EdgeGroups#树形结构#组合数学#树形dp题目描述Givenanundirectedconnectedgraphofnnnverticesandn......
  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest H
    LifeisaGame#最小生成树#重构树#图论#贪心题目描述Agoodproblemshouldhaveaconcisestatement.Youaregivenanarrayaaaoflength......
  • AtCoder Beginner Contest 375
    A-Seats#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;#defineintlonglongusingvi=vector<int>;usingpii=pair<int,int>;i32main(){ios::sync_with_stdio(false),cin.tie(null......
  • Panasonic Programming Contest 2024(AtCoder Beginner Contest 375)
    PanasonicProgrammingContest2024(AtCoderBeginnerContest375)\(A\)Seats\(AC\)基础字符串。点击查看代码intmain(){intn,ans=0,i;strings;cin>>n>>s;for(i=0;i<n;i++){ans+=(s[i]=='#'&&s[i......
  • AtCoder Beginner Contest 375
    省流版A.枚举所有子串判断即可B.模拟计算记录累加即可C.根据旋转的周期性对每个点旋转次数取模后暴力旋转即可D.枚举\(k\),考虑\(i,j\)的对数,写成数学表达式后维护个数和位置和即可E.背包问题,以前两个数组和为状态,考虑每个数移动到何处转移即可F.逆向,删边变加边,维护......
  • KEYENCE Programming Contest 2024(AtCoder Beginner Contest 374)E题
    六年级蒟蒻来题解了!!!题目大意:给定你一个n,表示有n个生产线,每一个生产线都有两种机器,第i个生产线第一件产品每天可以造Ai件零件但是得付出Pi元的代价,第二件产品每天可以生产Bi件物品但是得付出Qi元的代价,然后给你x元的预算问你所有流水线中的最小值的最大值是多少?思路:首先我们......
  • 2023 Benelux Algorithm Programming Contest (BAPC 23)
    A-\texttt题意\(n\)个软件包,已知大小,可以同时下载\(k\)个,已经下载好了\(m\)个,选\(k\)个下载使得下载完后进度最大,输出已完成进度所占百分比。思路选最大的\(m+k\)个。代码点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoid......
  • The 2022 ICPC Asia Hangzhou Regional Programming Contest K. Master of Both
    题目链接题意:给定n个字符串,然后给定q种字典序规则,在这q种字典序规则下求出这n个字符串的逆序对数量。思路:我们发现q很大,对于每一种排序规则都遍历一遍n个字符串去求逆序对的数量复杂度太高,所以考虑预处理。我们知道要判断两个字符串字典序的大小关系,只需要知道它们第......
  • AtCoder Beginner Contest 373 (A-F)
    AtCoderBeginnerContest373(A-F)比赛链接A-September#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;voidShowball(){intans=0;for(inti=1;i<=12;i++){strings;cin>>s;ans+=((int)s.si......
  • The 2021 ICPC Asia Shenyang Regional Contest
    目录写在前面E签到F签到JBFSB带权并查集H图论I数学L树形DP,容斥M字符串,离线,单调性G贪心写在最后写在前面比赛地址:https://codeforces.com/gym/103427。以下按个人向难度排序。唉唉国庆vp三场唯一打的还像人的一场,最后手里还有两道题在写可惜都没出来呃呃。被树上背......