HDU - 2181 哈密頓繞行世界問題(簡單dfs)
阿新 • • 發佈:2018-11-01
https://blog.csdn.net/libin56842/article/details/15028427
#include<bits/stdc++.h> using namespace std; int mp[25][5]; bool VIS[25]; int ans[25]; int T = 1; int n, m; void DFS(int u, int cnt) { VIS[u] = 1; ans[cnt] = u; for (int i = 0; i<3; i++) { int v = mp[u][i]; if (v == m && cnt == 19) { printf("%d: ", T++); for (int j = 0; j < 20; j++) cout << ans[j] << " "; cout << m << endl; } if (!VIS[v]) DFS(v, cnt + 1); } VIS[u] = 0; } int main() { for (int i = 1; i <= 20; i++) { cin >> mp[i][0] >> mp[i][1] >> mp[i][2]; sort(mp[i], mp[i] + 3); //如有多條路線,按字典序輸出 } while (cin>>m&&m) { memset(VIS, 0, sizeof(VIS)); DFS(m, 0); } return 0; }