leetcode_127_Word Ladder_BFS

本文介绍了一种使用广度优先搜索(BFS)解决词梯问题的方法,旨在找到两个单词间最短转换序列的长度。通过具体示例展示了如何在给定的单词列表中逐步变换一个单词直到达到目标单词的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

Only one letter can be changed at a time
Each intermediate word must exist in the word list
For example,

Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”]
As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
return its length 5.

Note:

Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.


这道题就是我们所谓的词梯,每次只变换一个字母,从一个单词变换到另一个单词,求出梯数,即变换步数+1

这道题还是挺简单的,用DFS,或者BFS可以做,但是DFS会超时,原因是DFS的回溯浪费了时间,而BFS避免了这种情况,因为题目只需要求步数,也就是我们一般在求一个二叉树的层数的做法,求二叉树时候我们会有一个标志变量current_num来表示当前层的结点数,同样的,由于这道题中我们不能很直接的知道每一层的终点,所以就加个变量next_num来表示下一层的结点数,从而顺利的广度搜索

另外这道题还让我增加了对头文件unordered_set的了解,有兴趣的也可以了解一下unordered_set

#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
#include <unordered_set> 
using namespace std;

int ladderLength(string start, string end, unordered_set<string>& dict) {
    if (start.size() != end.size())
        return 0;
    if (start == end)
        return 2;

    unordered_set<string> answer;
    answer.insert(start);

    int current_num = 1;
    int next_num = 0;

    queue<string> que;
    que.push(start);

    int length = 0;

    while (current_num > 0)
    {
        string temp_string = que.front();
        que.pop();
        current_num--;
        for (int i = 0; i< temp_string.size(); i++)
        {
            string temp = temp_string;
            for (char ch = 'a'; ch <= 'z'; ch++)
            {
                temp[i] = ch;
                if (dict.find(temp) != dict.end() && answer.find(temp) == answer.end())
                {
                    if (temp == end)
                    {
                        return length + 2;
                    }
                    else
                    {
                        answer.insert(temp);
                        que.push(temp);
                        next_num++;
                    }
                }
            }
        }
        if (current_num == 0)
        {
            current_num = next_num;
            next_num = 0;
            length++;
        }
    }
    return 0;

}

int main()
{
    string start = "a";
    string end = "c";
    unordered_set<string> dict;
    dict.insert("a");
    dict.insert("b");
    dict.insert("c");
    cout << ladderLength(start, end, dict) << endl;
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值