All checks were successful
Build and Deploy Quartz / build-and-deploy (push) Has been skipped
192 lines
4.0 KiB
C++
192 lines
4.0 KiB
C++
// https://leetcode.com/problems/last-day-where-you-can-still-cross
|
|
|
|
#include <iostream>
|
|
#include <format>
|
|
#include <vector>
|
|
|
|
using std::cout, std::endl;
|
|
using std::format;
|
|
using std::vector;
|
|
|
|
const bool debug = true;
|
|
|
|
class UnionFind2D
|
|
{
|
|
private: // private class
|
|
class UnionFindNode
|
|
{
|
|
public:
|
|
// record the rank of this node
|
|
int rank = 0;
|
|
|
|
// row, col work together as the parent pointer of this node
|
|
short row = 0;
|
|
short col = 0;
|
|
|
|
// special metadata for this node
|
|
bool valid = false;
|
|
bool top = false;
|
|
bool bottom = false;
|
|
|
|
bool isRoot()
|
|
{
|
|
return valid && (col == 0);
|
|
}
|
|
|
|
// merge this node to another node
|
|
// need to provide parent and its position
|
|
// will skip if parent is not valid
|
|
void mergeTo(UnionFindNode &parent, short r, short c)
|
|
{
|
|
// validate
|
|
if (!isRoot() || !parent.isRoot())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// update pointer, pointing to parent
|
|
row = r;
|
|
col = c;
|
|
|
|
// update parent rank
|
|
parent.rank += rank;
|
|
|
|
// update parent metadata
|
|
if (top)
|
|
{
|
|
parent.top = 1;
|
|
}
|
|
if (bottom)
|
|
{
|
|
parent.bottom = 1;
|
|
}
|
|
}
|
|
};
|
|
|
|
private: // private variables
|
|
// mCount * mCount represents the total size
|
|
int mCount = 0;
|
|
UnionFindNode mNodes[20010][20010];
|
|
|
|
private: // private methods
|
|
/**
|
|
* Get root node of a position
|
|
*
|
|
* Return invalid node if original node invalid,
|
|
* else, return root node
|
|
*/
|
|
UnionFindNode &getRoot(short r1, short c1)
|
|
{
|
|
UnionFindNode &curNode = mNodes[r1][c1];
|
|
|
|
if (!curNode.valid)
|
|
{
|
|
return curNode;
|
|
}
|
|
|
|
while (!curNode.isRoot())
|
|
{
|
|
curNode = mNodes[curNode.row][curNode.col];
|
|
}
|
|
|
|
return curNode;
|
|
}
|
|
|
|
// if an idx is valid as a position pointer
|
|
bool validRange(short idx)
|
|
{
|
|
if (idx < 1 || idx > mCount)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Connect r1, c1 to r2, c2
|
|
* Ignore if one of those are not exists
|
|
*/
|
|
void connect(short r1, short c1, short r2, short c2)
|
|
{
|
|
// validate range
|
|
bool allValid = validRange(r1) && validRange(c1) && validRange(r2) && validRange(c2);
|
|
|
|
if (!allValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// get nodes
|
|
UnionFindNode &n1 = getRoot(r1, c1);
|
|
UnionFindNode &n2 = getRoot(r2, c2);
|
|
|
|
// merge nodes
|
|
if (n1.rank > n2.rank)
|
|
{
|
|
n2.mergeTo(n1, r1, c1);
|
|
}
|
|
else
|
|
{
|
|
n1.mergeTo(n2, r2, c2);
|
|
}
|
|
}
|
|
|
|
public: // public method
|
|
UnionFind2D(int count) : mCount(count)
|
|
{
|
|
clear(count);
|
|
}
|
|
|
|
/**
|
|
* Mark a position as land, then connect it to other land block around if exists
|
|
* Note: r, c should be 1-indexed
|
|
*/
|
|
void add(int r, int c)
|
|
{
|
|
UnionFindNode &curNode = mNodes[r][c];
|
|
|
|
// create new node and init data if not added before (invalid)
|
|
if (!curNode.valid)
|
|
{
|
|
curNode.valid = 1;
|
|
curNode.rank = 1;
|
|
|
|
if (r == 1)
|
|
{
|
|
curNode.top = 1;
|
|
}
|
|
if (r == mCount)
|
|
{
|
|
curNode.bottom = 1;
|
|
}
|
|
}
|
|
|
|
// connect to nodes around
|
|
connect(r, c, r, c + 1);
|
|
connect(r, c, r, c - 1);
|
|
connect(r, c, r + 1, c);
|
|
connect(r, c, r - 1, c);
|
|
}
|
|
|
|
void clear(int mCount)
|
|
{
|
|
mCount = mCount;
|
|
|
|
// note: better init method?
|
|
for (int i = 0; i < mCount; ++i)
|
|
{
|
|
for (int j = 0; j < mCount; ++j)
|
|
{
|
|
mNodes[i][j] = UnionFindNode();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
class Solution
|
|
{
|
|
public:
|
|
int latestDayToCross(int row, int col, vector<vector<int>> &cells)
|
|
{
|
|
}
|
|
}; |