DFS刷题

洛谷P2089烤鸡

#include<iostream>
using namespace std;
const int N = 20, M = 1000010;
int ans[N];
int dp[M][N];
int n, count;
void dfs(int x, int sum){if(sum > n)return;if(x > 10){if(sum == n){count++;for(int i = 1; i <= n; i++)dp[count][i] = ans[i];}return;}for(int i = 1; i <= 3; i++){ans[x] = i;dfs(x + 1, sum + i);ans[x] = 0; }
} 
int main(){cin >> n;dfs(1, 0);cout << count << endl;for(int i = 1; i <= count; i++){for(int j = 1; j <= 10; j++){cout << dp[i][j] << " ";}cout << endl;}return 0;
} 

洛谷P1088火星人

next_permutation函数

#include<iostream>
#include<algorithm>
using namespace std;
int n, m;
const int N = 10010;
int a[N];int main(){cin >> n >> m;for(int i = 1; i <= n; i++)cin >> a[i];if(m == 0){for(int i = 1; i <= n; i++)cout << a[i] << " ";}while(m--){next_permutation(a + 1, a + n + 1);}for(int i = 1; i <= n; i++)cout << a[i] << " "; return 0;
}

 DFS

#include<iostream>
#include<algorithm>
using namespace std;
int n, m;
const int N = 10010;
int ans[N], a[N];
bool st[N];
int cnt;
void dfs(int x){if(cnt >= m + 1)return;if(x > n){cnt++;if(cnt == m + 1){for(int i = 1; i <= n; i++)cout << ans[i] << " ";}return;}for(int i = 1; i <= n; i++){if(!cnt){i = a[x];}if(!st[i]){ans[x] = i;st[i] = true;dfs(x + 1);ans[x] = 0;st[i] = false;}}
}
int main(){cin >> n >> m;for(int i = 1; i <= n; i++)cin >> a[i];dfs(1);return 0;
}

洛谷P1149火柴棒

#include<iostream>
#include<algorithm>
using namespace std;
int n;
const int N = 10010;
int dist[N] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int arr[4];
int ans;
void dfs(int x, int sum){if(sum > n)return;if(x > 3){if(arr[1] + arr[2] == arr[3] && sum == n){ans++;}return;}for(int i = 0; i <= 1000; i++){arr[x] = i;dfs(x + 1, sum + dist[i]);arr[x] = 0;}
}
int main(){cin >> n;n -= 4;for(int i = 10; i <= 10000; i++){dist[i] = dist[i % 10] + dist[i / 10];}dfs(1, 0);cout << ans << endl;return 0;
}

洛谷P2036PERKET

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
const int N = 20;
int acid[N], bitter[N];
int ans = 0x7fffffff;
bool st[N];void dfs(int x){if(x > n){int sum1 = 1;int sum2 = 0;bool has_sc = false;for(int i = 1; i <= n; i++){if(st[i] != 0){has_sc = true;sum1 *= acid[i];sum2 += bitter[i];}if(has_sc)ans = min(ans, abs(sum1 - sum2));}return;}st[x] = true;dfs(x + 1);st[x] = false; dfs(x + 1); 
}
int main(){cin >> n;for(int i = 1; i <= n; i++)cin >> acid[i] >> bitter[i];dfs(1);cout << ans << endl;return 0;
}

洛谷P1135奇怪的电梯

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n, a, b;
const int N = 210;
int s[N];
int ans = 0x7fffffff;
bool st[N];
void dfs(int x, int cnt){if(cnt >= ans)return;if(x == b){ans = min(ans, cnt);return;}int up = x + s[x];if(up <= n && !st[up]){st[up] = true;dfs(x + s[x], cnt + 1);st[up] = false;}int down = x - s[x];if(down >= 1 && !st[down]){st[down] = true;dfs(x - s[x], cnt + 1);st[down] = false; }
}
int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> a >> b;for(int i = 1; i <= n; i++)cin >> s[i];st[a] = true;dfs(a, 0);if(ans == 0x7fffffff)cout << -1 << endl;else cout << ans << endl;return 0;
}

洛谷P1683

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n, m;
const int N = 25;
char mp[N][N];
bool st[N][N];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int ans = 1;
void dfs(int x, int y){for(int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if(a < 1 || a > n || b < 1 || b > m)continue;if(mp[a][b] != '.')continue;if(st[a][b])continue;st[a][b] = true;ans++;dfs(a, b);}
}
int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> m >> n;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin >> mp[i][j];}}for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){if(mp[i][j] == '@'){st[i][j] = true;dfs(i, j);}}}cout << ans << endl;return 0;
}

洛谷P1596

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 110;
char g[N][N];
bool st[N][N];
int n, m;
int ans;
int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
void dfs(int x, int y){for(int i = 0; i < 8; i++){int a = x + dx[i], b = y + dy[i];if(a < 1 || a > n || b < 1 || b > m)continue;if(st[a][b] || g[a][b] != 'W')continue;st[a][b] = true;dfs(a, b);}
}
int main(){cin >> n >> m;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin >> g[i][j];}}for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){if(g[i][j] == 'W' && !st[i][j]){ans++;st[i][j] = true;dfs(i, j);}}}cout << ans << endl; return 0;
}

acwing1114棋盘问题

#include<iostream>
#include<cstring>
using namespace std;
int n, m;
const int N = 10;
char g[N][N];
bool col[N];
int ans;
void dfs(int x, int cnt){if(cnt == m){ans++;return;}if(x > n)return;for(int i = 1; i <= n; i++){if(!col[i] && g[x][i] == '#'){col[i] = true;dfs(x + 1, cnt + 1);col[i] = false;}}dfs(x + 1, cnt);
}
int main(){while(cin >> n >> m, (n != -1 && m != -1)){memset(g, 0, sizeof(g));ans = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){cin >> g[i][j];}}memset(col, 0, sizeof(col));dfs(1, 0);cout << ans << endl;}return 0;
}

洛谷P1025数的划分

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n, k;
const int N = 210;
int ans;
void dfs(int x, int cnt, int start){if(x > k){if(cnt == 0)ans++;return;}if(x > k || cnt < 0)return;for(int i = start; i <= cnt;  i++){dfs(x + 1, cnt - i, i);}
}
int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> k;dfs(1, n, 1);cout << ans << endl;return 0;
}

洛谷P1019单词接龙

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
const int N = 25;
string str[N];
int g[N][N];//g[i][j]表示第i个字符串到j个的共同部分的长度 
int used[100];
int ans;
void dfs(string dragon, int x){ans = max(ans, (int)dragon.size());used[x]++;for(int i = 1; i <= n; i++){if(g[x][i] && used[i] < 2){dfs(dragon + str[i].substr(g[x][i]), i);}}used[x]--;
}
int main(){cin >> n;for(int i = 1; i <= n; i++)cin >> str[i];char start; cin >> start;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){for(int k = 1; k <= min(str[i].size(), str[j].size()); k++){string a = str[i], b = str[j];if(a.substr(a.size() - k, k) == b.substr(0, k)){g[i][j] = k;break;}}}}for(int i = 1; i <= n; i++){if(str[i][0] == start){dfs(str[i], i);}}cout << ans << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/73152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《Operating System Concepts》阅读笔记:p460-p4470

《Operating System Concepts》学习第 36 天&#xff0c;p460-p4470 总结&#xff0c;总计 11 页。 一、技术总结 无。 二、英语总结(生词&#xff1a;3) 1.lifespan (1)lifespan: life span(“the period of time that sth exists or happens”) c. 也写作 life-span, …

stratis,容器podman

一、stratis 1.stratis可以实现动态的在线扩容&#xff0c;lvm虽然也可以实现在线扩容&#xff0c;但是是需要人为的手动扩容。 2.stratis不需要手动格式化&#xff0c;自动会创建文件系统&#xff08;默认是xfs&#xff09; 1. 安装stratis软件包 yum list | grep stratis…

音频焦点 Android Audio Focus

Android 音频焦点详解 音频焦点&#xff08;Audio Focus&#xff09;是 Android 系统用于协调多个应用同时访问音频输出的机制。当多个应用需要播放音频时&#xff0c;音频焦点确保用户听到的内容不会混乱&#xff08;如多个音乐应用同时播放&#xff09;。以下从核心概念、使…

【用 Trace读源码】PlanAgent 执行流程

前提条件 在 Trae 中打开 OpenManus 工程&#xff0c;使用 build 模式&#xff0c;模型选择 claude-sonnet-3.7 提示词 分析 agent/planning.py 中 main 方法及相关类的执行流程&#xff0c;以流程图的方式展示PlanningAgent 执行流程图 以下流程图展示了 PlanningAgent 类…

1、双指针法

关于每个知识点的例题 可以自己看力扣标准题解。也可以在哔哩哔哩上看。想看我的&#xff0c;就到github 看 - 库 &#xff0c;介绍里写的算法讲解那些&#xff0c;里面有知识点&#xff0c;有题库。题库&#xff0c;每天都发题&#xff0c;可能跟博客的进度不一样。因为我上传…

LangChain 基础

一、LangChain 模块和体系 LangChain 是一个用于开发由大型语言模型&#xff08;LLMs&#xff09;驱动的应用程序的框架。 官方文档&#xff1a;https://python.langchain.com/docs/introduction/ LangChain 简化了LLM应用程序生命周期的每个阶段&#xff1a; 开发&#xf…

#echarts#折线图#饼图

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>折线图</title> </head> <body><div id"app" style"width:100%;height:100%;"><div id"chart-c…

Parsing error: Unexpected token, expected “,“

今天在使用Trae AI 编程工具开发大文件切片上传功能&#xff0c;使用的是VUE3,TS技术栈&#xff0c;开发完成运行时&#xff0c;编译报错&#xff08;Parsing error: Unexpected token, expected ","&#xff09;&#xff0c;让AI自行修复此问题多次后还是没有解决&a…

NLP高频面试题(九)——大模型常见的几种解码方案

大模型常见的几种解码方案 在自然语言生成任务中&#xff0c;如何从模型生成的概率分布中选择合适的词汇&#xff0c;是影响文本质量的关键问题。常见的解码方法包括贪心搜索&#xff08;Greedy Search&#xff09;、束搜索&#xff08;Beam Search&#xff09;、随机采样&…

农用车一键启动工作原理

移动管家农用车一键启动的工作原理与普通汽车类似&#xff0c;主要依赖于无线射频识别技术&#xff08;RFID&#xff09;。以下是具体的工作步骤和原理&#xff1a; 智能钥匙识别&#xff1a; 车主携带智能钥匙靠近车辆时&#xff0c;钥匙通过发射射频信号与车辆进行交互。车辆…

Cursor从小白到专家

文章目录 1&#xff1a;简单开发一个贪吃蛇游戏规则设置提示词 cursor开发小工具开发整体步骤创建.cursorrules输入提示词composer模式chat模式 执行cursor accept all发布到线上进行分享 cursor开发一个浏览器插件创建.cursorrulescursor rules范例集工具 输入提示词执行curso…

MAC+PHY 的硬件连接

文章目录 以太网的 MAC 与 PHY简介硬件拓扑CPU集成MAC与PHYCPU集成MAC&#xff0c;PHY采用独立芯片CPU不集成MAC与PHY&#xff0c;MAC与PHY采用集成芯片 在 OSI 分层中的位置MACPHYMAC 与 PHY 数据交互参考 本文为笔者学习以太网对网上资料归纳整理所做的笔记&#xff0c;文末均…

仿函数 VS 函数指针实现回调

前提&#xff1a; 本博客对比 函数指针实现回调 和 仿函数 &#xff0c;突出仿函数的优势。 目的&#xff1a; 一个类要能够灵活的调用两个函数&#xff0c;essfc 和 greaterfc&#xff0c;分别用于比较两个整数的大小&#xff1a; ①&#xff1a;lessfc&#xff1a;判断 x …

CH32V208蓝牙内部带运放32位RISC-V工业级微控制器

开发板 CH32V208CBU6立创格式的开发板上述链接可下载&#xff0c;官方文件进行了转换&#xff0c;使用前请仔细核对。 CH32V208CBU6原理图&#xff0c;上述图片为芯片部分。已进行DRC。 CH32V208CBU6 PCB三维图&#xff0c;上述图片为芯片部分。已进行DRC。 概述 CH32V208C…

整理和总结微信小程序的高频知识点

前言 近期萌生了一些想法&#xff0c;感觉可以做一个小程序作为产出。 但小程序做得比较少&#xff0c;因此边做边复习。整理和总结了一些高频知识点和大家一起分享。 一、模板和组件 1.1模板&#xff08;Template&#xff09; 优势 简单灵活&#xff1a;模板定义和使用都较…

1996-2023年各省公路里程数据(无缺失)

1996-2023年各省公路里程数据&#xff08;无缺失&#xff09; 1、时间&#xff1a;1996-2023年 2、来源&#xff1a;国家统计局、统计年鉴 3、指标&#xff1a;公路里程&#xff08;万公里&#xff09; 4、范围&#xff1a;31省 5、指标解释&#xff1a;公路里程指报告期末…

SEARCH-R1:大型语言模型的多轮搜索推理革命

当AI学会"边搜索边思考" 2025年&#xff0c;语言模型领域迎来重大突破——SEARCH-R1框架通过强化学习&#xff08;RL&#xff09;让大模型实现"动态搜索自主推理"的协同进化。这项技术不仅让模型在回答"泰坦尼克号沉没时的船长是谁"时能自动检索…

Wi-Fi NAN 架构(Wi-Fi Aware Specification v4.0,第2章:2.7~2.9)

1. NAN 介质访问控制层&#xff08;MAC&#xff09; NAN MAC负责通过参与 NAN同步信标帧&#xff08;NAN Synchronization Beacon frame&#xff09;的传输&#xff0c;获取并维护设备所在的NAN集群的同步。作为同步功能的一部分&#xff0c;NAN MAC运行 TSF 定时器。NAN MAC还…

基于物联网的便携式土壤综合参数检测仪设计

标题:基于物联网的便携式土壤综合参数检测仪设计 内容:1.摘要 随着农业现代化和环境监测需求的不断增长&#xff0c;对土壤综合参数的实时、准确检测变得至关重要。本研究旨在设计一种基于物联网的便携式土壤综合参数检测仪&#xff0c;以满足现场快速检测和数据远程传输的需求…

《Android 13深度定制:手势拦截技术实现SystemUI状态栏智能折叠方案》

核心机制解析 在Android 13的SystemUI定制中&#xff0c;状态栏下拉行为由NotificationPanelViewController控制&#xff0c;其核心逻辑聚焦于手势事件处理和布局动态调整。当用户执行下拉操作时&#xff0c;系统通过onQsIntercept方法拦截滑动事件&#xff0c;并调用setQsExp…