function x=gmaze(P)
% generate a tour of the maze according to P

[m,n]=size(P);

x=zeros(1,m);
r=rand(m,1);

for i=1:m,
for k=1:n,
	if((sum(P(i,1:k-1))<r(i))&&(r(i)<=sum(P(i,1:k)))),
		x(i)=k;
    end

if x(i)==0,
        if P(i,1)~=0,
           x(i)=1;
        elseif P(i,2)~=0,
		   x(i)=2;
        elseif P(i,3)~=0,
		   x(i)=3;
        else
           x(i)=4;
        end
end

end				
end



