天津网站建设方案维护,wordpress运行c语言,开通域名后怎样建设网站,seo长沙目录 迷宫中离入口最近的出口
最小基因变化
单词接龙
为高尔夫比赛砍树 迷宫中离入口最近的出口
题目 思路
使用宽度优先遍历解决这道题#xff0c;需要一个二维数组标记是否被遍历过#xff0c;也需要一个队列辅助完成宽度优先遍历#xff0c;类似于水波纹一样#x…
目录 迷宫中离入口最近的出口
最小基因变化
单词接龙
为高尔夫比赛砍树 迷宫中离入口最近的出口
题目 思路
使用宽度优先遍历解决这道题需要一个二维数组标记是否被遍历过也需要一个队列辅助完成宽度优先遍历类似于水波纹一样一层一层的往外扩如果扩到了矩阵边缘行则返回步数就是离入口最近的距离。
代码
class Solution {int dx[4]{0,0,1,-1};int dy[4]{1,-1,0,0};public:int nearestExit(vectorvectorchar maze, vectorint entrance) {int mmaze.size(),nmaze[0].size();vectorvectorbool vis(m,vectorbool(n,false));queuepairint,int q;int step0;q.push({entrance[0],entrance[1]});vis[entrance[0]][entrance[1]]true;while(!q.empty()){int szq.size();step;for(int i0;isz;i){int aq.front().first;int bq.front().second;q.pop();for(int k0;k4;k){int xadx[k],ybdy[k];if(x0 xm y0 yn !vis[x][y] maze[x][y].){if(x0 || xm-1 || y0 || yn-1)return step;q.push({x,y});vis[x][y]true;}}}}return -1;}
};
最小基因变化
题目 思路
其实仔细分析这道题后可以发现也可以用宽度优先遍历来解决这道题每一次某位置变化一个字母就相当于以某位置为起点往外扩了一层为了便于快速地判断变换后的基因序列是否在基因库中将基因库中的基因序列存储到哈希表中同时也需要一个哈希表来标记某些基因序列是否已经变换过另外也需要一个队列来完成宽度优先遍历的辅助操作。
需要注意的是在对基因序列的某位置变换前需保存一下变换前的基因序列便于变换后再进行恢复还原。
代码
class Solution {
public:int minMutation(string startGene, string endGene, vectorstring bank) {unordered_setstring vis;unordered_setstring hash(bank.begin(),bank.end());string sACGT;if(startGeneendGene) return 0;if(!hash.count(endGene)) return -1;queuestring q;q.push(startGene);vis.insert(startGene);int ret0;while(!q.empty()){int szq.size();ret;for(int k0;ksz;k){string topq.front();q.pop();for(int i0;i8;i){string strtop;for(int j0;j4;j){top[i]s[j];if(topendGene) return ret;if(!vis.count(top) hash.count(top))q.push(top),vis.insert(top);}topstr;}}}return -1;}
};
单词接龙
题目 思路
其实这道题和前一道题是类似的只不过这道题的单词变化范围不再是A,C,G,T而是26个英文小写字母为了快速判断单词是否是字典中的单词首先将字典中的单词存放到哈希表中同时也需要一个哈希表标记已经访问过的单词和一个队列来完成宽度优先遍历的辅助操作。
这道题同上一道题一样进行单词的某位置前需要先保存原始单词以便变换完单词某位置后进行恢复和还原。
代码
class Solution {
public:int ladderLength(string beginWord, string endWord, vectorstring wordList) {unordered_setstring vis;unordered_setstring hash(wordList.begin(),wordList.end());if(!hash.count(endWord)) return 0;queuestring q;q.push(beginWord);vis.insert(beginWord);int ret1;while(!q.empty()){int szq.size();ret;for(int i0;isz;i){string topq.front();q.pop();for(int j0;jbeginWord.size();j){string strtop;for(char cha;chz;ch){top[j]ch;if(topendWord) return ret;if(!vis.count(top) hash.count(top))q.push(top),vis.insert(top);}topstr;}}}return 0;}
};
为高尔夫比赛砍树
题目 思路
解决这道题也是使用宽度优先遍历来解决但是题目要求按高度从低到高砍完所有的树因此需要对砍树的顺序进行排序然后求出被砍顺序挨着的两个树的最短距离分别求出这样的被砍顺序挨着的两个树的最短距离然后加起来就是按高度从低到高砍完所有树的最短步数如果某一步不能正常进行说明无法完成砍树直接返回-1.
代码
class Solution {int m,n;int dx[4]{1,-1,0,0};int dy[4]{0,0,1,-1};public:int cutOffTree(vectorvectorint forest) {mforest.size(),nforest[0].size();vectorpairint,int order;for(int i0;im;i)for(int j0;jn;j)if(forest[i][j]1)order.push_back({i,j});sort(order.begin(),order.end(),[](const pairint,int p1,const pairint,int p2){return forest[p1.first][p1.second]forest[p2.first][p2.second];});int bx0,by0;int ret0;for(auto [a,b]:order){int stepbfs(forest,bx,by,a,b);if(step-1) return -1;retstep;bxa,byb;}return ret;}int bfs(vectorvectorint forest,int bx,int by,int ex,int ey){if(bxex byey) return 0;queuepairint,int q;bool vis[51][51];memset(vis,false,sizeof vis);q.push({bx,by});vis[bx][by]true;int step0;while(!q.empty()){int szq.size();step;for(int i0;isz;i){auto [a,b]q.front();q.pop();for(int j0;j4;j){int xadx[j],ybdy[j];if(xex yey) return step;if(x0 xm y0 yn forest[x][y] !vis[x][y])q.push({x,y}),vis[x][y]true;}}}return -1;}
};
文章转载自: http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.gkgr.cn.gov.cn.gkgr.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.mkkcr.cn.gov.cn.mkkcr.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.qpqb.cn.gov.cn.qpqb.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.zxznh.cn.gov.cn.zxznh.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn