公交车查询系统

%gong2

A=[ 1 11 13 12 0 0 0
    2 12 13 11 0 0 0
    3 15 13 10 12 0 0
    4 15 16 11 41 18 13];
S1=11;
S2=12;%输入起始站点
[m,n]=size(A);
B=zeros(1,3);%存放直达线路
CS1=zeros(1,2);%存放S1
CS2=zeros(1,2);%存放S2
p1=0;p2=0;q1=0;q2=0;t1=0;t2=0;t3=0;
for i=1:m
    for j=1:n
        switch A(i,j)
            case 0
                break;
            case S1
                p1=i;
                q1=j;
            case S2
                p2=i;
                q2=j;
        end       
    end 
    if p1==p2 & q1        t1=t1+1;
        B(t1,:)=[p1,q1,q2];
    %需要从CS1和CS2中去掉直达线路;
    else if p1==p2 & q1>q2
            t2=t2+1;
            CS1(t2,:)=[p1,q1];
            t3=t3+1;
            CS2(t3,:)=[p2,q2];
        else if p1~=p2
                if p1~=0
                        t2=t2+1;
                        CS1(t2,:)=[p1,q1];
                end
                if p2~=0
                        t3=t3+1;
                        CS2(t3,:)=[p2,q2];
                end               
            end
        end
    end
    p1=0;p2=0;q1=0;q2=0;
end
B
CS1
CS2

 

%gong3

gong2;
[m1 n1]=size(CS1);
[m2 n2]=size(CS2);
t=0;
D=zeros(1,4);
for i1=1:m1
    for j1=CS1(i1,2)+1:n
        if A(CS1(i1,1),j1)==0
            continue;
        end
        for  i2=1:m2
            for j2=1:CS2(i2,2)-1
                if A(CS2(i2,1),j2)==0
                    continue;
                end
                if A(CS1(i1,1),j1)==A(CS2(i2,1),j2)
                    t=t+1;
                    D(t,:)=[CS1(i1,1) j1 CS2(i2,1) j2];
                end
            end
        end
    end
end
D

你可能感兴趣的:(Matlab,n2)