本帖最后由 tommywong 于 2021-4-18 08:55 编辑
之前玩過程序走遍所有路徑
下圖中,從A 出發,先走向B,最後回到A,不重複地走遍所有路段,有幾種不同路徑?
http://www.mathchina.com/bbs/for ... =1950355&extra=
- clc;clear;
- A=zeros(10,10);
- A(1,2)=1;A(1,4)=1;
- A(2,3)=1;A(2,4)=1;
- A(3,5)=1;A(3,6)=1;
- A(4,5)=1;
- A(5,6)=1;A(5,8)=1;
- A(6,7)=1;A(6,8)=1;
- A(7,9)=1;A(7,10)=1;
- A(8,9)=1;
- A(9,10)=1;
- A=A+A';
- a=1;
- for k=1:sum(sum(A))/2
- s(k)=0;
- T=[];
- n=size(a,1);
- for i=1:n
- B=A;
- if(k>1)
- for K=1:k-1
- B(a(i,K),a(i,K+1))=B(a(i,K),a(i,K+1))-1;
- B(a(i,K+1),a(i,K))=B(a(i,K+1),a(i,K))-1;
- end
- end
- for j=1:10
- if(B(a(i,k),j)==1 && sum(a(i,:)==j)==0)
- t=[a(i,:),j];
- T=[T;t];
- if(j==10)
- s(k)=s(k)+1;
- if(k==5)
- disp(t);
- end
- end
- end
- end
- end
- a=T;
- end
- s
- sum(s)
复制代码 我讓程序輸出了所有5步路徑
1 2 3 6 7 10
1 4 5 6 7 10
1 4 5 8 9 10
1至15步的路徑數
s=0 0 0 0 3 12 19 14 4 0 0 0 0 0
所有路徑數
sum(s)=52 |