AtCoder D - Multiply and Rotate

题目地址:D - Multiply and Rotate

在这里插入图片描述
题意如上图所示:

思路 + 代码

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

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

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

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#define ll long long
#define mem(s, i) memset(s, i, sizeof(s))
#define PII pair<int, int>
#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<ll> q;
template <class T>
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<<t<<endl;
		if(t==m){
			cout<<stp[m]<<endl;
			return ;
		}
		for(int i = 0;i < 2;i++){
			if(i==0){
				ll res = t*n;
				if(to_string(res).length() > 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<<endl;
}
int main()
{
	// cin >> t;getchar();
	// while (t--)
	solve();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供一段超声延时乘和DMAS的MATLAB代码。请注意,以下代码是基于假设传感器阵列以及声源的几何结构已知,并且已经进行了合适的采样和处理。 ```matlab % 假设传感器阵列和声源的几何结构已知 % 声源参数 source_position = [x_source, y_source, z_source]; % 声源位置坐标 source_amplitude = amplitude; % 声源幅度 % 传感器阵列参数 sensor_array_position = [x_sensor, y_sensor, z_sensor]; % 传感器阵列位置坐标 sensor_array_size = [num_rows, num_columns]; % 传感器阵列大小(行数和列数) sensor_spacing = spacing; % 传感器间距 % 超声延时乘和DMAS算法 num_sensors = num_rows * num_columns; % 传感器总数 sensor_response = zeros(num_sensors, 1); % 存储每个传感器的响应 for i = 1:num_sensors % 计算传感器位置坐标 sensor_position = [sensor_array_position(1) + (i-1) * sensor_spacing, sensor_array_position(2), sensor_array_position(3)]; % 计算声源到传感器的距离 distance = norm(sensor_position - source_position); % 计算声波传播时间(延时) propagation_time = distance / sound_speed; % 计算传感器响应(幅度乘积) sensor_response(i) = source_amplitude * exp(-1i * 2 * pi * frequency * propagation_time); end % 对传感器的响应进行求和 output_signal = sum(sensor_response); ``` 请注意,上述代码中的变量 `x_source`, `y_source`, `z_source` 代表声源的 x、y 和 z 坐标,`x_sensor`, `y_sensor`, `z_sensor` 代表传感器阵列的 x、y 和 z 坐标,`num_rows` 和 `num_columns` 分别代表传感器阵列的行数和列数,`spacing` 代表传感器间的距离,`amplitude` 代表声源的幅度,`sound_speed` 代表声速,`frequency` 代表声源的频率。您可以根据实际情况进行相应的修改。 希望这能帮到您!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值