蓝桥冲刺31天打卡-Day28

目录

1.🌟36进制

📕题目描述:

☀️思路:

✏️代码 : 

2.🌟交换瓶子

📕题目描述:

☀️思路:

✏️代码 : 

3.🌟路径之谜

📕题目描述:

☀️思路:

✏️代码 : 


1.🌟36进制

📕题目描述

☀️思路:

n进制转换为10进制

从右到左用每个数去乘以n的相应次方,然后将这些数字相加就是了。

例如:

ABC=10*36^2+11*36^1+12*36^0=13368

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<math.h>
using namespace std;
string a;
int n;
int main() {
	a = "MANY";
	int cnt = 0;
	for (int i = a.size() - 1; i >= 0; i--) {
		int t = a[i] - 'A' + 10;
		n += t * pow(36, cnt);
		cnt++;
	}
	cout << n;
	return 0;
}

2.🌟交换瓶子

📕题目描述

 

输入

5

3 1 2 5 4

输出

3

☀️思路:

根据题意我们可以看出,目标序列的每个下标与其对应的 瓶子序号相同。

因此我们可以直接对序列进行枚举,当当前瓶子的序号a[i]和其下标i不同时,再从i+1往后进行枚举,找到对应的 a[j] 和的下标 i 相等,然后将两者交换,交换次数 ans+1

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10 ;
int n;
int a[N];
int main() {
	cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		if (a[i] != i) {
			for (int j = i+1; j <= n; j++) {
				if (a[j] == i) {
					swap(a[i], a[j]);
					ans++;
					break;
				}
			}
		}
	}
	cout << ans;
	return 0;
}

3.🌟路径之谜

📕题目描述

 

 

 

输入:

4

2 4 3 4

4 3 3 3

输出

0 4 5 1 2 3 7 11 10 9 13 14 15

☀️思路:

dfs题,第一次做大致思路和框架写出来了,但是WA了,后来看了题解发现我只在移动每一步时判断了箭靶的箭数是否大于0。 在运行到终点时还要对每个箭靶数量的箭数进行判断,所有箭靶的箭数为0才能输出此路径

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
const int N = 50 ;
//int mp[N][N];
int n;
bool vis[N][N];//标记是否已走过
int x[N], y[N];//靶子箭数
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int road[N * N]; //储存路径
//(左边)x=下标/4
//(上面) y=下标%4
//每走一步将两靶子箭数量减一
bool check() { //判断到终点时靶子的箭是否用完
	for (int i = 0; i < n; i++) {
		if (x[i] != 0 || y[i] != 0) return false; //靶子箭数未清零
	}
	return true;
}
bool flag;
void dfs(int xx, int yy, int step) { //每一次只能走一步,用step来记录步数

	road[step] = (xx * n + yy); //加入路径数组
	if (x[xx] < 0 || y[yy] < 0) return ;//箭靶数不够(可以不考虑)

	if (xx == n - 1 && yy == n - 1) {
		flag = 1;
		if (!check()) {//到终点后没有用完箭说明此路径非法
			flag = 0;
		}
		if (flag) {
			for (auto t : road) {
				if (t) cout << t << " ";
			}
		}
		return;
	}


	for (int i = 0; i < 4; i++) {
		int fx = xx + dx[i], fy = yy + dy[i];
		if (fx >= 0 && fx < n && fy >= 0 && fy < n && !vis[fx][fy] && x[fx] > 0 && y[fy] > 0) { //在范围内
			x[fx]--, y[fy]--;
			vis[fx][fy] = 1;

			dfs(fx, fy, step + 1); //走下一步

			vis[fx][fy] = 0; //回溯
			x[fx]++, y[fy]++;
		}
	}

}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> y[i];
	for (int i = 0; i < n; i++) cin >> x[i];

	x[0]--, y[0]--; //起点的靶子提前减去一箭
	//vis[0][0] = 1;
	cout << 0 << " ";
	dfs(0, 0, 0);
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月色美兮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值