Commit d496c4e0 authored by CDK6182CHR's avatar CDK6182CHR
Browse files

2021.6.3

parent 539b24bd
......@@ -6,7 +6,7 @@
/// <summary>
/// 2021.06.03
/// 标准二搜索树。暂不考虑数据域
/// 标准二搜索树。暂不考虑数据域
/// </summary>
template <typename K>
class BST {
......
......@@ -7,3 +7,5 @@ Implementation of classic data structure and some algorithm for OJ of Data Struc
一些数据结构仅根据要求实现了部分功能;有的功能不允许操作或者声明了函数而没写实现。
注意编译器必须支持`C++11`标准。
/**
* 使用邻接矩阵表示的有向图。
*/
#include <iostream>
#include <utility>
#include <limits>
/**
* 二维数组的简单封装,使用连续空间,运算下标
*/
template <typename T>
class Matrix {
int row, col;
T* data;
public:
Matrix(int row_, int col_);
~Matrix();
T& get(int i, int j);
T* get_pointer(int i, int j);
inline T* get_pointer_serial(int n) {
return data + n;
}
inline const T& get(int i, int j)const {
return const_cast<Matrix*>(this)->get(i, j);
}
inline int get_row()const {
return row;
}
inline int get_col()const {
return col;
}
void fill(const T& d);
Matrix(const Matrix&) = delete;
Matrix(Matrix&&) = delete;
Matrix& operator=(const Matrix&) = delete;
Matrix& operator=(Matrix&&) = delete;
void show()const;
};
template<typename T>
Matrix<T>::Matrix(int row_, int col_) :row(row_), col(col_)
{
data = new T[row * col];
}
template<typename T>
Matrix<T>::~Matrix()
{
delete[] data;
data = nullptr;
}
template<typename T>
T& Matrix<T>::get(int i, int j)
{
return data[i * col + j];
}
template<typename T>
T* Matrix<T>::get_pointer(int i, int j)
{
return data + i * col + j;
}
template<typename T>
void Matrix<T>::fill(const T& d)
{
std::fill(data, data + row * col, d);
}
template<typename T>
void Matrix<T>::show() const
{
std::cout << '[' << std::endl;
for (int i = 0; i < row; i++) {
std::cout << ' ';
for (int j = 0; j < col; j++) {
std::cout << get(i, j) << ' ';
}
std::cout << std::endl;
}
std::cout << ']' << std::endl;
}
/// <summary>
/// 2021.06.03
/// 邻接矩阵表示的图
/// </summary>
template <typename E>
class MatGraph {
Matrix<E> mat;
int size;
public:
static constexpr E MAX = std::numeric_limits<E>::max()-1;
MatGraph(int size_);
//冲突时只保留最短边
void add_edge(int u, int v, const E& data);
void add_biedge(int u, int v, const E& data);
inline int get_edata(int u, int v)const {
return mat.get(u, v);
}
//-1表示不存在
int first_neigh(int u)const;
int next_neigh(int u, int v)const;
inline int get_size()const {
return size;
}
};
template<typename E>
MatGraph<E>::MatGraph(int size_): size(size_), mat(size_, size_)
{
mat.fill(MAX);
for (int i = 0; i < size; i++) {
mat.get(i, i) = 0;
}
}
template<typename E>
void MatGraph<E>::add_edge(int u, int v, const E& data)
{
mat.get(u, v) = std::min(mat.get(u, v), data);
}
template<typename E>
void MatGraph<E>::add_biedge(int u, int v, const E& data)
{
add_edge(u, v, data);
add_edge(v, u, data);
}
template<typename E>
int MatGraph<E>::first_neigh(int u) const
{
for (int j = 0; j < size; j++) {
if (mat.get(u, j) < MAX)
return j;
}
return -1;
}
template<typename E>
int MatGraph<E>::next_neigh(int u, int v) const
{
if (v == -1)
return -1;
for (int j = v + 1; j < size; j++) {
if (mat.get(u, j) < MAX)
return j;
}
return -1;
}
using G = MatGraph<int16_t>;
inline int get_min(const bool* si, const int* dist, const int n) {
int mi = -1, ms = G::MAX;
for (int i = 0; i < n; i++) {
if (!si[i]) {
if (dist[i] < ms) {
ms = dist[i];
mi = i;
}
}
}
return mi;
}
/**
* 基于邻接矩阵的Dijkstra算法的实现
*/
int sssp(const G& graph)
{
int n = graph.get_size();
bool* si = new bool[n];
int* dist = new int[n];
std::fill(si, si + n, false);
std::fill(dist, dist + n, G::MAX);
dist[0] = 0;
for (int cnt = 0; cnt < n; cnt++) {
int mi = get_min(si, dist, n);
if (mi == -1 || dist[mi] == G::MAX) {
//没有可达的了,再见
break;
}
si[mi] = true;
int mj = graph.first_neigh(mi);
for (; mj!=-1; mj=graph.next_neigh(mi,mj)) {
dist[mj] = std::min(dist[mj], dist[mi] + graph.get_edata(mi,mj));
}
}
if (dist[n - 1] == G::MAX)
return -1;
else return dist[n - 1];
}
#include <iostream>
#include <utility>
/**
* 二维数组的简单封装,使用连续空间,运算下标
*/
template <typename T>
class Matrix {
int row, col;
......@@ -16,6 +10,12 @@ public:
inline T* get_pointer_serial(int n) {
return data + n;
}
inline T& get_serial(int n) {
return *(data + n);
}
inline int get_size()const {
return row * col;
}
inline const T& get(int i, int j)const {
return const_cast<Matrix*>(this)->get(i, j);
}
......@@ -25,6 +25,10 @@ public:
inline int get_col()const {
return col;
}
inline int index_to_serial(int i, int j)const {
return i * col + j;
}
void index_to_rowcol(int n, int& i, int& j)const;
void fill(const T& d);
Matrix(const Matrix&) = delete;
......@@ -36,7 +40,7 @@ public:
};
template<typename T>
Matrix<T>::Matrix(int row_, int col_):row(row_),col(col_)
Matrix<T>::Matrix(int row_, int col_) :row(row_), col(col_)
{
data = new T[row * col];
}
......@@ -60,6 +64,13 @@ T* Matrix<T>::get_pointer(int i, int j)
return data + i * col + j;
}
template<typename T>
void Matrix<T>::index_to_rowcol(int n, int& i, int& j) const
{
j = n % col;
i = n / col; //int div
}
template<typename T>
void Matrix<T>::fill(const T& d)
{
......@@ -78,4 +89,28 @@ void Matrix<T>::show() const
std::cout << std::endl;
}
std::cout << ']' << std::endl;
}
//以下:四邻域判定
using MI = Matrix<int>;
//using MD = Matrix<std::array<int, 4>>;
using namespace std;
//方向指标:0,1,2,3 上下左右
inline void get_dir(int dir, int& dx, int& dy) {
switch (dir) {
case 0:dx = 0; dy = 1; return;
case 1:dx = 0, dy = -1; return;
case 2:dx = -1; dy = 0; return;
case 3:dx = 1; dy = 0; return;
default:return;
}
}
inline bool valid(const MI& mat, int si, int sj) {
return 0 <= si && si < mat.get_row() && 0 <= sj && sj < mat.get_col();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment