图论第5天

127.单词接龙

需要cout看一下过程。

cpp 复制代码
#include <iostream>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace ::std;

class Solution
{
public:
    int ladderLength(string beginWord, string endWord, vector<string> &wordList)
    {
        unordered_set<string> wordSet(wordList.begin(), wordList.end()); // 换成unordered_set后查询速度增加
        if (wordSet.find(endWord) == wordSet.end())
            return 0;
        unordered_map<string, int> visitMap; //<word,查询到的路径长度>
        queue<string> que;
        que.push(beginWord);
        visitMap.insert(pair<string, int>(beginWord, 1));
        while (!que.empty())
        {
            string cur_word = que.front();
            que.pop();
            int path = visitMap[cur_word];
            for (int i = 0; i < cur_word.size(); i++)
            {
                string new_word = cur_word;
                for (int j = 0; j < 26; j++)
                {
                    new_word[i] = j + 'a';
                    if (new_word == endWord)
                        return path + 1;
                    if (wordSet.find(new_word) != wordSet.end() && visitMap.find(new_word) == visitMap.end())
                    {
                        visitMap.insert(pair<string, int>(new_word, path + 1));
                        que.push(new_word);
                    }
                }
                cout << cur_word << endl;
                cout << i << endl;
                for (pair<string, int> x : visitMap)
                {
                    cout << "visitMap[" << x.first << "] = " << x.second << "   ";
                }
                cout << endl;
            }
        }
        return 0;
    }
};

int main()
{
    Solution syz;
    string beginWord = "hit", endWord = "cog";
    vector<string> wordList;
    wordList.push_back("hot");
    wordList.push_back("dot");
    wordList.push_back("dog");
    wordList.push_back("lot");
    wordList.push_back("log");
    wordList.push_back("cog");
    syz.ladderLength(beginWord, endWord, wordList);
    return 0;
}

cout运行结果需要看一下,理解一下过程:

所有的word的字母都需要执行一次,所以是0、1、2,

hit 改 h是没有单词加入的,改i会有个hot,改t没有单词加入,所以visitMap里不变。hit这时候已经pop()。

hot改h,增加两个dot\lot,改o\t没有,hot.pop()。

竟然是先dot后lot,跟push顺有关。hot会先push " d " 后 push " l "

dot,d\o都没有,t改完,出现一个dog,g会继续往下走,dot.pop()

lot,l\o都没有,出现log.pop()

这时候que里,front 是dog,然后是 log

dog直接知道cog。visitMap[dog] = 4,4 + 1 = 5

cpp 复制代码
hit
0
visitMap[hit] = 1
hit
1
visitMap[hot] = 2   visitMap[hit] = 1
hit
2
visitMap[hot] = 2   visitMap[hit] = 1
hot
0
visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
hot
1
visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
hot
2
visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
dot
0
visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
dot
1
visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
dot
2
visitMap[dog] = 4   visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
lot
0
visitMap[dog] = 4   visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
lot
1
visitMap[dog] = 4   visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1
lot
2
visitMap[log] = 4   visitMap[dog] = 4   visitMap[dot] = 3   visitMap[lot] = 3   visitMap[hot] = 2   visitMap[hit] = 1

广搜更适合这道题,会把所有可能性在起始点周边的可能性,一点点往外扩,不会造成走冤枉路。

图论看来一道题就要很刺激啊。 T _ T。明天摸鱼看能不能做两道吧。

相关推荐
Joe_Wang52 小时前
[图论]拓扑排序
数据结构·c++·算法·leetcode·图论·拓扑排序
蒙奇D索大4 小时前
【数据结构】图解图论:度、路径、连通性,五大概念一网打尽
数据结构·考研·算法·图论·改行学it
君义_noip7 小时前
信息学奥赛一本通 1524:旅游航道
c++·算法·图论·信息学奥赛
刃神太酷啦1 天前
基础算法篇(3)(蓝桥杯常考点)-图论
数据结构·c++·算法·职场和发展·蓝桥杯·图论·蓝桥杯c++组
羑悻的小杀马特1 天前
【狂热算法篇】探寻图论幽径:Bellman - Ford 算法的浪漫征程(通俗易懂版)
c++·算法·图论·bellman_ford算法
好好学习^按时吃饭2 天前
P4551 最长异或路径
算法·深度优先·图论
triticale2 天前
【图论】最短路径问题总结
java·开发语言·图论
百渡ovO2 天前
【蓝桥杯】每日练习 Day 16,17
c++·算法·图论
JNU freshman2 天前
蓝桥杯 之 图论基础+并查集
蓝桥杯·图论
JLU_LYM3 天前
图论问题集合
图论·有向图