Task3 bos
unknown
java
9 months ago
2.5 kB
5
Indexable
// Read input file and test cases Scanner sc = new Scanner(Main.class.getResourceAsStream("penacony.txt")); int tc = sc.nextInt(); // Loop through all test cases for(int i = 0;i<tc;i++){ // read the start city and the end city int startCity = sc.nextInt() - 1; int endCity = sc.nextInt() - 1; // create the 2D array to represent the graph int[][] city = new int[6][6]; for(int row = 0;row<6;row++){ for(int col = 0;col < 6;col++){ city[row][col] = sc.nextInt(); } } // create a boolean variable to check whether the end city can be visited boolean isPassed = false; // Check if Penacony can be visited boolean[] visited = {false,false,false,false,false,false}; visited[startCity] = true; // Check start - penacony while(!visited[5]){ int start = startCity; for(int col = 0;col<6;col++){ if(city[start][col] == 1 && !visited[col]){ start = col; visited[col] = true; } } if(visited[start]){ break; } } // If penacony can be visited, check if the end city can be visited also // Check penacony - end if(visited[5]){ boolean[] visitedAfterPenacony = {false,false,false,false,false,true}; int start = 5; while(!visitedAfterPenacony[endCity]){ for(int col = 0;col<6;col++){ if(city[start][col] == 1 && !visitedAfterPenacony[col]){ visitedAfterPenacony[col] = true; start = col; } } if(start == 5){ break; } } // if the final city can be visited, then this test case evaluates to true if(visitedAfterPenacony[endCity]){ isPassed = true; } } // Generate output if(isPassed){ System.out.println("YES"); } else{ System.out.println("NO"); } }
Editor is loading...
Leave a Comment