18/10/22 02:17:14.19 YJqFFfdN.net
>>748
一般解
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)
c(P1st=sum(re<0),Q1st=sum(re>0),even=sum(re==0))
}
treasure(3,4,2)
treasure(5,10,3)