目录
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;
}

本文解析了四道经典算法题目,包括寻找素数、考场分配、合根植物问题及大胖子走迷宫,涵盖素数判断、图论、并查集和广度优先搜索等知识点。

1100

被折叠的 条评论
为什么被折叠?



