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

算法训练营|图论第4天 110.字符串接龙 105.有向图的完全可达性 106.岛屿的周长

题目:110.字符串接龙

题目链接:

110. 字符串接龙 (kamacoder.com)
代码:

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main() {
	int n;
	cin >> n;
	string beginStr, endStr;
	cin >> beginStr >> endStr;
	set<string>MySet;
	for (int i = 0; i < n; i++) {
		string str;
		cin >> str;
		MySet.insert(str);
	}
	unordered_map<string, int>MyMap;
	MyMap.insert(pair<string, int>(beginStr, 1));
	queue<string>que;
	que.push(beginStr);
	while (!que.empty()) {
		string word = que.front();
		que.pop();
		int path = MyMap[word];
		for (int i = 0; i < word.size(); i++) {
			string newWord = word;
			for (int j = 0; j < 26; j++) {
				newWord[i] = 'a' + j;
				if (newWord == endStr) {
					cout << path + 1 << endl;
					return 0;
				}
				else {
					if (MySet.count(newWord) && !MyMap.count(newWord)) {
						MyMap.insert(pair<string, int>(newWord, path + 1));
						que.push(newWord);
					}
				}
			}
		}
	}
	cout << 0 << endl;
	return 0;
}

题目:105.有向图的完全可达性

题目链接:

105. 有向图的完全可达性 (kamacoder.com)

代码:

dfs:

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
void dfs(vector<list<int>>& vec, vector<bool>& visited, int key) {
	if (visited[key]) return;
	visited[key] = true;
	for (auto t : vec[key]) {
		dfs(vec, visited, t);
	}
}
int main() {
	int n, k;
	cin >> n >> k;
	vector<list<int>>vec(n + 1);
	for (int i = 0; i < k; i++) {
		int s, t;
		cin >> s >> t;
		vec[s].push_back(t);
	}
	vector<bool>visited(n + 1, false);
	dfs(vec, visited, 1);
	for (int i = 1; i <= n; i++) {
		if (visited[i] == false) {
			cout << -1 << endl;
			return 0;
		}
	}
	cout << 1 << endl;
	return 0;
}

 bfs:

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
void bfs(vector<list<int>>& vec, vector<bool>& visited, int key) {
	queue<int>que;
	que.push(key);
	visited[key] = true;
	while (!que.empty()) {
		int cur = que.front();
		que.pop();
		visited[cur] = true;
		for (auto t : vec[cur]) {
			if (visited[t]) continue;
			que.push(t);
		}
	}
}
int main() {
	int n, k;
	cin >> n >> k;
	vector<list<int>>vec(n + 1);
	for (int i = 0; i < k; i++) {
		int s, t;
		cin >> s >> t;
		vec[s].push_back(t);
	}
	vector<bool>visited(n + 1, false);
	bfs(vec, visited, 1);
	for (int i = 1; i <= n; i++) {
		if (visited[i] == false) {
			cout << -1 << endl;
			return 0;
		}
	}
	cout << 1 << endl;
	return 0;
}

题目:106.岛屿的周长

题目链接:

106. 岛屿的周长 (kamacoder.com)

代码:

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (grid[i][j] == 1) {
				for (int k = 0; k < 4; k++) {
					int x = i + dir[k][0];
					int y = j + dir[k][1];
					if (x < 0 || y < 0 || y >= grid[0].size() || x >= grid.size() || grid[x][y] == 0) {
						result++;
					}
				}
			}
		}
	}
	cout << result << endl;
}


http://www.kler.cn/news/283285.html

相关文章:

  • 网络原理 TCP与UDP协议
  • 本地构建spotbugs,替换gradle的默认仓库地址。
  • chapter08-面向对象编程——(Object类详解)——day09
  • 【C++ Primer Plus习题】7.5
  • Docker方式部署K8s集群
  • 灵神算法题单——不定长滑动窗口(求最长最大)
  • C#入门(13)if语句
  • HTML简单了解和基础知识记录
  • 《机器学习》 基于GANs构建数字图像生成器 探索深度学习世界
  • 群晖(Docker Compose)配置 frp 服务
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueuepriority_queue(模拟实现)
  • zset使用lua实现取最高分数中的随机成员
  • 使用notepad++将shell脚本转为UNIX格式方法(主要差别在换行符)
  • MySQL中的锁详解
  • AndroidStudio无线连接Android手机进行调试
  • 利润暴涨507%的携程,做对了什么?
  • C++/Qt 多媒体(续三)
  • 酒店管理系统小程序(包含源码C++实现)
  • 生成和应用patch
  • Redis入门篇 - CentOS 7下载、安装Redis实操演示
  • 每天学习一个基础算法之顺序查找
  • Python观察者模式:构建松耦合的通信机制
  • 深入理解归并排序
  • C++,如何写单元测试用例?
  • PHP语言有哪些优势和特点?
  • C语言通用函数 - 判断ip是否合法
  • 顺序表和链表知识点
  • 运维学习————Docker自制镜像并上传至阿里云以及Docker Compose的使用
  • vmware解决虚拟机空间占用不断增大问题
  • FFmpeg源码:ffurl_seek2、ffurl_seek、avio_size函数分析