Deformation course for hdu1181 BFS search

Source: Internet
Author: User
Original question address

The data for this question is said to be relatively watery. Except for the first group of data which is yes, the rest are no. Many people have seized this point and it is too watery. Of course, I don't think that would make sense. The quality is not the quantity. This question is a good search question. There are many solutions, including DFS, BFs, query set, and Dijkstra algorithm.

Question Analysis:

The question contains many strings, but only the first and last characters of the string are concerned. It seems that we want to create a "ing" between characters. Otherwise, it can be converted into a directed graph of 26*26. There are up to 26 nodes, and the original ing between characters is converted into a path between nodes. The problem is whether a path can be found from Node B to node M.

I used BFs to solve the problem.

# Include <iostream> # include <cstring> using namespace STD; bool G [26] [26]; // The value is initialized to 0 (false) when the global variable is declared) bool used [26]; // when the global variable is declared, it is initialized to 0 (false) bool BFS (int I) {used [1] = true; for (Int J = 0; j <26; j ++) {if (I! = J &&! Used [J] & G [I] [J]) {if (I! = 1) g [1] [J] = true; used [J] = true; BFS (j) ;}} return G [1] [12]; // B is 1, m is 12} int main () {char s [100]; while (CIN> S) {If (s [0]! = '0') {int Len = strlen (s); G [s [0]-'a'] [s [len-1]-'a'] = true; continue;} If (BFS (1) cout <"yes. "<Endl; else cout <" no. "<Endl; memset (G, false, sizeof (g); memset (used, false, sizeof (used ));}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.