[127] C language LeetCode brush. Solitaire word (M)

Given two words (beginWord and endWord) and a dictionary, to find the length of the conversion sequence from the shortest endWord the beginWord. Conversion must follow the following rules:


    Each conversion can only change one letter.
    Middle of a word conversion process must be a dictionary word.


Description:


    If such a conversion sequence is not present, it returns 0.
    All words have the same length.
    All words only consist of lowercase letters.
    Repeat the word does not exist in the dictionary.
    You can assume beginWord endWord and non-empty, and the two are not the same.


Example 1:

输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: a shortest conversion sequence is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
     returns its length 5.


Example 2:

输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explained: endWord "cog" not in the dictionary, it can not be converted.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/word-ladder
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.


 

This question is really hard to do in C language.

I have been using deep search before, but this question must be used to search wide. Need to construct wide search queue, C language library function itself is not related to the queue. Some people use chain simulation queue, but waste time code and ah, do the questions we still have to build wheels, and made my heart devastated, uneconomical. Therefore, considering the number of groups stack simulation, to simulate an array queue.

Then prepare a queue array, a tail, a head. Then enough found a plain array, configured to obtain an array of structures. A deposit of wordList idx, a store conversions count.

If the difference between the current word and find it lined up to join a character, and then taken out one by one comparison with beginWord words, the first to meet the requirements of a minimum number of inevitable conversion count.

This question is worth read it several times.

 

 

int check(char *onew, char *twow, int len)
{
    int i;
    int diff = 0;
    
    for (i = 0; i < len; i++) {
        if(onew[i] != twow[i]) {
            if (diff == 0) {
                diff = 1;
            } else {
                return false;
            }
        }
    }
    
    return diff;
}

struct QUE {
    int idx;
    int count;
};

struct QUE que[8096];
int tail;
int head;

int ladderLength(char * beginWord, char * endWord, char ** wordList, int wordListSize){
    int i;
    int len = strlen(wordList[0]);
    int endidx = 0xfffff;
    int flag[wordListSize];
    int count = 1;
    int idx;

    memset(flag, 0, sizeof(int) * wordListSize);
    memset(que, 0, sizeof(int) * 8096);
    tail = 0;
    head = 0;

    for (i = 0; i < wordListSize; i++) {
        if (!strcmp(endWord, wordList[i])) {
            endidx = i;
            flag[i] = 1;
        }
    }
    
    if (endidx == 0xfffff) {
        return false;
    }
    
    que[tail].idx = endidx;
    que[tail++].count = 1;
    
    while (tail > head) {
        idx = que[head].idx;
        count = que[head++].count;
        
        if (check(beginWord, wordList[idx], len)) {
            return count + 1;
        }
        
        for(i = 0; i < wordListSize; i++) {
            if(!flag[i] && check(wordList[idx], wordList[i], len)){
                que[tail].idx = i;
                que[tail++].count = count + 1;
                flag[i] = 1;
            }
        }
    }

    return 0;
}

 

Published 110 original articles · won praise 17 · views 110 000 +

Guess you like

Origin blog.csdn.net/jin615567975/article/details/104330391