DFS HDOJ 2181 哈密顿绕行世界问题

题目传送门

题意:中文题面

分析:直接排完序后DFS.这样的题以后不应该再写题解的.

#include <bits/stdc++.h>
using namespace std; vector<int> G[21];
int ans[21];
int v[3];
bool vis[21];
int cnt; void print() {
printf ("%d: ", ++cnt);
for (int i=0; i<20; ++i) {
printf ("%d ", ans[i]);
}
printf ("%d\n", ans[20]);
} void DFS(int u, int x, int step) {
for (int i=0; i<G[u].size (); ++i) {
int v = G[u][i];
if (v == x) {
if (step < 20) continue;
ans[step] = x;
print (); return ;
}
else if (vis[v]) continue;
vis[v] = true;
ans[step] = v;
DFS (v, x, step + 1);
vis[v] = false;
}
} int main(void) {
for (int i=1; i<=20; ++i) {
for (int j=0; j<3; ++j) {
scanf ("%d", &v[j]);
G[i].push_back (v[j]);
//G[v[j]].push_back (i);
}
}
for (int i=1; i<=20; ++i) {
sort (G[i].begin (), G[i].end ());
}
int m;
while (scanf ("%d", &m) == 1) {
if (!m) break;
cnt = 0;
memset (vis, false, sizeof (vis));
vis[m] = true;
ans[0] = m;
DFS (m, m, 1);
} return 0;
}

  

上一篇:Win10安装软件时出现2502、2503错误代码的问题


下一篇:dirname和basename命令