fork download
  1. import Data.Array
  2. import Data.List
  3.  
  4. --問題、空欄は0
  5. q:: Array (Int,Int)Int
  6. q=array((1,1),(9,9))[((x,y),0)|x<-[1..9],y<-[1..9]]
  7. q1=q//[((1,3),5),((1,4),3),
  8. ((2,1),8),((2,8),2),
  9. ((3,2),7),((3,5),1),((3,7),5),
  10. ((4,1),4),((4,6),5),((4,7),3),
  11. ((5,2),1),((5,5),7),((5,9),6),
  12. ((6,3),3),((6,4),2),((6,8),8),
  13. ((7,2),6),((7,4),5),((7,9),9),
  14. ((8,3),4),((8,8),3),
  15. ((9,6),9),((9,7),7)
  16. ]
  17.  
  18. t=[(x,y)|x<-indexX,y<-indexY]
  19.  
  20. indexX,indexY,numbers::[Int]
  21. indexX=[1..9]
  22. indexY=[1..9]
  23. numbers=[1..9]
  24.  
  25. -- (x,y)に値が入っていればそれを返す(問題の値)
  26. -- そうでなければ、その時点で可能性のある数字を列挙
  27. candidate ary x y ns
  28. | ary!(x,y)/=0 = [ary!(x,y)]
  29. | otherwise = deleteFirstsBy (==) ns ([ary!(x,yy)|yy<-indexY]++
  30. [ary!(xx,y)|xx<-indexX]++
  31. [ary!(xx,yy)|xx<-[x1..x1+2],yy<-[y1..y1+2]]
  32. )
  33. where
  34. x1=(x-1)`div`3 * 3 + 1
  35. y1=(y-1)`div`3 * 3 + 1
  36.  
  37. f []=[]
  38. f ((ary,[]):xs) = ary : f xs
  39. f ((ary,lss@((x,y):ls)):xs) = f(g ary lss (candidate ary x y numbers)) ++ f xs
  40.  
  41. g ary lss@((x,y):ls)(n:ns)=(ary//[((x,y),n)] ,ls):g ary lss ns
  42. g _ _ []=[]
  43.  
  44. main = do print$ f[(q1,t)]
  45.  
Success #stdin #stdout 0.48s 6772KB
stdin
Standard input is empty
stdout
[array ((1,1),(9,9)) [((1,1),1),((1,2),4),((1,3),5),((1,4),3),((1,5),2),((1,6),7),((1,7),6),((1,8),9),((1,9),8),((2,1),8),((2,2),3),((2,3),9),((2,4),6),((2,5),5),((2,6),4),((2,7),1),((2,8),2),((2,9),7),((3,1),6),((3,2),7),((3,3),2),((3,4),9),((3,5),1),((3,6),8),((3,7),5),((3,8),4),((3,9),3),((4,1),4),((4,2),9),((4,3),6),((4,4),1),((4,5),8),((4,6),5),((4,7),3),((4,8),7),((4,9),2),((5,1),2),((5,2),1),((5,3),8),((5,4),4),((5,5),7),((5,6),3),((5,7),9),((5,8),5),((5,9),6),((6,1),7),((6,2),5),((6,3),3),((6,4),2),((6,5),9),((6,6),6),((6,7),4),((6,8),8),((6,9),1),((7,1),3),((7,2),6),((7,3),7),((7,4),5),((7,5),4),((7,6),2),((7,7),8),((7,8),1),((7,9),9),((8,1),9),((8,2),8),((8,3),4),((8,4),7),((8,5),6),((8,6),1),((8,7),2),((8,8),3),((8,9),5),((9,1),5),((9,2),2),((9,3),1),((9,4),8),((9,5),3),((9,6),9),((9,7),7),((9,8),6),((9,9),4)]]