题意:给定起点和终点的n为坐标及一系列n为坐标,问是否能从起点走到终点;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException{
StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in));
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
int n , t = 1 ;
while((n = in.nextInt()) != 0){
new Task().solve(t++ , n , in, out) ; // out.flush() ;
}
out.flush() ;
}
}
class Task{
int n ;
HashMap<Integer, ArrayList<Integer>> adj = new HashMap<Integer, ArrayList<Integer>>() ;
HashSet<Integer> hash = new HashSet<Integer>() ;
int read(InputReader in){
int sum = 0 , k ;
for(int i = 0 ; i < n ; i++){
k = in.nextInt() ;
if(k == -1) return -1 ;
sum = sum * 10 + k ;
}
return sum ;
}
int S , T ;
boolean dfs(int now){
if(now == T) return true ;
hash.add(now) ;
ArrayList<Integer> g = adj.get(now) ;
if(g == null) return false ;
for(int v : g){
if(! hash.contains(v)){
if(dfs(v)) return true ;
}
}
return false ;
}
public void solve(int testcas , int n , InputReader in , PrintWriter out) throws IOException{
this.n = n ;
S = read(in) ; T = read(in) ;
int s , t ;
while((s = read(in)) != -1){
t = read(in) ;
if(adj.get(s) == null) adj.put(s, new ArrayList<Integer>()) ;
adj.get(s).add(t) ;
if(adj.get(t) == null) adj.put(t, new ArrayList<Integer>()) ;
adj.get(t).add(s) ;
}
if(dfs(S)) out.println("Maze #" + testcas + " can be travelled") ;
else out.println("Maze #" + testcas + " cannot be travelled") ;
}
}
class InputReader{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream), 32768) ;
tokenizer = null ;
}
public String next(){
while(tokenizer == null || ! tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
}catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}