HDOJ 1181 deformation course DFS

Source: Internet
Author: User

AC 781 MS 476 K, the idea is a very simple search question, deep search is very simple, the meaning of the question is a bit unclear, that is, words must be connected at the beginning and end

In addition, you must note that if the word contains multiple words starting with B, you must search for them in sequence. Of course, if you have found the correct answer, you can break it and put it in a queue, simulate dfs.

AC code:


[Cpp]
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Bool visit [100010]; // access tag
Vector <string> buf; // stores all words
Queue <string> start; // used to store words starting with B
Int flag;
Void dfs (string s ){
If (s [s. size ()-1] = 'M '){
Flag = 1;
Return;
}
For (int I = 0; I! = Buf. size (); I ++)
{
If (visit [I]) continue;
If (flag) return; // only one group of satisfied answers is required.

String tmp1 = buf [I];
If (s [s. size ()-1] = tmp1 [0]) {
Visit [I] = 1;
Dfs (tmp1 );
Visit [I] = 0;
}
}
}
Int main ()
{
 
String tmp, s;
While (cin> tmp ){
If (tmp [0] = 'B') start. push (tmp );
Buf. push_back (tmp );
While (cin> tmp & tmp! = "0 "){
If (tmp [0] = 'B') start. push (tmp );
Buf. push_back (tmp );}
Int len = buf. size ();
Memset (visit, 0, sizeof (visit ));
Flag = 0;
While (! Start. empty () {// multiple searches
S = start. front ();
Start. pop ();
For (int I = 0; I! = Buf. size (); I ++ ){
If (s = buf [I]) {
Visit [I] = 1;
Break;
}
}
Dfs (s );
If (flag ){
Break;
}
}
If (flag) cout <"Yes." <endl;
Else cout <"No." <endl;
Buf. clear ();

}
Return 0;
 
}
 

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.