add: 220212
This commit is contained in:
parent
ea7f5ea260
commit
51a6234dc9
|
|
@ -0,0 +1,108 @@
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
#include <random>
|
||||||
|
#include <ctime>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1020. Number of Enclaves
|
||||||
|
* You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.
|
||||||
|
* A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.
|
||||||
|
* Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
inline static const int dX[] = {0, 1, 0, -1};
|
||||||
|
inline static const int dY[] = {1, 0, -1, 0};
|
||||||
|
inline static constexpr bool isValid(int x, int y, int m, int n) {
|
||||||
|
return (x >= 0) && (y >= 0) && (x < m) && (y < n);
|
||||||
|
}
|
||||||
|
inline static constexpr bool isEdge(int x, int y, int m, int n) {
|
||||||
|
return (!x) || (!y) || (x == m - 1) || (y == n - 1);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
static int numEnclaves(std::vector<std::vector<int>>& grid) {
|
||||||
|
int m = grid.size(), n = grid.back().size();
|
||||||
|
int s = m * n * 2 + 100;
|
||||||
|
int* const q = new int[s];
|
||||||
|
bool* const vis = new bool[m * n]{};
|
||||||
|
int f = 0, r = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
const auto& t = grid[i];
|
||||||
|
if (t[0]) {
|
||||||
|
q[r++] = i;
|
||||||
|
q[r++] = 0;
|
||||||
|
vis[i * n] = true;
|
||||||
|
}
|
||||||
|
if (t[n - 1]) {
|
||||||
|
q[r++] = i;
|
||||||
|
q[r++] = n - 1;
|
||||||
|
vis[i * n + (n - 1)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& tf = grid[0];
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (tf[i]) {
|
||||||
|
q[r++] = 0;
|
||||||
|
q[r++] = i;
|
||||||
|
vis[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& tb = grid[m - 1];
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (tb[i]) {
|
||||||
|
q[r++] = m - 1;
|
||||||
|
q[r++] = i;
|
||||||
|
vis[(m - 1) * n + i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (f < r) {
|
||||||
|
int x = q[f++];
|
||||||
|
int y = q[f++];
|
||||||
|
grid[x][y] = 0;
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
int nx = x + dX[i], ny = y + dY[i];
|
||||||
|
if (isValid(nx, ny, m, n) && !vis[nx * n + ny] && grid[nx][ny]) {
|
||||||
|
q[r++] = nx;
|
||||||
|
q[r++] = ny;
|
||||||
|
vis[nx * n + ny] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
ret += grid[i][j];
|
||||||
|
|
||||||
|
delete[] q;
|
||||||
|
delete[] vis;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const int SIZE = 500;
|
||||||
|
std::mt19937 r(std::time(nullptr));
|
||||||
|
std::vector<std::vector<int>> args;
|
||||||
|
args.reserve(SIZE);
|
||||||
|
for (int i = 0; i < SIZE; ++i) {
|
||||||
|
args.emplace_back();
|
||||||
|
auto& t = args.back();
|
||||||
|
t.reserve(SIZE);
|
||||||
|
for (int j = 0; j < SIZE; ++j)
|
||||||
|
t.push_back(1);
|
||||||
|
}
|
||||||
|
const auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
int ans = Solution::numEnclaves(args);
|
||||||
|
const auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
std::cout << ans << std::endl;
|
||||||
|
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms.\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,92 @@
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 127. Word Ladder
|
||||||
|
* A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s[1] -> s[2] -> ... -> s[k] such that:
|
||||||
|
* Every adjacent pair of words differs by a single letter.
|
||||||
|
* Every s[i] for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
|
||||||
|
* s[k] == endWord
|
||||||
|
* Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
// Assume s is same length as t.
|
||||||
|
static inline bool isDist1(const std::string& s, const std::string& t) {
|
||||||
|
int n = s.length();
|
||||||
|
bool c = 0;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (s[i] != t[i]) {
|
||||||
|
if (c)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
c = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
static int ladderLength(const std::string& beginWord, const std::string& endWord, const std::vector<std::string>& wordList) {
|
||||||
|
int reachable = -1, n = wordList.size();
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (wordList[i] == endWord) {
|
||||||
|
reachable = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reachable == -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
std::vector<std::vector<int>> G;
|
||||||
|
G.reserve(wordList.size());
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
G.emplace_back();
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
for (int j = i + 1; j < n; ++j) {
|
||||||
|
if (isDist1(wordList[i], wordList[j])) {
|
||||||
|
G[j].push_back(i);
|
||||||
|
G[i].push_back(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool* const vis = new bool[n]{};
|
||||||
|
std::queue<int> q;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (isDist1(beginWord, wordList[i])) {
|
||||||
|
q.push(i);
|
||||||
|
q.push(2);
|
||||||
|
vis[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!q.empty()) {
|
||||||
|
int idx = q.front();
|
||||||
|
q.pop();
|
||||||
|
int depth = q.front();
|
||||||
|
q.pop();
|
||||||
|
if (reachable == idx) {
|
||||||
|
delete[] vis;
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
for (int i : G[idx]) {
|
||||||
|
if (!vis[i]) {
|
||||||
|
q.push(i);
|
||||||
|
q.push(1 + depth);
|
||||||
|
vis[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete[] vis;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << Solution::ladderLength("hit", "cog", {"hot","dot","dog","lot","log","cog"});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2202 220210.cpp)
|
ADD_EXECUTABLE(2202 220212.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue