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;
}