18/10/26 12:56:44.55 +rLvZj8m.net
treasure <- function(m=3,n=4,k=2){
y=1:(m*n)
(z=matrix(y,ncol=n,byrow=T))
(P=as.vector(z))
(Q=as.vector(t(z)))
PQ <- function(x){
p=q=numeric(k)
for(i in 1:k){
p[i]=which(P==x[i])
q[i]=which(Q==x[i])
}
min(p)-min(q)
}
tre=combn(m*n,k)
re=apply(tre,2,PQ)
return(c(短軸有利=sum(re<0),長軸有利=sum(re>0),同等=sum(re==0)))
}
sapply(1:12,function(k) treasure(3,4,k))
sapply(1:20,function(k) treasure(4,5,k))