paiza ãªã³ã©ã¤ã³ããã«ã½ã³ã¨ããã¤ãã³ãã§ã15 ããºã«ã解ãããã°ã©ã ã®ä½æã«ææ¦ãã¦ã¿ã¾ãããã¤ãã³ãã®å
¬å¼ãµã¤ãã¯ãã¡ãã§ãã
Rena and Minami International Programming Competition |Paiza Online Hackathon 5
ã¤ãã³ãã§ã¯ãããããã決ãããã 5 種é¡ã®ç¤é¢ã対象ã«ãã¦ããããã解ãã®ã«è¦ããææ°ã®çãã競ãå½¢å¼ã«ãªã£ã¦ãã¾ããã¤ãã³ãã®æ§æ ¼ä¸ãåé¡ãåºå®ãã¦ããã®ã ã¨æãã¾ãããå®è£ ããããã°ã©ã ã®åä½ã確èªãããæ§è½ãè©ä¾¡ãããããããã«ã¯ãã¤ãã³ãã§ç¨æãããç¤é¢ã ãã§ã¯ãªããããå¤ãã®ç¤é¢ã解ããã¦ã¿ããã¨ããã§ãã
ããã§ä»åã15 ããºã«ã®åé¡ãã©ã³ãã ã«çæããããã°ã©ã ã PHP ã§å®è£ ãã¦ã¿ã¾ããã以ä¸ã®ããã«æ¸ãã¾ããããã°ã©ã ãå®è¡ããã¨ã解ããåé¡ãã©ã³ãã ã«ä¸ã¤çæãã¦ããªã³ã©ã¤ã³ããã«ã½ã³ã®å ¥åãã©ã¼ãããã«åãããå½¢ã§åºåãã¾ãã
<?php function main() { $problem = range(0, 15); shuffle($problem); if (!isSolvable($problem)) { $problem = flipLR($problem); } printProblem($problem); } function isSolvable(array $problem) { $i = 0; while (!is_null($n = array_shift($problem))) { if ($n > 0) { $i += count(array_filter($problem, function ($m) use ($n) { return $m != 0 && $m < $n; })); } else { $i += intval(count($problem) / 4); } } return $i % 2 == 0; } function flipLR(array $problem) { return array_map(function ($i) use ($problem) { return $problem[$i]; }, [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12]); } function printProblem(array $problem) { foreach ($problem as $i => $n) { printf("%s%s", $n ?: '*', $i % 4 < 3 ? ' ' : "\n"); } } main();
15 ããºã«ã®åé¡çæã§é£ããã®ã¯ãé©å½ãªç¤é¢ãä¸ããã¨ãã«ãããã解ããåé¡ãã©ãããå¤å®ãããã¨ã§ããã¦ã§ãã§èª¿ã¹ã¦ã¿ãã¨ãããããã¯ç°¡åã«å¤å®å¯è½ã ã¨ãããã¨ãåããé¢ç½ãã£ãã®ã§ãç´¹ä»ãã¾ããä¸è¨ã®ã¦ã§ããã¼ã¸ã«å¤å®æ¹æ³ãè¨è¼ããã¦ãã¾ãã
15 Puzzle -- from Wolfram MathWorld
以ä¸ã®ç¤é¢ãä¾ã«ãã¦ãå ·ä½çã«èª¬æãã¾ãã
10 | 8 | 4 | 14 |
15 | 12 | 13 | |
7 | 11 | 6 | 1 |
5 | 9 | 2 | 3 |
ã¾ãããã®ç¤é¢ã®æ°åãå·¦ããå³ãä¸ããä¸ã®é ã«ä¸ç´ç·ã«ä¸¦ã¹ã¾ãããã®ã¨ã空ç½ã®ãã¹ã¯ç¡è¦ãã¾ãã次ã®ããã«ãªãã¾ãã
(10, 8, 4, 14, 15, 12, 13, 7, 11, 6, 1, 5, 9, 2, 3)
ãã®ããã«ä¸¦ã¹ãç¶æ ã§ãããããã®æ°åã«ã¤ãã¦ãèªåããå³å´ã«ãèªåããå°ããªæ°åãããã¤ããããæ°ãã¾ããå é ã® 10 ã«ã¤ãã¦èª¿ã¹ãã¨
(8, 4, 7, 6, 1, 5, 9, 2, 3)
ã®åè¨ 9 åã次㮠8 ã«ã¤ãã¦èª¿ã¹ãã¨
(4, 7, 6, 1, 5, 2, 3)
ã® 7 åããã®æ¬¡ã® 4 ã«ã¤ãã¦èª¿ã¹ãã¨
(1, 2, 3)
ã® 3 åãã¨ãã£ãå ·åã§ãããããæå¾ã®æ°åã¾ã§ç¶ãã¦ããã¹ã¦ãåè¨ãã¾ããä»åã®ä¾ã§ã¯
9 + 7 + 3 + 10 + 10 + 8 + 8 + 5 + 6 + 4 + 0 + 2 + 2 + 0 + 0 = 74
ã¨æ±ã¾ãã¾ãã
次ã«ãå ã®ç¤é¢ã§ç©ºç½ã®ãã¹ãä½è¡ç®ã«ãã£ããã調ã¹ã¦ããã®æ°åã«è¶³ãåããã¾ããä»åã®ä¾ã§ã¯ 2 è¡ç®ã«ç©ºç½ããã£ãã®ã§ã
74 + 2 = 76
ã¨ãªãã¾ãã
ãããã¦å¾ãããæ°åãå¶æ°ãªãããã® 15 ããºã«ã¯è§£ãã¾ããå¥æ°ãªã解ãã¾ããã
ä½ã¨ãä¸æè°ãªè©±ã§ããã以ä¸ã®ãã¼ã¸ã§ã¯ããã®æ¹æ³ã§å¤å®ã§ããçç±ãä¸å¯§ã«èª¬æããã¦ãã¾ã*1ã空ç½ã®ãã¹ãä¸ä¸å·¦å³ã®ããããã«åããæä½ãèããã¨ãå·¦å³ã®ç§»åã§ã¯æ°åã¯å¤ããããä¸ä¸ã®ç§»åã§ã¯æ°åã¯å¿
ãå¶æ°ã ãå¤ãã (大å°é¢ä¿ã«ããæ°åã®åè¨ãå¥æ°ã ãå¤ããã空ç½ã®è¡çªå·ã 1 å¤ããã®ã§ãå
¨ä½ã¨ãã¦ã¯å¶æ°ã ãå¤ãã) ã¨ãããã¨ã§ãã解ãã¦ããç¤é¢ã®æ°åã¯å¶æ°ãªã®ã§ãå¥æ°ã®ç¤é¢ããããã空ç½ãåããã¦ãã解ãããã¨ã¯ããã¾ããã
Solvability of the Tiles Game
ãã¦ãã¦ã§ããã¼ã¸ã«ã以ä¸ã®ããã«è¨è¼ããã¦ããã¨ãããããã¯ãå¥æ°ã®ç¤é¢ã解ããªãããã¨ã®è¨¼æã«ã¯ãªãã¾ããããå¶æ°ã®ç¤é¢ãå¿ ã解ããããã¨ã®è¨¼æã«ã¯ãªã£ã¦ãã¾ããããã®ããã«ã¯ãå¶æ°ã®ç¤é¢å士ã¯ãäºãã«è¡ãæ¥ã§ãããã¨ã示ãã¾ãã
So you have shown that all solvable states have the stated property. But could there be some unsolvable states that have it too?
ããã«é¢ãã¦ã¯ã次ã®ã¦ã§ããã¼ã¸ã®èª¬æãåèã«ãªãã¾ãããã®ã¦ã§ããã¼ã¸ã§ã¯ã決ã¾ã£ãæé ã«æ²¿ã£ã¦ 15 ããºã«ã解ãæ¹æ³ãå
·ä½çã«èª¬æããã¦ãã¾ããã¾ãä¸ååãæãã次ã«å·¦ä¸ã® 4 ãã¹ãæããæå¾ã«å³ä¸ã® 4 ãã¹ãæããã¨ããæé ã«ãªã£ã¦ãã¾ãã
About the 15 Puzzle
ãã®ã¦ã§ããã¼ã¸ã®èª¬æã§ãå·¦ä¸ã® 4 ãã¹ãæããã¨ããã¾ã§ã¯ã解ããåé¡ã§ã解ããªãåé¡ã§ãã¾ã£ããåãæé ã§é²ãããã¾ããå·¦ä¸ã® 4 ãã¹ãæã£ãæç¹ã§ãå³ä¸ã® 4 ãã¹ã« 11, 12, 15, 空ç½ãæ®ã£ãç¶æ ã«ãªã£ã¦ãã¾ãããããã空ç½ãé©å½ã«åãã㦠15 ãæ£ããä½ç½®ã«æã£ã¦ããã°ãã©ããªç¤é¢ããã¹ã¿ã¼ããã¦ããå¿ ã以ä¸ã®ã©ã¡ããã«å°éã§ãã¾ãã
3 è¡ç®ã® 3, 4 åç®ã« 11, 12 ã®é çªã§å ¥ã£ã¦ããç¶æ (解ãã¦ãã)
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 |
3 è¡ç®ã® 3, 4 åç®ã« 12, 11 ã®é çªã§å ¥ã£ã¦ããç¶æ (解ãã¦ããªã)
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 12 | 11 |
13 | 14 | 15 |
ããã§å ã»ã©ã®è§£ãããã©ãããå¤å®ããæ°åãè¨ç®ãã¦ã¿ãã¨ã解ãã¦ããç¶æ ã¯å¶æ°ã®ç¤é¢ã解ãã¦ããªãç¶æ ã¯å¥æ°ã®ç¤é¢ã«ãªã£ã¦ãã¾ããæ´çããã¨
- ã©ããªç¤é¢ããã¹ã¿ã¼ããã¦ããä¸è¨ã®ã©ã¡ããçæ¹ã®ç¤é¢ã«å°éã§ãã
- å¶æ°ã®ç¤é¢ããå¥æ°ã®ç¤é¢ã«ã¯å°éã§ããªã
- 解ãã¦ããªãç¤é¢ã¯å¥æ°ã®ç¤é¢ã§ãã
ã¨ãªãããããã£ã¦ãã©ããªå¶æ°ã®ç¤é¢ããã¹ã¿ã¼ããã¦ãå¿ ãã解ãã¦ãããæ¹ã®ç¤é¢ã«å°éã§ãããã¨ãè¨ãã¾ãã
åé ã®ããã°ã©ã ã§ã¯ãã©ã³ãã ã«çæããç¤é¢ã解ãããã©ããã isSolvable é¢æ°ã§å¤å®ãã解ããªãåé¡ã ã£ãå ´åã«ã¯ flipLR é¢æ°ã§å·¦å³ãå転ãã¾ããç¤é¢ã®å·¦å³ãå転ãããã¨æ°åã®å¶å¥ãå ¥ãæ¿ããã®ã§ã解ããªãåé¡ã®å·¦å³ãå転ããããã®ã¯è§£ããåé¡ã«ãªãã¾ãã
*1:æåã«ç¤ºããã¦ã§ããã¼ã¸ã«ãããã®ãã¨ã証æããè«æã¸ã®åç §ãããã¾ãããè«æã®è¨¼æã¯ç§ã«ã¯å°ãé£ããããã¡ãã®ã¦ã§ããã¼ã¸ã®èª¬æã®æ¹ãç°¡åã«ç解ã§ãã¾ããã