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 Inputsosoonrivergoesthemgotmoonbeginbig0
Sample Outputyes.
HintHintharry can read this mantra: "Big-got-them". This problem gives me a lot more of the flexibility of global variables use global variables for values that have an effect on the main function in the calling function and then the understanding of backtracking requires that the searched node also needs to be searched and that the topic does not need to be written to the DFS call function (the alignment is fixed) The first thing to be clear is the condition of the recursive termination (including the possibility of finding and not finding possible) in the continue and return to make a good distinction ...
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace Std;
Char a[200][190];
int visit[200];
int flag=0;
void Dfs (int start,int Leng)
{
int I,newstart;
if (flag==1) return;
for (i=1;i<=leng;i++)
{
if (visit[i]==1) continue;///here wrong several times!!! Not return!!!!.
if (A[start][1]==a[i][0])
{
newstart=i;
Visit[newstart]=1;
if (a[newstart][1]== ' m ')
{
flag=1;
Return
}
else Dfs (Newstart,leng);
}
}
}
int main ()
{
int t=1,len,i,j;
memset (visit,0,sizeof (visit));
while (scanf ("%s", &a[t++])!=eof)
{
Len=strlen (A[t-1]);
A[T-1][1]=A[T-1][LEN-1];
if (a[t-1][0]== ' 0 ')
{
flag=0;
memset (visit,0,sizeof (visit));
for (i=1;i<t;i++)
{
if (a[i][0]== ' B ')
{
if (flag==1) break;
Visit[i]=1;
DFS (I,T-1);
}
}
if (flag==1) printf ("yes.\n");
else printf ("no.\n");
T=1;
}
}
return 0;
}
Hangzhou Electric 1081 Second DFS problem