码迷,mamicode.com
首页 > 其他好文 > 详细

哈密顿路

时间:2015-10-27 15:06:43      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 1 var
 2   n,m,i,j,k1,k2:longint;
 3   a:array[1..20,1..20] of longint;
 4   b:array[1..20] of longint;
 5   visited:array[1..20] of boolean;
 6   flag:boolean;
 7 procedure print(k:longint);
 8 var i:longint;
 9 begin
10   if k=0 then writeln(no round)
11   else
12    begin
13      for i:=1 to n-1 do write(b[i], );
14      writeln(b[n]);
15    end;
16 //  halt;
17 end;
18 procedure dfs(i,k:longint);
19 var j:longint;
20 begin
21   if k=n then begin print(1); flag:=true; end;
22   for j:=1 to n do
23    if (a[j,i]=1) and (visited[j]=false) then
24     begin
25       visited[j]:=true;
26       b[k+1]:=j;
27       dfs(j,k+1);
28       visited[j]:=false;
29     end;
30 end;
31 begin
32   readln(n,m);
33   fillchar(a,sizeof(a),0);
34   for i:=1 to m do
35    begin
36      readln(k1,k2);
37      a[k1,k2]:=1;
38      a[k2,k1]:=1;
39    end;
40    for i:=1 to n do
41     begin
42       fillchar(visited,sizeof(visited),false);
43       b[1]:=i;
44       visited[i]:=true;
45       dfs(i,1);
46     end;
47   if not(flag) then print(0);
48 end.

 

哈密顿路

标签:

原文地址:http://www.cnblogs.com/vacation/p/4913899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!