1. dfs 인데 2. 계속 돌길래 원인을 찾아봤더니, 0이여도 다시 제자리로 돌아와서 그렇다.... 3. 꼼꼼히 짤 필요가 있다. dfs 는 정확하게 구현하지 않으면 계속 돈다... 40... |
/* 1550 시작합니다. 존재하면 1 없으면 0 아하 그럼 배열 2개를 저장한다음에 어떻게 해야 하지 0 에서 1, 2 로 가면 도달하는 지점에 멈출 것입니다. 오께이. 이것을 큐로 넣어서 하나씩 할까요? 아니면 바로바로 쭉쭉 할까요? dfs 로 쭉쭉해보도록 하겠습니다. */ #include <iostream> #include <memory.h> using namespace std; #define SIZE 100 int one[SIZE]; int two[SIZE]; int oneV[SIZE]; int twoV[SIZE]; int n; int ans; int a, b; void problemIn() { cin >> n; memset(one, 0, sizeof(one)); memset(two, 0, sizeof(two)); for (int i = 0; i < n; i++) { cin >> a >> b; if (one[a] == 0) { one[a] = b; } else { two[a] = b; } } } void dfs(int x) { if (one[x] == 0) { // 갈곳이 없다는 뜻이므로 돌아가야 합니다. return; } if (one[x] == 99 || two[x] == 99) { ans = 1; return; } if (one[x] != 0) { dfs(one[x]); if (ans) return; } if (two[x] != 0) { dfs(two[x]); if (ans) return; } } void solve() { dfs(0); } int main() { int T; T = 10; for (int i = 0; i < 10; i++) { int tc; cin >> tc; problemIn(); solve(); if (ans == 2) ans = 0; cout << "#" << tc << " " << ans << endl; ans = 0; } return 0; } |