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

算法训练营day67

题目1:

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <queue>using namespace std;int main() {string beginStr, endStr;int n;cin >> n;cin >> beginStr >> endStr;unordered_set<string> set;for(int i = 0;i < n;i++) {string str;cin >> str;set.insert(str);}unordered_map<string, int> visitedmp;visitedmp.insert(pair<string, int>(beginStr, 1));queue<string> qu;qu.push(beginStr);while(!qu.empty()) {string word = qu.front();qu.pop();int path = visitedmp[word];for(int i = 0;i < word.size();i++) {string newword = word;for(int j = 0;j < 26;j++) {newword[i] = j + 'a';if(newword == endStr) {cout << path + 1 << endl;return 0;}if(visitedmp.find(newword) == visitedmp.end() && set.find(newword) != set.end()) {visitedmp.insert(pair<string, int>(newword, path + 1));qu.push(newword);}}}}cout << 0 << endl;return 0;
}

题目2:105. 有向图的完全可达性 (kamacoder.com)

#include<bits/stdc++.h>using namespace std;int n, m;void dfs(vector<vector<int>>& map, int key, vector<bool>& visited) {if(visited[key]) {return;}visited[key] = true;for(int i = 1;i <= n;i++) {if(map[key][i] == 1) {dfs(map, i, visited);}}
}int main() {cin >> n >> m;vector<vector<int>> map(n + 1, vector<int>(n + 1, 0));while(m--) {int i,j;cin >> i >> j;map[i][j] = 1;}vector<bool> visited(n + 1, false);dfs(map, 1, visited);for(int i = 1;i <= n;i++) {if(!visited[i]) {cout << -1 << endl;return 0;}}cout << 1 << endl;return 0;
}

题目3:106. 岛屿的周长 (kamacoder.com)

// 其实可以不用dfs因为题目说了中间岛屿是联通的
#include<iostream>
#include<vector>using namespace std;
int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
int n, m;
vector<pair<int, int>> result;
int count = 0;void dfs(vector<vector<int>>& map, vector<vector<bool>>& visited, int x, int y) {if(map[x][y] == 0 || visited[x][y]) {return;}result.push_back(pair<int, int>(x, y));visited[x][y] = true;for(int i = 0;i < 4;i++) {int nextx = x + dir[i][0];int nexty = y + dir[i][1];if(nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue;dfs(map, visited, nextx, nexty);}
}int main() {cin >> n >> m;vector<vector<int>> map(n, vector<int>(m));for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {cin >> map[i][j];}}vector<vector<bool>> visited(n, vector<bool>(m, false));for(int i = 0;i < n;i++) {for(int j= 0;j < m;j++) {if(map[i][j] != 0 && !visited[i][j]) {dfs(map, visited, i, j);}}}for(int i = 0;i < result.size();i++) {int x = result[i].first;int y = result[i].second;for(int j = 0;j < 4;j++) {int nextx = x + dir[j][0];int nexty = y + dir[j][1];if(nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) {if(nextx == -1 || nextx == n || nexty == -1 || nexty == m) count++;continue;}if(map[nextx][nexty] == 0) count++;}}cout << count << endl;return 0;
}

http://www.lryc.cn/news/390696.html

相关文章:

  • 人工智能--图像语义分割
  • fl studio20和21用哪一个好?FL-Chan from FL Studio欣赏
  • OpenCV直方图计算函数calcHist的使用
  • 09 docker 安装tomcat 详解
  • 44.实现管理HOOK点的链表对象
  • Unity小知识
  • 【Jupyter Notebook与Git完美融合】在Notebook中驾驭版本控制的艺术
  • Python开发者必看:内存优化的实战技巧
  • Golang | Leetcode Golang题解之第214题最短回文串
  • 【ajax实战08】分页功能
  • 基于Hadoop平台的电信客服数据的处理与分析②项目分析与设计---需求分析-项目场景引入
  • debug-mmlab
  • 年轻人为什么那么爱喝奶茶?
  • 手写数组去重
  • Firewalld 防火墙
  • Hive查询优化 - 面试工作不走弯路
  • 【VUE3】uniapp + vite中 uni.scss 使用 /deep/ 不生效(踩坑记录三)
  • 容器部署rabbitmq集群迁移
  • DP:背包问题----0/1背包问题
  • React antd umi 监听当前页面离开,在菜单栏提示操作
  • 在 Windows PowerShell 中模拟 Unix/Linux 的 touch 命令
  • 鸿蒙NEXT
  • VUE3-Elementplus-form表单-笔记
  • Analyze an ORA-12801分析并行 parallel 12801 实际原因
  • 高级运维工程师讲述银河麒麟V10SP1服务器加固收回权限/tmp命令引起生产mysql数据库事故实战
  • 昇思25天学习打卡营第09天|sea_fish
  • flutter开发实战-Charles抓包设置,dio网络代理
  • Elasticsearch:Runtime fields - 运行时字段(二)
  • Python正则表达式的入门用法(上)
  • Audio Processing Graphs 管理 Audio Units