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

DFS入门刷题

目录

P1683 入门

P1596 [USACO10OCT] Lake Counting S

1114. 棋盘问题

P1025 [NOIP 2001 提高组] 数的划分


P1683 入门

#include <iostream>
using namespace std;
char a[30][30];
bool vis[30][30];
int res = 1;
int n, m;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int x, int y)
{for (int i = 0; i < 4; i++){int nx = x + dx[i];int ny = y + dy[i];if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && a[nx][ny] != '#' && !vis[nx][ny]){vis[nx][ny] = true;res++;dfs(nx, ny);}}
}
int main()
{cin >> m >> n;int x, y;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> a[i][j];if (a[i][j] == '@'){x = i;y = j;vis[i][j] = true;}}}dfs(x, y);cout << res;return 0;
}

P1596 [USACO10OCT] Lake Counting S

#include <iostream>
using namespace std;
char a[105][105];
int n, m;
int res;
bool vis[105][105];
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, -1, 1, -1, 1, 1, -1};
void dfs(int x, int y)
{for (int i = 0; i < 8; i++){int nx = x + dx[i];int ny = y + dy[i];if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && a[nx][ny] == 'W' && !vis[nx][ny]){vis[nx][ny] = true;dfs(nx, ny);}}
}
int main()
{cin >> n >> m;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> a[i][j];}}for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (a[i][j] == 'W' && !vis[i][j]){res++;dfs(i, j);}}}cout << res;return 0;
}

1114. 棋盘问题

#include <iostream>
using namespace std;
int n, k;
bool vis[10];
char a[10][10];
int res;
void dfs(int x, int count)
{if (k == count){res++;return;}if (x > n){return;}for (int i = 1; i <= n; i++){if (!vis[i] && a[x][i] == '#'){vis[i] = true;dfs(x + 1, count + 1);vis[i] = false;}}dfs(x + 1, count); // 强行访问
}
int main()
{while (cin >> n >> k, n > 0 && k > 0){for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cin >> a[i][j];}}res = 0;dfs(1, 0);cout << res << "\n";}return 0;
}

P1025 [NOIP 2001 提高组] 数的划分

#include <iostream>
using namespace std;
int n, k;
int res;
int a[10];
void dfs(int x, int sum, int start)
{if (sum > n){return;}if (x > k){if (sum == n){res++;}return;}for (int i = start; sum + (k - x + 1) * i <= n; i++){a[x] = i;dfs(x + 1, sum + i, i);a[x] = 0;}
}
int main()
{cin >> n >> k;dfs(1, 0, 1);cout << res;return 0;
}

http://www.xdnf.cn/news/749557.html

相关文章:

  • vasp的输出文件解读--OUTCAR
  • 常见的RAG文档解析辅助工具汇总及企业选型思考
  • 一周学会Pandas2之Python数据处理与分析-数据重塑与透视-pivot() - 透视 (长 -> 宽,有限制)
  • [SC]SystemC在CPU/GPU验证中的应用(四)
  • 图像修复的可视化demo代码
  • PostIn入门教程 - 使用IDEA插件快速生成API接口定义
  • 流媒体基础分析:延迟分析与安全性保障
  • 牛客小白月赛117
  • Baklib知识中台驱动服务升级
  • OD 算法题 B卷【模拟消息队列】
  • Linux环境搭建MCU开发环境
  • [001]从操作系统层面看锁的逻辑
  • 计算机组织原理第三章
  • LearnOpenGL-笔记-其十二
  • 《高等数学》(同济大学·第7版) 的 详细章节目录
  • 顺序查找与折半查找
  • [python]Prophet‘ object has no attribute ‘stan_backend‘解决方法
  • Lyra学习笔记 Experience流程梳理
  • 5月31日day41打卡
  • 【Unity笔记】Unity WASD+QE 控制角色移动与转向(含 Shift 加速)实现教程
  • Qt -使用OpenCV得到SDF
  • thinkpad T-440p 2025.05.31
  • YOLOv10速度提升与参数缩减解析2025.5.31
  • 华为OD机试_2025 B卷_静态扫描(Python,100分)(附详细解题思路)
  • SAR ADC 同步逻辑设计
  • 【CBAP50技术手册】#31 Observation(观察法):BA(业务分析师)的“现场侦探术”
  • Kerberos面试内容整理-Kerberos 与 LDAP/Active Directory 的集成
  • 关于TongWeb数据源兼容mysql驱动的注意事项
  • 基于晶体塑性有限元(CPFEM)的钛合金圆棒拉伸过程模拟
  • 元胞自动机(Cellular Automata, CA)