åŠæ ¡ã®åäŸæ代ã嚯楜ã®1ã€ã¯ã銬ã®åãã«åãããŠç®±ã®äžã®ããŒãããã¯ã«ããçš®ã®é·æ¹åœ¢mx nãå¡ãã€ã¶ãããšã§ããã éåžžã¯10x10ã®æ£æ¹åœ¢ã§ããããä»ã§ã¯åãåãã§ïŒèãçªãåºããŠïŒãã»ã«ã«æ éã«æ°åãå ¥åããŸãã 次ã®ã¹ãããã§ãããªãã¯è¡ãââæ¢ãŸãã«ããããšãç解ããæåããããçŽããŸãã ããã¯ããªãåã®ããšã§
å®éã«ããã®åé¡ã解決ããã«ã¯ã©ãããã°ããã§ããã ãHaskellãåã®åã§æ¢æ€ããŠãã ããããšããæ¬ã®èè ã®æããæãåºããŠãã ãããããã¯ãååž°çã«èãããããã«èãããŸãã ã€ãŸããäžé£ã®ã»ã«ããããã€ãã®ã»ã«ãååŸããæ®ããäœããã®æ¹æ³ã§åããå ã®ã»ã«ãæºããããã銬ã®åããã«æ¥ç¶ããããšã確èªããŸãã ããŠããã©ããããããæ®ããåããããšã¯ãã»ã«ã®ã»ãããæžãããŠååž°ã«å ¥ãããšãæå³ããŸãã é ããæ©ãããã»ããã¯åäžã®èŠçŽ ã«çž®å°ããããããååž°ã®åºç€ã«ãªããŸãã ãªã¹ããã¶ã€ãã䜿çšããã³ãŒãã§ã¯ãããšãã°æ¬¡ã®ããã«èšè¿°ã§ããŸãã
knightComb [x] = [[x]] knightComb xs = [x:ks | x <- xs, ks <- knightComb $ delete x xs, near x $ head ks] where near (x1,y1) (x2,y2) = abs ((x2-x1)*(y2-y1)) == 2
èšèªããŒã¿ããŒã¹ã«ã¯åé€æ©èœã¯ãããŸããããData.Listã¢ãžã¥ãŒã«ããããŒãã§ããŸãã
çµã¿åããé¢æ°ã®å ¥åã§ã¯ãã»ã«ã®ã»ããã座æšãã¢ïŒxãyïŒã®ãªã¹ãã®åœ¢åŒã§è¡šç€ºãããåºåã§ã¯ããã®ã»ããããã€ãã¹ãããã¹ãŠã®å¯èœãªæ¹æ³ã®ãªã¹ãã衚瀺ãããŸãã ãã¹ãŠã®ãªãã·ã§ã³ãå¿ èŠã§ã¯ãªããä»»æã®1ã€ã§ååãªå Žåã¯ã headé¢æ°ã䜿çšããŠæåã®ãªãã·ã§ã³ã䜿çšã§ããŸãããã®åŸãHaskellã®é 延ã«ãããæ®ãã¯èšç®ãããŸããã ãããïŒ çŽã®äžã§ã¯æ»ããã§ããã ã¢ã«ãŽãªãºã ã¯å®éã«æ©èœããäžå€®ã®ã»ã«ã®ãªã3x3ã®æ£æ¹åœ¢ã¯ãã¿ã³ãšå¡ãã€ã¶ãããŸãã 3x4ã®é·æ¹åœ¢ã§ã¯ãæåã®çµæã¯æ°ååŸã«è¡šç€ºãããŸãããã»ã«ã1ã€è¿œå ããã ãã§ãã®æéã¯30åã«å¢ããŸãã 倧ããªã¹ããŒã¹ã«ã€ããŠã¯ãã©ããããšãã§ããŸããã
ãŸããååãšããŠãçµæã¯èª¬æå¯èœã§ãã æåã®ã»ã«ã®æ£ããéžæã¯ãååž°ã®ããã«åºãåŸã«ãã§ãã¯ãããŸããæåã¯ãå€ãã®ã»ã«ãããããã¹ãŠãé©åããããã§ã¯ãããŸããããããã£ãŠãæ lazã«ãããããããæ€çŽ¢ã®è€éãã¯NïŒã®ãªãŒããŒã§ããåºåã§ååŸãããããé 延ãåŒãç¶ãæ©èœããŸããäœåãªæ€çŽ¢ãäœããã®æ¹æ³ã§å¶éããå¿ èŠããããŸãã
æ£ç¢ºã«ã©ã®ããã«ïŒ æåã«é ã«æµ®ãã¶ã®ã¯ãæåã«ä»»æã®ã»ã«ã§ã¯ãªããåã®åãããååŸã§ããã»ã«ãååŸããããšã§ãã ã€ãŸã 空ãã»ã«ã®ã»ããã«å ããŠãæåŸã«å æãããã»ã«ã®åº§æšãååž°å ¥åã«è»¢éããããã䜿çšããŠå¯èœãªç§»åã®ãªã¹ããäœæãããããã®ã»ã«ã空ã§ããããšã確èªããã»ã«ãçµäºãããŸã§æ°ãã移åã§åã³ååž°ã«å ¥ããŸãã
knightsTo x [] = [[x]] knightsTo x xs = [x:ks | k <- next x, elem k xs, ks <- knightsTo k $ delete k xs] where next (x,y) = [(x+dx,y+dy) | (dx,dy) <- [(-2,-1),(-2,1),(-1,-2),(-1,2),(1,-2),(1,2),(2,-1),(2,1)] ]
ãã®ãããªãã®ã ããŠã䟿å®äžãã€ã³ã¿ãŒãã§ãŒã¹ãäœæããŸã
knights n = head . knightsTo (1,1) $ tail [(x,y) | x <- [1..n], y <- [1..n]]
ãããŠãç§ã¯èšããªããã°ãªããªããããã¯é¡èãªåé²ã§ãïŒ
*ã¡ã€ã³>éšå£«5
[ïŒ1,1ïŒãïŒ2,3ïŒãïŒ1,5ïŒãïŒ3,4ïŒãïŒ1,3ïŒãïŒ2,1ïŒãïŒ4,2ïŒãïŒ5,4ïŒãïŒ 3.5ïŒãïŒ1.4ïŒãïŒ2.2ïŒãïŒ4.1ïŒãïŒ3.3ïŒãïŒ2.5ïŒãïŒ4.4ïŒãïŒ5.2ïŒãïŒ3ã 1ïŒãïŒ1,2ïŒãïŒ2,4ïŒãïŒ4,5ïŒãïŒ5,3ïŒãïŒ3,2ïŒãïŒ5,1ïŒãïŒ4,3ïŒãïŒ5,5ïŒ ]
*ã¡ã€ã³>éšå£«6
[ïŒ1,1ïŒãïŒ2,3ïŒãïŒ1,5ïŒãïŒ3,4ïŒãïŒ1,3ïŒãïŒ2,1ïŒãïŒ3,3ïŒãïŒ1,2ïŒãïŒ 2.4ïŒãïŒ1.6ïŒãïŒ3.5ïŒãïŒ5.6ïŒãïŒ6.4ïŒãïŒ5.2ïŒãïŒ3.1ïŒãïŒ4.3ïŒãïŒ6ã 2ïŒãïŒ4.1ïŒãïŒ2.2ïŒãïŒ1.4ïŒãïŒ2.6ïŒãïŒ4.5ïŒãïŒ6.6ïŒãïŒ5.4ïŒãïŒ4.2ïŒ ãïŒ6.1ïŒãïŒ5.3ïŒãïŒ3.2ïŒãïŒ5.1ïŒãïŒ6.3ïŒãïŒ5.5ïŒãïŒ3.6ïŒãïŒ4.4ïŒãïŒ 2.5ïŒãïŒ4.6ïŒãïŒ6.5ïŒ]
*ã¡ã€ã³>éšå£«7
[ïŒ1,1ïŒãïŒ2,3ïŒãïŒ1,5ïŒãïŒ2,7ïŒãïŒ3,5ïŒãïŒ1,4ïŒãïŒ2,2ïŒãïŒ3,4ïŒãïŒ 1.3ïŒãïŒ2.1ïŒãïŒ3.3ïŒãïŒ1.2ïŒãïŒ2.4ïŒãïŒ1.6ïŒãïŒ3.7ïŒãïŒ2.5ïŒãïŒ1ã 7ïŒãïŒ3.6ïŒãïŒ4.4ïŒãïŒ3.2ïŒãïŒ5.1ïŒãïŒ4.3ïŒãïŒ3.1ïŒãïŒ5.2ïŒãïŒ6.4ïŒ ãïŒ7.2ïŒãïŒ5.3ïŒãïŒ4.1ïŒãïŒ6.2ïŒãïŒ7.4ïŒãïŒ5.5ïŒãïŒ7.6ïŒãïŒ5.7ïŒãïŒ 4,5ïŒãïŒ2,6ïŒãïŒ4,7ïŒãïŒ6,6ïŒãïŒ5,4ïŒãïŒ4,6ïŒãïŒ6,7ïŒãïŒ7,5ïŒãïŒ5ã 6ïŒãïŒ7.7ïŒãïŒ6.5ïŒãïŒ7.3ïŒãïŒ6.1ïŒãïŒ4.2ïŒãïŒ6.3ïŒãïŒ7.1ïŒ]
*ã¡ã€ã³>éšå£«8
[ïŒ1,1ïŒãïŒ2,3ïŒãïŒ1,5ïŒãïŒ2,7ïŒãïŒ3,5ïŒãïŒ1,4ïŒãïŒ2,2ïŒãïŒ3,4ïŒãïŒ 1.3ïŒãïŒ2.1ïŒãïŒ3.3ïŒãïŒ1.2ïŒãïŒ2.4ïŒãïŒ1.6ïŒãïŒ2.8ïŒãïŒ3.6ïŒãïŒ1ã 7ïŒãïŒ2.5ïŒãïŒ3.7ïŒãïŒ1.8ïŒãïŒ2.6ïŒãïŒ3.8ïŒãïŒ4.6ïŒãïŒ5.4ïŒãïŒ4.2ïŒ ãïŒ6.1ïŒãïŒ5.3ïŒãïŒ3.2ïŒãïŒ4.4ïŒãïŒ5.2ïŒãïŒ3.1ïŒãïŒ4.3ïŒãïŒ5.1ïŒãïŒ 7.2ïŒãïŒ6.4ïŒãïŒ4.5ïŒãïŒ5.7ïŒãïŒ7.8ïŒãïŒ8.6ïŒãïŒ6.5ïŒãïŒ8.4ïŒãïŒ7ã 6ïŒãïŒ8.8ïŒãïŒ6.7ïŒãïŒ4.8ïŒãïŒ5.6ïŒãïŒ6.8ïŒãïŒ4.7ïŒãïŒ5.5ïŒãïŒ7.4ïŒ ãïŒ8.2ïŒãïŒ6.3ïŒãïŒ7.1ïŒãïŒ8.3ïŒãïŒ7.5ïŒãïŒ8.7ïŒãïŒ6.6ïŒãïŒ5.8ïŒãïŒ 7.7ïŒãïŒ8.5ïŒãïŒ7.3ïŒãïŒ8.1ïŒãïŒ6.2ïŒãïŒ4.1ïŒ]
ãããŠå¿èããããªã
*ã¡ã€ã³>éšå£«9
[ïŒ1,1ïŒãïŒ2,3ïŒãïŒ1,5ïŒãïŒ2,7ïŒãïŒ1,9ïŒãïŒ3,8ïŒãïŒ1,7ïŒãïŒ2,5ïŒãïŒ 1.3ïŒãïŒ2.1ïŒãïŒ3.3ïŒãïŒ1.2ïŒãïŒ2.4ïŒãïŒ1.6ïŒãïŒ2.8ïŒãïŒ3.6ïŒãïŒ4ã 4ïŒãïŒ3.2ïŒãïŒ5.1ïŒãïŒ4.3ïŒãïŒ2.2ïŒãïŒ1.4ïŒãïŒ2.6ïŒãïŒ1.8ïŒãïŒ3.7ïŒ ãïŒ2.9ïŒãïŒ4.8ïŒãïŒ5.6ïŒãïŒ3.5ïŒãïŒ4.7ïŒãïŒ3.9ïŒãïŒ5.8ïŒãïŒ4.6ïŒãïŒ 3.4ïŒãïŒ4.2ïŒãïŒ5.4ïŒãïŒ6.2ïŒãïŒ4.1ïŒãïŒ5.3ïŒãïŒ4.5ïŒãïŒ5.7ïŒãïŒ4ã 9ïŒãïŒ6.8ïŒãïŒ7.6ïŒãïŒ5.5ïŒãïŒ6.3ïŒãïŒ7.1ïŒãïŒ9.2ïŒãïŒ8.4ïŒãïŒ6.5ïŒ ãïŒ8.6ïŒãïŒ9.8ïŒãïŒ7.9ïŒãïŒ6.7ïŒãïŒ5.9ïŒãïŒ7.8ïŒãïŒ9.9ïŒãïŒ8.7ïŒãïŒ 6.6ïŒãïŒ7.4ïŒãïŒ9.5ïŒãïŒ8.3ïŒãïŒ9.1ïŒãïŒ7.2ïŒãïŒ9.3ïŒãïŒ8.1ïŒãïŒ7ã 3ïŒãïŒ6.1ïŒãïŒ8.2ïŒãïŒ9.4ïŒãïŒ7.5ïŒãïŒ9.6ïŒãïŒ8.8ïŒãïŒ6.9ïŒãïŒ7.7ïŒ ãïŒ8.9ïŒãïŒ9.7ïŒãïŒ8.5ïŒãïŒ6.4ïŒãïŒ5.2ïŒãïŒ3.1ïŒ]
10x10ã®æ£æ¹åœ¢ã§ã¯ãçµæãåŸ ã¡ãŸããã§ããã ããããããã«ãããããããååž°ã®åã¹ãããã§ãæ°ããåŒã³åºãã®æ°ã¯çŸåšãããªãŒã»ã«ã®æ°ã§ã¯ãªããå¯èœãªç§»åã®æ°ã«äŸåããŸãã ã€ãŸã ã¢ã«ãŽãªãºã ã®è€éãã¯OïŒp ^ NïŒã«ãªããŸããã å€é åŒã§ããããŸãããããã§ã«é²ãã§ããŸãã
次ã®é¢æ°ã¯äœåãªåããçæããå¯èœæ§ããããŸãããããã¯elemãã§ãã¯ã«ãã£ãŠåæãããŸããããªã¹ãã8åå®è¡ããããšã¯éå¹ççã§ãã 空ãã»ã«ã®ãªã¹ããäžåºŠå®è¡ããŠãé©åãªã»ã«ãé€å€ããæ¹ãçã«ããªã£ãŠããŸãã
knightsTo x [] = [[x]] knightsTo x xs = [x:ks | k <- filter (near x) xs, ks <- knightsTo k $ delete k xs] where near (x1,y1) (x2,y2) = abs ((x2-x1)*(y2-y1)) == 2
ããããå®éã«ã¯ãããã¯ããã©ãŒãã³ã¹ã«ãŸã£ãã圱é¿ãäžããªãããšã瀺ãããŠããŸããã»ãŒåæã«ããŸã£ããåãçãããããŸãã å®éãã¢ã«ãŽãªãºã ã®è€éãã¯ãŸã£ããå€åããŠããŸããããã ããå¯èœãªç§»åã¯æ¬¡ã®é¢æ°ã§å³å¯ã«å®çŸ©ãããé åºã§ã¯ãªãã空ãã»ã«ã®ãªã¹ãã«è¡šç€ºãããé åºã§éžæãããŸãã ãããŠãéšåçã«é åºä»ããããã»ããã§ã¯ããœãªã¥ãŒã·ã§ã³ã¯ããé«éã«ãªããŸãã é äžåã®ã»ããã§ã¯ãããããåæ¢ããããšããããããŸãã
ä»ã«ã©ã®ããã«äžå¿ èŠãªãã¹ãã£ã³ã°ãå¶éã§ããŸããïŒ ããšãã°ãååž°ã®åã¹ãããã§ãå€ç¬ãªã»ã«ããªããã©ããã®å ¥åãªã¹ãã®è¿œå ãã§ãã¯ãç·šæããããšãã§ããŸãã å ¥åã»ããå ã®å空ã®ã»ã«ã«ã¯ãã»ããå šäœã1ã€ã®ã»ã«ã§æ§æãããŠããªãéããå°ãªããšã1ã€ã®ç©ºããã€ããŒãå¿ èŠã§ãã ãã ãããã®ãã¹ãã®åŸãã«ããã«ãããªãã«ãªã©ã¯ã¹ããŒããããŸãŸã«ãªãå¯èœæ§ããããŸãã ã»ã«ã ãããŠãäžè¬åããã«ã¯ãæ®ãã®é åå šäœã®æ¥ç¶æ§ã確èªããå¿ èŠããããŸãã ãŸããäžè¬çãªæ¥ç¶ãšã¯ãçŸåšã®äœçœ®ããæ®ãã®ã»ã«ãžã®ãã¹ãå¿ èŠã§ããããšãæå³ããŸãã ãã®ãããªãã§ãã¯ã¯ãããšãã°ãè£å©é¢æ°ãäœæãããã¯ã€ãããã€ãã¹ã¢ã«ãŽãªãºã ã«ãã£ãŠå®è¡ã§ããŸãã
connected _ [] = True connected [] _ = False connected (x:xs) ws = let ns = filter (near x) ws in connected (xs++ns) (ws\\ns)
å ¥åé¢æ°ã«ã¯2ã€ã®ãªã¹ãããããŸãããã§ãã¯ãããé ç¹ãšæªæ€èšŒã®é ç¹ã§ãã2çªç®ã®é ç¹ã空ã«ãªã£ãå Žåãé å
ã¡ã€ã³é¢æ°ã«æ°ãããã§ãã¯ãè¿œå ããŸã
knightsTo x [] = [[x]] knightsTo x xs = [x:ks | connected [x] xs, k <- filter (near x) xs, ks <- knightsTo k $ delete k xs]
ãããŠãè¿åãã§ãã¯æ©èœãã°ããŒãã«ã«èšè¿°ããŸã
near (x1,y1) (x2,y2) = abs ((x2-x1)*(y2-y1)) == 2
ããã¯ããªãããŸããããŸãïŒ 9x9ã¹ã¯ãšã¢ã®ãœãªã¥ãŒã·ã§ã³ã¯ã8x8ã¹ã¯ãšã¢ã«ä»¥åå¿ èŠã ã£ãæéãããé«éã«ãªããŸããã ããã¯ãè¿œå ãã§ãã¯ã®è€éããå ¥åããŒã¿ã«äºæ¬¡çã«äŸåãããšããäºå®ã«ããããããã§ãã ããããã¢ã«ãŽãªãºã ã®å šäœçãªè€éãã¯ææ°é¢æ°çã§ããããããã®ã³ã¹ãã§ãã£ãŠãäžèŠãªåå²ãç Žæ£ãããšãèšç®ãå€§å¹ ã«åæžãããŸãã
çºè¡ããããœãªã¥ãŒã·ã§ã³ãåæãããšã移åéžæã¢ã«ãŽãªãºã ããšãªã¢ã®å·ŠåŽã®éšåãå¯ã«åããããšããŠããããšãããããŸããå°ãªããšãæåã¯ããŸãæ©èœããŸããããã¯ã倧ããªãšãªã¢ãå°ããªã³ã³ãã¯ããªã¹ããŒã¹ã«åå²ãããšããã¢ã€ãã¢ã瀺åããŠããŸãã çŽ æŽãããã¢ã€ãã¢ïŒ 10x10ã®åé¡ã®æ£æ¹åœ¢ã¯4ã€ã®5x5ã®æ£æ¹åœ¢ã§ããããã®ããã«ã¯ãªãã¯ããŸãã ç¹å®ã®ãã€ã³ãããéå§ããæ¹æ³ãåŠç¿ããå¿ èŠãããã ãã§ãªããæå®ããå Žæã§çµäºããŸãã ãããè¡ãã«ã¯ãã€ã³ã¿ãŒãã§ã€ã¹é¢æ°ãå€æŽããŸã
knights5 ((m,n), st, fin) = head . filter (end fin) . knightsTo st $ delete st [(x,y) | x <- [m..m+4], y <- [n..n+4]] where end x xs = x == last xs
é åã®ãµã€ãºãåºå®ããå ¥åæã«5x5ã®æ£æ¹åœ¢ã®å·Šäžé ã®åº§æšãç®çã®ãã¹ã®éå§ç¹ãšçµäºç¹ã®åº§æšãæå®ããŸãã ãããŠããã®é¢æ°ã4åãç®çã®ãã©ã¡ãŒã¿ãŒã«é©çšããŸãã
knights10 = concatMap knights5 [((1,1),(5,3),(5,5)), ((1,6),(3,6),(5,6)), ((6,6),(6,8),(6,6)), ((6,1),(8,5),(6,5))]
åºæ¥äžããïŒ
*ã¡ã€ã³> knights10
[ïŒ5.3ïŒãïŒ4.1ïŒãïŒ2.2ïŒãïŒ1.4ïŒãïŒ3.3ïŒãïŒ4,5ïŒãïŒ2,4ïŒãïŒ1,2ïŒãïŒ 3.1ïŒãïŒ5.2ïŒãïŒ4.4ïŒãïŒ2.5ïŒãïŒ1.3ïŒãïŒ2.1ïŒãïŒ4.2ïŒãïŒ5.4ïŒãïŒ3ã 5ïŒãïŒ4.3ïŒãïŒ5.1ïŒãïŒ3.2ïŒãïŒ1.1ïŒãïŒ2.3ïŒãïŒ1.5ïŒãïŒ3.4ïŒãïŒ5.5ïŒ ãïŒ3.6ïŒãïŒ1.7ïŒãïŒ2.9ïŒãïŒ4.10ïŒãïŒ3.8ïŒãïŒ5.7ïŒãïŒ4.9ïŒãïŒ2.10ïŒãïŒ 1.8ïŒãïŒ2.6ïŒãïŒ4.7ïŒãïŒ5.9ïŒãïŒ3.10ïŒãïŒ1.9ïŒãïŒ2.7ïŒãïŒ4.6ïŒãïŒ5ã 8ïŒãïŒ3.7ïŒãïŒ1.6ïŒãïŒ2.8ïŒãïŒ1.10ïŒãïŒ3.9ïŒãïŒ5.10ïŒãïŒ4.8ïŒãïŒ5.6ïŒ ãïŒ6.8ïŒãïŒ7.6ïŒãïŒ8.8ïŒãïŒ7.10ïŒãïŒ9.9ïŒãïŒ10.7ïŒãïŒ8.6ïŒãïŒ6.7ïŒãïŒ 7.9ïŒãïŒ9.10ïŒãïŒ10.8ïŒãïŒ9.6ïŒãïŒ7.7ïŒãïŒ6.9ïŒãïŒ8.10ïŒãïŒ10.9ïŒãïŒ9ã 7ïŒãïŒ7.8ïŒãïŒ6.10ïŒãïŒ8.9ïŒãïŒ10.10ïŒãïŒ9.8ïŒãïŒ10.6ïŒãïŒ8.7ïŒãïŒ6.6ïŒ ãïŒ8.5ïŒãïŒ6.4ïŒãïŒ7.2ïŒãïŒ9.1ïŒãïŒ8.3ïŒãïŒ10.4ïŒãïŒ9.2ïŒãïŒ7.1ïŒãïŒ 6.3ïŒãïŒ7.5ïŒãïŒ9.4ïŒãïŒ10.2ïŒãïŒ8.1ïŒãïŒ6.2ïŒãïŒ7.4ïŒãïŒ9.5ïŒãïŒ10ã 3ïŒãïŒ8.4ïŒãïŒ10.5ïŒãïŒ9.3ïŒãïŒ10.1ïŒãïŒ8.2ïŒãïŒ6.1ïŒãïŒ7.3ïŒãïŒ6.5ïŒ ]
æ°ç§åŸ ã£ãŠãããããã«è€éãªã¿ã¹ã¯ã®è§£æ±ºçãèŠã€ããŸããã 銬ã®ã³ãŒã¹ã§èŠã€ãã£ããã¹ã®çµãããããæåã«å°éã§ããŸãã ç§ãã¡ã®æ±ºå®ã¯åšæçã§ãã
å®éãå®å šãªå¹žçŠã®ããã«ãé©åãªãã§ãŒã³ãæ§ç¯ããŠã倧ããªãšãªã¢ãå°ããªãšãªã¢ã«åå²ããããã»ã¹ãèªååããã ãã§ãã ãããã圌ããèšãããã«ãããã¯ãŸã£ããå¥ã®ã¿ã¹ã¯ã§ãã
ããŒã2
ããŒã3