蓝桥冲刺31天打卡—Day18

目录

1.🌟找素数

📕题目描述:

☀️思路:

✏️代码 : 

2.🌟分考场

📕题目描述:

☀️思路:

✏️代码 : 

3.🌟合根植物

📕题目描述:

☀️思路:

✏️代码 : 

4.🌟大胖子走迷宫

📕题目描述:

☀️思路

✏️代码


1.🌟找素数

📕题目描述

☀️思路:

此题在dev里运行出答案输出,不用筛法也行,直接for循环判断是否为素数,当素数个数为100002时输出当前数。

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int i;
bool f(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0) return 0;
	}
	return 1;
}

int main(){
	int ans=0;
	for( i=2;;i++){
		if(f(i)) ans++;
		if(ans==100002) break;
	}
	cout<<i;
	return 0;
}//1299743

2.🌟分考场

📕题目描述

输入:

5
8
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5

输出

4

☀️思路:

dfs,这题不怎么会做,参考了大佬的想法思路

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110 ;
int n;
int num = 110; //记录所需考场数
int s[N][N];//表示两者关系,1代表认识,0不认识
int p[N][N];//记录第i个考场第j个位坐的考生的序号

void dfs(int u, int x) { //考生号(数量)u,考场数x
	if (x >= num) return; //考场数超过上限
	
	if (u > n) { //当考生全部放好时
		num = min(num,x); //最少考场数
		return;
	}

	for (int  i = 1; i <= x; i++) { //枚举所有考场
			int j = 0; //代表每个考场的座位号
		while (p[i][j] && !s[u][p[i][j]]) j++; //空位且考场内无有关系人

		if (p[i][j] == 0) { //此位置没有人
			p[i][j] = u; //放入考生
			
			dfs(u + 1, x);//放下一个学生
			
			p[i][j] = 0; //回溯
		}
	}
	p[x + 1][0] = u; //新开辟考场
	dfs(u + 1, x + 1);
	p[x + 1][0] = 0; //回溯
	
}
int main() {
	int a,b,m;
	cin >> n >> m;
	for (int k = 1; k <= m; k++) {
		cin >> a >> b;
		s[a][b] = 1, s[b][a] = 1;
	}
	dfs(1, 1);
	cout << num;
	return 0;
}

3.🌟合根植物

📕题目描述

输入:

5 4
16
2 3
1 5
5 9
4 8
7 8
9 10
10 11
11 12
10 14
12 16
14 18
17 18
15 19
19 20
 9 13
13 17

输出

5

☀️思路:

并查集,最后判断每个点的祖宗结点是否为它本身

✏️代码 : 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e6+10 ;
int p[N];
int n,m,k;
int a,b;
int ans;
void init(int n){//初始化并查集
	for (int i = 1; i <= n; i ++ ) p[i] = i;
}
int find(int x)//并查集
{
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
}
//p[find(a)] = find(b);合并集合
int main(){
	cin>>n>>m>>k;
	
	int num=n*m;
	
	init(num);
	
	while(k--){
		cin>>a>>b;
		p[find(a)]=find(b);
	}
	int ans=0;
	for(int i=1;i<=num;i++){
		if(p[i]==i) ans++;//当当前结点的父节点是它本身时,说明此结点是一颗合根植物
	}
	//合根植物数即父节点是本身的结点数
	cout<<ans;
	return 0;
}

4.🌟大胖子走迷宫

📕题目描述

 

☀️思路

题意要求求最短时间,也就是最短路问题,使用bfs

设置一个变量w储存小明的体积,根据时间范围改变体积

  • 判断障碍时要对以当前点为中心,小明体积w的为边长的矩阵中判断是否有障碍。
  • 越界判断时也要记得加上小明的体积w
  • 注意当小明体积不为1时,会因为太胖而无法走,停在原地。

参考群内永遇乐金枪鱼大佬的代码

第一遍敲不知道为什么跑不出来(代码逻辑没错),后来删掉代码重新打了一遍就过了(很迷惑)

✏️代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
using namespace std;
const int N = 400 ;
char mp[N][N];
bool vis[N][N];
int n, k;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int w;//小明体积
struct Node {
	int x;
	int y;
	int t;
};

bool check(int a, int b) {

	if (a - w / 2 < 1 || b - w / 2 < 1 || a + w / 2 > n || b + w / 2 > n) return 0;//越界


	for (int i = a - w / 2; i <= a + w / 2; i++) { //障碍
		for (int j = b - w / 2; j <= b + w / 2; j++) {
			if (mp[i][j] == '*') return 0;
		}
	}

	return 1;
}
void bfs() {


	queue<Node>q;
	q.push({3, 3, 0});
	vis[3][3] = 1;

	while (q.size()) {
		Node s = q.front();
		q.pop();


		if (s.x == n - 2 && s.y == n - 2) {//到达终点
			cout << s.t;
			return;
		}
			
		//体积变化
		if (s.t < k) w = 5;
		else if (s.t >= k && s.t < 2 * k) w = 3;
		else if (s.t >= 2 * k) w = 1;

		if (w != 1) q.push({s.x, s.y, s.t + 1}); //保持不动


		for (int i = 0; i < 4; i++) {
			int fx = s.x + dx[i], fy = s.y + dy[i];
			if (check(fx, fy) && !vis[fx][fy]) {

				vis[fx][fy] = 1;
				q.push({fx, fy, s.t + 1});
			}

		}
	}

}
int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> mp[i][j];

	bfs();

	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、付费专栏及课程。

余额充值