fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /*
  8. プログラミングのお題スレ Part14
  9. //mevius.5ch.net/test/read.cgi/tech/1558168409/695
  10.  
  11. 695 名前:デフォルトの名無しさん[sage] 投稿日:2019/07/03(水) 23:21:16.00 ID:S/aBv8fE
  12. お題
  13. 直線状の(配列を使った)ライフゲームがある。ルールは、
  14.  
  15. 1. 両隣が生きていれば、暑苦しいので死ぬ
  16. 2. 両隣が死んでいれば、寂しいので死ぬ
  17. 3. 両隣の内、片方だけが生きていれば、生きる
  18.  
  19. 4. 両端の2つについては、隣が生きていれば生きるし、隣が死んでいれば死ぬ
  20. 5. すべてのマスの状態の変更は、同時にすること
  21.  
  22. 下の初期値(1 ターン目)から初めて、状態が変わらなくなるのは、何ターン目か?
  23. nターン目と、( n + 1 )ターン目が同じなら、nターン目を答える
  24.  
  25. ただし、漏れは検証していないので、100ターンを超えたら、終了してくださいw
  26. * は生、. は死を表す
  27.  
  28. .*...**.*.***..
  29. */
  30. class Ideone
  31. {
  32. public static void main(String[] args)
  33. {
  34. lifegame(".*...**.*.***..", false);
  35. lifegame(".*...**.*.***..", true);
  36. lifegame("*...", true);
  37. }
  38.  
  39. static void lifegame(String data, boolean birth)
  40. {
  41. System.out.printf("'%s' (%s)%n", data, birth ? "誕生あり" : "誕生なし");
  42. HashMap<String, Integer> map = new HashMap<>();
  43. for (int i = 1; i <= 100; i++)
  44. {
  45. System.out.printf("% 3d: %s%n", i, data);
  46. if (map.containsKey(data))
  47. {
  48. int turn = map.get(data);
  49. if (turn == i - 1)
  50. {
  51. System.out.printf("%dターン目に停止%n", turn);
  52. } else
  53. {
  54. System.out.printf("%dターン目にループ突入(→%d)%n", i, turn);
  55. }
  56. break;
  57. }
  58.  
  59. map.put(data, i);
  60. data = next(data, birth);
  61. }
  62. System.out.println();
  63. }
  64.  
  65. static String next(String data, boolean birth)
  66. {
  67. int l = data.length();
  68. char[] next = new char[l];
  69. next[0] = (data.charAt(0) == '*' || birth) ? data.charAt(1) : '.';
  70. next[l - 1] = (data.charAt(l - 1) == '*' || birth) ? data.charAt(l - 2) : '.';
  71. for (int i = 1; i < l - 1; i++)
  72. {
  73. next[i] = (data.charAt(i) == '*' || birth)
  74. && (data.charAt(i - 1) + data.charAt(i + 1) == '*' + '.')
  75. ? '*'
  76. : '.';
  77. }
  78. return new String(next);
  79. }
  80. }
Success #stdin #stdout 0.06s 2184192KB
stdin
Standard input is empty
stdout
'.*...**.*.***..' (誕生なし)
  1: .*...**.*.***..
  2: .....**...*.*..
  3: .....**........
  4: .....**........
3ターン目に停止

'.*...**.*.***..' (誕生あり)
  1: .*...**.*.***..
  2: *.*.***...*.**.
  3: ....*.**.*..***
  4: ...*..**..***.*
  5: ..*.*******.*..
  6: .*..*.....*..*.
  7: *.**.*...*.**.*
  8: ..**..*.*..**..
  9: .*****...*****.
 10: **...**.**...**
 11: ***.***.***.***
 12: *.*.*.*.*.*.*.*
 13: ...............
 14: ...............
13ターン目に停止

'*...' (誕生あり)
  1: *...
  2: .*..
  3: *.*.
  4: ...*
  5: ..*.
  6: .*.*
  7: *...
7ターン目にループ突入(→1)