Deformation lesson
Problem Description uh ... In the Metamorphosis class Harry ran into a little trouble, because he was not as Hermione as he could remember all the spells and randomly turned a baseball into a hedgehog or something, but he found a uniform rule of Transfiguration spells: If the spell is a word ending with a B, Then it works just to make a object into a B object.
Harry has already made a list of all the spells he has, and he wants you to help calculate whether he can finish the teacher's homework and turn a B (ball) into a M (Mouse), you know, if he can't do it himself, he has to ask Hermione. and was forced to listen to a lot of good learning.
There are multiple sets of input test data. Each group has multiple lines, one word per line, and only lowercase letters, all of which Harry spells. The number 0 indicates the end of a set of inputs.
Output "Yes" If Harry can finish his homework, otherwise output "No." (Do not ignore the period)
Sample Input
Sosoonrivergoesthemgotmoonbeginbig0
Sample Output
Yes. Hint HintHarry can read this mantra: "Big-got-them".
The
first feeling, like the Solitaire, and then think, each statement is not a path, see through these paths can be from the Node B, go to M's borrowing point. Into this problem, it is simple, you can use Deep Search (DFS), can also use wide search (BFS) and so on, and I used the method is DFS, the code is as follows:
#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int f[30][30],f2[30]; BOOL Flag;void DFS (int x) { if (x==12) { flag=true; return; } else{ f2[x]=1; for (int i=0;i<26;i++) { if (F[x][i]&&!f2[i]) { f2[i]=1; DFS (i);}}} int main () { char s[100]; while (scanf ("%s", s)!=eof&&s[0]!= ' 0 ') { memset (f,0,sizeof (f)); memset (f2,0,sizeof (F2)); int a=s[0]-' a '; int B=s[strlen (s) -1]-' a '; F[a][b]=1; while (scanf ("%s", s)!=eof&&s[0]!= ' 0 ') { int a=s[0]-' a '; int B=s[strlen (s) -1]-' a '; f[a][b]=1; } Flag=false; DFS (1); if (flag) printf ("yes.\n"); else printf ("no.\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1181. Transformation Course "DFS (BFs also available)" "Problem Conversion" "August 5"