当前位置: 首页 > news >正文

搜索+动态规划

刷题刷题刷题刷题

​​​​​​​​​​​​​​Forgery - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

需要两个数组,一个数组全部初始化为".",另一个数组输入数据,每碰到一个“.”就进行染色操作,将其周围的8个全部变成"#",全部染完色之后将两个数组进行比对,若完全一样则YES,否则NO

AC代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
char a[1010][1010];
char b[1010][1010];
int n, m;
int flag;
bool cmp()
{for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)if (a[i][j] != b[i][j])return false;return true;
}
void dfs(int x, int y)
{flag = 1;int next[8][2] = { {1,1},{-1,-1},{-1,0},{1,0},{0,1},{0,-1},{1,-1},{-1,1} };for (int i = 0; i < 8; i++){int tx = x + next[i][0];int ty = y + next[i][1];if (tx<1 || ty<1 || tx>n || ty>m){flag = 0;return;}if (a[tx][ty] != '#'){flag = 0;break;}}if (!flag)return;for (int i = 0; i < 8; i++){int tx = x + next[i][0];int ty = y + next[i][1];b[tx][ty] = '#';}
}
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++){cin >> a[i][j];b[i][j] = '.';}for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++){dfs(i, j);}if (cmp())cout << "YES";else{cout << "NO";}return 0;
}

P1460 [USACO2.1] 健康的荷斯坦奶牛 Healthy Holsteins - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

开始没写出来,看了题解后有了一些头绪(感觉应该有普及+的难度了)。

用数组记录需要种类数的最小量和不同饲料不同种类的最小量,

从第一种饲料开始进入搜索,每次搜索进行一次判断,若饲料数已超过数据则马上结束这一次搜索(对当前饲料进行搜索,用数组记录),得到需要的饲料数,并用数组存起来所需的饲料编号,对所有饲料的相同一种的维他命进行判断,得到最优解,继续进行搜索有两种可能(一是不算当前饲料,即饲料数加1,但是所需饲料数不加1,二是当前饲料数和所需饲料数都加1),最后所有搜索结束后按照题目要求输出即可

AC:

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
int a[1010];
int b[1010][1010];
int ans[1010];
int n, m, k=20;
int s[1010];
bool judge(int x)
{for (int i = 1; i <= n; i++){int sum = 0;for (int j = 1; j <= x; j++){sum += b[s[j]][i];}if (sum < a[i])return false;}return true;
}
void dfs(int pos, int num)
{if (pos > m){if (judge(num) && num < k){k = num;for (int i = 1; i <=  num; i++){ans[i] = s[i];}}return;}s[num + 1] = pos;dfs(pos + 1, num + 1);dfs(pos + 1, num);
}
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n;for (int i = 1; i <= n; i++){cin >> a[i];}cin >> m;for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){cin >> b[i][j];}}dfs(1, 0);cout << k << " ";for (int i = 1; i <= k; i++)cout << ans[i] << " ";cout << endl;return 0;
}

01背包思路(太久远了,忘了)

来几道板子题唤醒一下沉睡的记忆

P1060 [NOIP2006 普及组] 开心的金明 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

AC:

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
int v[100010], w[100010];
int dp[1010][30010];
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;for (int i = 1; i <= m; i++){cin >> v[i] >> w[i];w[i] = w[i] * v[i];}for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){if (v[i] > j){dp[i][j] = dp[i - 1][j];}else{dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);}}}cout << dp[m][n];return 0;
}

P1049 [NOIP2001 普及组] 装箱问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

AC:

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
int dp[35][20010];
int w[20010];
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int v, n;cin >> v >> n;for (int i = 1; i <= n; i++){cin >> w[i];}for (int i = 1; i <= n; i++){for (int j = 1; j <= v; j++){if (w[i] > j){dp[i][j] = dp[i - 1][j];}else{dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]]+w[i]);}}}cout << v-dp[n][v];return 0;
}

P1048 [NOIP2005 普及组] 采药 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
int w[110];
int t[1100];
int dp[110][1100];
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;for (int i = 1; i <= m; i++){cin >> t[i] >> w[i];}for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){if (t[i] > j)dp[i][j] = dp[i - 1][j];else{dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - t[i]] + w[i]);}}}cout << dp[m][n];return 0;
}

分组背包问题(卡了半天也并不知道哪错了,无语)

P1757 通天之分组背包 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
int a[1010], b[1010], c[1010];
int dp[1010];
int g[1010][1010];
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int n, m, t, x = 0;cin >> m >> n;for (int i = 1; i <= n; i++){cin >> a[i] >> b[i] >> t;x = max(t, x);c[t]++;g[t][c[t]] = i;}for (int i = 1; i <= x; i++)//小组{for (int j = m; j >= 0; j--)//容量{for (int k = 1; k <= c[i]; k++)//小组成员{if(a[g[i][k]]<=j)dp[j] = max(dp[j], dp[j - a[g[i][k]]] + b[g[i][k]]);}}}cout << dp[m] << endl;return 0;
}
http://www.lryc.cn/news/392551.html

相关文章:

  • strcpy,srtcmp,strlen函数漏洞利用
  • SketchUp + Enscape+ HTC Focus3 VR
  • 推荐3款Windows系统的神级软件,免费、轻量、绝对好用!
  • -bash: /snap/bin/docker: 没有那个文件或目录
  • [深度学习]卷积理解
  • 基于aardio web.view2库和python playwright包的内嵌浏览器自动化操作
  • 《数据仓库与数据挖掘》 总复习
  • EtherCAT主站IGH-- 8 -- IGH之domain.h/c文件解析
  • 《昇思25天学习打卡营第10天|使用静态图加速》
  • 【HarmonyOS4学习笔记】《HarmonyOS4+NEXT星河版入门到企业级实战教程》课程学习笔记(二十二)
  • 六西格玛绿带培训如何告别“走过场”?落地生根
  • Linux——提取包文件到指定目录,命令解释器-shell,type 命令
  • 【最详细】PhotoScan(MetaShape)全流程教程
  • Excel多表格合并
  • AI作画工具深度剖析:Midjourney vs. Stable Diffusion (SD)
  • ASP.NET Core Blazor 5:Blazor表单和数据
  • C++ 仿QT信号槽二
  • 联合概率密度函数
  • 【Java10】成员变量与局部变量
  • Spring Session与分布式会话管理详解
  • 从0开始学习pyspark--Spark DataFrame数据的选取与访问[第5节]
  • Fastjson首字母大小写问题
  • GuLi商城-商品服务-API-品牌管理-效果优化与快速显示开关
  • 如何成为C#编程高手?
  • SpringBoot学习06-[SpringBoot与AOP、SpringBoot自定义starter]
  • Maven - 在没有网络的情况下强制使用本地jar包
  • JAVA--JSON转换工具类
  • 每日复盘-20240705
  • MySQL 一些用来做比较的函数
  • 一个使用率超高的大数据实验室是如何练成的?