class Solution {
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
typedef pair<int, int> PII;
bool vis[105][105];
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
queue<PII> q;
q.push({entrance[0], entrance[1]});
vis[entrance[0]][entrance[1]] = 1;
int cnt = 0;
while (q.size()) {
cnt++;
int sz=q.size();
for (int i = 0; i < sz; i++) {
auto [a, b] = q.front();
q.pop();
for (int j = 0; j < 4; j++) {
int x = a + dx[j], y = b + dy[j];
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] &&
maze[x][y] == '.') {
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
return cnt;
}
q.push({x, y});
vis[x][y] = 1;
}
}
}
}
return -1;
}
};
class Solution {
public:
int minMutation(string startGene, string endGene, vector<string>& bank) {
unordered_set<string> vis;
unordered_set<string> hash(bank.begin(),bank.end());
string change="ACGT";
queue<string> q;
q.push(startGene);
vis.insert(startGene);
if(startGene==endGene) return 0;
if(!hash.count(endGene)) return -1;
int ans=0;
while(q.size())
{
ans++;
int sz=q.size();
while(sz--)
{
string t=q.front();
q.pop();
for(int i=0;i<8;i++)
{
string tmp=t;
for(int j=0;j<4;j++)
{
tmp[i]=change[j];
if(hash.count(tmp)&&!vis.count(tmp))
{
if(tmp==endGene) return ans;
q.push(tmp);
vis.insert(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> vis;
if(!hash.count(endWord)) return 0;
queue<string> q;
q.push(beginWord);
vis.insert(beginWord);
int ans=1;
while(q.size())
{
ans++;
int sz=q.size();
while(sz--)
{
string t=q.front();
q.pop();
for(int i=0;i<t.size();i++)
{
string tmp=t;
for(char ch='a';ch<='z';ch++)
{
tmp[i]=ch;
if(hash.count(tmp)&&!vis.count(tmp))
{
if(tmp==endWord) return ans;
q.push(tmp);
vis.insert(tmp);
}
}
}
}
}
return 0;
}
};
class Solution {
typedef pair<int, int> PII;
int m, n;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
bool vis[55][55];
public:
int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey) {
if (bx == ex && by == ey)
return 0;
queue<PII> q;
q.push({bx, by});
vis[bx][by] = 1;
memset(vis, 0, sizeof(vis));
int step = 0;
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 && !vis[x][y] &&
forest[x][y] != 0) {
if (x == ex && y == ey)
return step;
q.push({x, y});
vis[x][y] = 1;
}
}
}
}
return -1;
}
int cutOffTree(vector<vector<int>>& forest)
{
m = forest.size(), n = forest[0].size();
vector<PII> tree;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (forest[i][j] > 1)
tree.push_back({i, j});
sort(tree.begin(), tree.end(), [&](const PII& p1, const PII& p2) {
return forest[p1.first][p1.second] < forest[p2.first][p2.second];
});
int bx = 0, by = 0, ret = 0;
for (auto& [a, b] : tree)
{
int step = bfs(forest, bx, by, a, b);
if (step == -1)
return -1;
ret += step;
bx = a, by = b;
}
return ret;
}
};