remove<-function(X,N) { T<-list() for(v in X) { tmp<-c() if(length(v)>0) { for(item in v) if(item!=N) tmp<-c(tmp,item) } else { tmp<-NULL } T<-c(T,list(tmp)) } T[N]<-list(NULL) T } solvable<-function(X,path) { ret<-TRUE for(i in 1:length(X)) ret<-ret & (i %in% path | length(X[[i]])>0) ret } mirror<-function (m) { m <- as.data.frame(m) m <- rev(m) as.matrix(m) } bindH<-function(m1,m2) { matrix(c(as.integer(m1),as.integer(m2)),ncol=10) } bindV<-function(m1,m2) { m1<-t(m1) m2<-t(m2) t(matrix(c(as.integer(m1),as.integer(m2)),ncol=10)) } X <- list() X <- c(X, list(c(4,13,16))) X <- c(X, list(c(5,14,17))) X <- c(X, list(c(11,15,18))) X <- c(X, list(c(1,12,19))) X <- c(X, list(c(2,13,20))) X <- c(X, list(c(9,18,21))) X <- c(X, list(c(10,19,22))) X <- c(X, list(c(16,20,23))) X <- c(X, list(c(6,17,24))) X <- c(X, list(c(7,18,25))) X <- c(X, list(c(3,14,23))) X <- c(X, list(c(4,15,24))) X <- c(X, list(c(1,5,21,25))) X <- c(X, list(c(2,22,11))) X <- c(X, list(c(3,23,12))) X <- c(X, list(c(1,8,19))) X <- c(X, list(c(2,9,20))) X <- c(X, list(c(3,6,10))) X <- c(X, list(c(4,7,16))) X <- c(X, list(c(5,8,17))) X <- c(X, list(c(6,13,24))) X <- c(X, list(c(7,14,25))) X <- c(X, list(c(8,11,15))) X <- c(X, list(c(9,12,21))) X <- c(X, list(c(10,13,22))) store<-X path<-c() path[25]<-1 while(length(path)<25 | (length(path)==25 & !(path[25]==22))) { X<-store path<-c(14) i<-1 while(length(X[[path[i]]])>0 && solvable(X,path)) { path<-c(path,X[[path[i]]][sample(1:length(X[[path[i]]]), 1)]) X<-remove(X,path[i]) i<-i+1 } } tmp<-c() for(i in 1:25) tmp[path[i]]<-i q1<-t(matrix(tmp,ncol=5)) q2<-101-mirror(q1) q3<-76-q1 q4<-mirror(q1)+25 solution<-bindV(bindH(q1,q2),bindH(q4,q3)) solution<-solution-solution[1,1]+1 solution[solution<1]<-solution[solution<1]+100 solution sink("cento_revello.htm") cat("") for(y in 1:10) { for(x in 1:10) { if(x==1) cat("") cat(paste("",sep="")) if(x==10) cat("") } } cat("
",solution[y,x],"
") sink()