第一次用java写深搜, 菜鸟级别, 求大神指点
http://acm.hdu.edu.cn/showproblem.php?pid=1181
import java.util.Scanner;
public class Main {
private static String[] sa = new String[10000];
private static int length; //输入的字符串的个数
private static boolean[] visited; //标志是否被访问
private static boolean flag1;
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String s = scanner.next();
//读取字符串
while(!s.equals("0")){
sa[length++] = s;
s = scanner.next();
}
visited = new boolean[length];
for(int i = 0; i < length; i++){
if(sa[i].charAt(0)=='b'){ //找到第一个字符为b的字符串,然后对其进行深度搜索
bfs(i);
if(flag1==true){
break;
}
//如果第一次深搜找不到,在进行下一次时要对visited重置
for(int j = 0; j < length; j++){
visited[j] = false;
}
}
}
if(flag1==true){
System.out.println("Yes.");
}else
System.out.println("No.");
//重置
flag1 = false;
length = 0;
}
}
public static void bfs(int i){
visited[i]=true; //标志访问
char b = sa[i].charAt(sa[i].length()-1);
if(b == 'm'){
flag1= true;
}
for(int j = 0; j < length; j++){
if(sa[j].charAt(0)==b&&visited[j]==false){ //如果找到的字符串的第一个字符和当前字符串最后一个字符相同并且未被访问,则继续
bfs(j);
}
}
}
}