class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool used[101][101] = {};
int m = maze.size(), n = maze[0].size(), step = 0;
queue<pair<int, int>> q;
int row = entrance[0], col = entrance[1];
q.push({row, col});
used[row][col] = true;
while (q.size())
{
step++; // 向外扩展一层
int sz = q.size();
while (sz--)
{
auto [a, b] = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int x = a + dx[i], y = b + dy[i];
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.'
&& !used[x][y])
{
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return step;
used[x][y] = true;
q.push({x, y});
}
}
}
}
return -1;
}
};
class Solution {
public:
int minMutation(string startGene, string endGene, vector<string>& bank) {
unordered_set<string> hash(bank.begin(), bank.end());
unordered_set<string> used;
const string str = "ACGT";
if (startGene == endGene) return 0;
if (!hash.count(endGene)) return -1;
queue<string> q;
q.push(startGene);
used.insert(startGene);
int step = 0;
while (q.size())
{
step++;
int sz = q.size();
while (sz--)
{
string s = q.front();
q.pop();
for (int i = 0; i < 8; i++)
{
string tmp = s;
for (int j = 0; j < 4; j++)
{
tmp[i] = str[j];
if (tmp == endGene) return step;
if (hash.count(tmp) && !used.count(tmp))
{
used.insert(tmp);
q.push(tmp);
}
}
}
}
}
return -1;
}
};
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> hash(wordList.begin(), wordList.end());
unordered_set<string> used;
queue<string> q;
if (!hash.count(endWord)) return 0;
q.push(beginWord);
used.insert(beginWord);
int ret = 1; // 最初beginWord也算一个单词
while (q.size())
{
ret++;
int sz = q.size();
while (sz--)
{
string str = q.front();
q.pop();
for (int i = 0; i < beginWord.size(); i++)
{
string tmp(str);
for (char ch = 'a'; ch <= 'z'; ch++)
{
tmp[i] = ch;
if (tmp == endWord) return ret;
if (hash.count(tmp) && !used.count(tmp))
{
used.insert(tmp);
q.push(tmp);
}
}
}
}
}
return 0;
}
};
class Solution {
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int m, n, ret = 0;
public:
int cutOffTree(vector<vector<int>>& forest) {
m = forest.size(), n = forest[0].size();
map<int, pair<int, int>> hash;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (forest[i][j] > 1)
hash[forest[i][j]] = {i, j};
int bx = 0, by = 0;
for (auto &[a, b] : hash)
{
int step = bfs(forest, bx, by, b.first, b.second);
if (step == -1) return -1;
ret += step;
bx = b.first;
by = b.second;
}
return ret;
}
int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey)
{
if (bx == ex && by == ey) return 0;
int ret = 0;
queue<pair<int, int>> q;
bool used[51][51] = {};
q.push({bx, by});
used[bx][by] = true;
while (q.size())
{
ret++;
int sz = q.size();
while (sz--)
{
auto [a, b] = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int x = a + dx[i], y = b + dy[i];
if (x == ex && y == ey) return ret;
if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] && !used[x][y])
{
used[x][y] = true;
q.push({x, y});
}
}
}
}
return -1;
}
};
class Solution {
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int m, n, ret = 0;
public:
int cutOffTree(vector<vector<int>>& f) {
m = f.size(), n = f[0].size();
vector<pair<int, int>> trees;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (f[i][j] > 1)
trees.push_back({i, j});
sort(trees.begin(), trees.end(),
[&f](const pair<int, int>& p1, const pair<int, int>& p2)
{ return f[p1.first][p1.second] < f[p2.first][p2.second];});
int bx = 0, by = 0;
for (auto [a, b] : trees)
{
int step = bfs(f, bx, by, a, b);
if (step == -1) return -1;
ret += step;
bx = a, by = b;
}
return ret;
}
int bfs(vector<vector<int>>& f, int bx, int by, int ex, int ey)
{
// 处理边界情况
if (bx == ex && by == ey) return 0;
queue<pair<int, int>> q;
bool used[51][51] = {};
q.push({bx, by});
used[bx][by] = true;
int ret = 0;
while (q.size())
{
ret++;
int sz = q.size();
while (sz--)
{
auto [a, b] = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int x = a + dx[i], y = b + dy[i];
if (x == ex && y == ey) return ret;
if (x < 0 || x >= m || y < 0 || y >= n || !f[x][y] || used[x][y])
continue;
used[x][y] = true;
q.push({x, y});
}
}
}
return -1;
}
};
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有