栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AtCoder D - Multiply and Rotate

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AtCoder D - Multiply and Rotate

题目地址:D - Multiply and Rotate


题意如上图所示:

思路 + 代码

我们采用 b f s bfs bfs搜索的方法,同时我们可以保证第一次到达目的数字时的步数就是答案所需的最小操作次数,因为 b f s bfs bfs是一层一层进行搜索的。
但是我们需要注意这道题中的一些细节问题:

对于题目当中的第二个操作,我们在移位变换的过程中要避免循环到同一个数字,所以我们可以定义一个数组来标记该数字。对于每个操作,我们需要判断操作以后的数字是否有意义。当操作后的数作为字符串时的长度小于等于 N N N作为字符串时的长度时,我们认为该数有意义,如此我们才能将其存入队列进行下一次操作。

下面时代码展示(附快读板子):

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
#define mem(s, i) memset(s, i, sizeof(s))
#define PII pair
#define INF 0x7fffffff
using namespace std;
const int N = 1e8+10;
const int mod = 1e9 + 7;
ll t, n, m;
ll stp[N];
int vim[N];
queue q;
template 
inline void read(T &res)
{
	char c;
	T flag = 1;
	while ((c = getchar()) < '0' || c > '9')
		if (c == '-')
			flag = -1;
	res = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		res = res * 10 + c - '0';
	res *= flag;
}
void solve()
{
	cin>>n>>m;
	stp[1] = 0;
	q.push(1);
	while(!q.empty()){
		ll t = q.front();q.pop();
		// cout< to_string(m).length())continue;
				stp[res] = stp[t]+1;
				q.push(res);
			}
			else{
				if(t>=10&&t%10!=0){
					ll res = 0;
					res = t%10*pow(10,to_string(t).length()-1)+t/10;
					if(to_string(res).length() > to_string(m).length())continue;
					if(vim[res]==0){
						vim[res]++;
						stp[res] = stp[t]+1;
						q.push(res);
					}
				}
			}
		}
	}cout<<-1<> t;getchar();
	// while (t--)
	solve();
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号