fork(4) download
  1. import Data.List
  2. import Control.Monad
  3. import Data.Array
  4. s r=let{h as bs=[(a,b)|a<-as,b<-bs];(&)m k=(\(Just x)->x)$lookup k m;j=Just;n=Nothing;c=[1..r];q=delete;u=h[1..r]c;o=[(s,[u |u<-[h[1..r][c]|c<-c]++[h[r]c|r<-[1..r]]++[zip[1..r][1..r],zip[1..r][r,r-1..1]],s`elem`u])|s<-u];k=foldr(>=>)j;a p d g0=k[t p d2|d2<-q d(g0!p)]g0;t p d g0|not(d`elem`(g0!p))=j g0|[]<-v=n|[d2]<-v=k[t s2 d2|s2<-[(s,delete s$nub(concat(o&s)))|s<-u]&p]g1|True=k[l[s|s<-u,not(d`elem`v)]|u<-o&p]g1 where{v=q d(g0!p);g1=g0//[(p,v)];l[]_=n;l[d3]g=a d3 d g;l _ r=j r};w g0|and[case g0!s of{[_]->True;_->False}|s<-u]=j g0|True=msum[a s' d g0>>=w|d<-g0!s']where(_,s')=minimumBy(\(a,_)(b,_)->compare a b)[(l,s)|s<-u,let v=g0!s;l=length v,l>1]}in fmap(fmap(\[x]->x))$w$array((1,1),(r,r))[((i,j),[1..r])|i<-[1..r],j<-[1..r]]
  5.  
  6. showSudoku g =
  7. unlines [ unwords [ show (g ! (i,j)) | j <- [min0..max0]] | i <- [min1..max1] ]
  8. where ((min0, min1),(max0,max1)) = bounds g
  9.  
  10. main = mapM_ (mapM_(putStrLn.showSudoku).maybe[](:[]).s) [4..8]
Success #stdin #stdout 0.01s 4680KB
stdin
Standard input is empty
stdout
1 2 3 4
4 3 2 1
2 1 4 3
3 4 1 2

1 2 3 4 5
5 3 4 1 2
4 5 2 3 1
2 4 1 5 3
3 1 5 2 4

1 2 3 4 5 6
6 3 4 2 1 5
5 1 6 3 2 4
3 4 2 5 6 1
2 5 1 6 4 3
4 6 5 1 3 2

1 2 3 4 5 6 7
3 4 6 7 2 1 5
2 7 5 6 3 4 1
5 6 1 2 7 3 4
7 3 4 1 6 5 2
4 5 2 3 1 7 6
6 1 7 5 4 2 3

1 2 3 4 5 6 7 8
8 3 4 2 6 5 1 7
3 7 5 8 4 2 6 1
4 5 7 6 3 1 8 2
2 8 1 5 7 4 3 6
5 1 6 7 2 8 4 3
6 4 8 3 1 7 2 5
7 6 2 1 8 3 5 4