개발자 톡
연습문제 톡
[HSAT 6회 정기 코딩 인증평가 기출] 출퇴근길
왜 틀렸는지 모르겠어요 ㅠ
- 등록일
- 2024-02-02 15:41:11
- 조회수
- 532
- 작성자
- nayeon16
틀린 이유를 모르겠는데 한 번 봐주실 분 계신가요 ㅠㅠ ?!
import java.util.*; public class Main { static int n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); int m = sc.nextInt(); int[][] map = new int[n+1][n+1]; int[][] reverseMap = new int[n+1][n+1]; for(int i=0; i<m; i++){ int x = sc.nextInt(); int y = sc.nextInt(); map[x][y] = 1; reverseMap[y][x] = 1; } int s = sc.nextInt(); //집 int t = sc.nextInt(); //회사 ArrayList<Integer> homeStart = bfs(s,t, map); System.out.println(homeStart); ArrayList<Integer> companyStart = bfs(t,s, map); System.out.println(companyStart); ArrayList<Integer> homeStartRev = bfs(s,t, reverseMap); System.out.println(homeStartRev); ArrayList<Integer> companyStartRev = bfs(t,s, reverseMap); System.out.println(companyStartRev); int ans = 0; for(int i=0; i<homeStart.size() ; i++){ int cnt = 0; if(companyStart.contains(homeStart.get(i))) cnt++; if(homeStartRev.contains(homeStart.get(i))) cnt++; if(companyStartRev.contains(homeStart.get(i))) cnt++; if(cnt == 3) ans++; } System.out.println(ans); } public static ArrayList<Integer> bfs(int start, int end, int[][] map){ Queue<Integer> queue = new LinkedList<Integer>(); ArrayList<Integer> route = new ArrayList<Integer>(); boolean[] visited = new boolean[n+1]; queue.add(start); visited[start] = true; while(!queue.isEmpty()){ int tmp = queue.remove(); for(int i=1; i<=n; i++){ if(tmp != i && map[tmp][i] == 1 && !visited[i]){ if(i == end){ continue; } queue.add(i); visited[i] = true; route.add(i); } } } return route; } }
#[hsat_6회_정기_코딩_인증평가_기출]_출퇴근길