fork download
  1. #!/usr/bin/perl
  2. @MOVE = (
  3. [0,0,1],
  4. [0,1,0],
  5. [1,0,0],
  6. [0,1,1],
  7. [1,0,1],
  8. [1,1,0],
  9. [1,1,1]
  10. );
  11.  
  12. ################################
  13. # 0 = 未點燃, 1 = 點燃, 2 = 熄滅
  14.  
  15. @FAIL_STATE = (
  16. '002','020','200','022','202','220'
  17. );
  18.  
  19.  
  20. &dfs(0,0,0);
  21.  
  22. print $cnt;
  23.  
  24. sub dfs{
  25.  
  26. my ($i,$j,$k) = @_;
  27.  
  28. my $curState = "$i$j$k";
  29.  
  30. ############################
  31. # final state (count answer)
  32.  
  33. if($curState eq '222'){
  34. $cnt++;
  35. }
  36.  
  37. #################################
  38. # fail state (return immediately)
  39.  
  40. return if($curState ~~ @FAIL_STATE);
  41.  
  42.  
  43. #######################
  44. # try all possible move
  45.  
  46. for my $move (@MOVE){
  47.  
  48. my $x = $i + $move->[0];
  49. my $y = $j + $move->[1];
  50. my $z = $k + $move->[2];
  51.  
  52. #########################
  53. # invalid move (try next)
  54.  
  55. next if( 3 ~~ [$x,$y,$z]);
  56.  
  57. ##################
  58. # 以下處理相同狀況:
  59. #
  60. # 例如以下兩種狀況視為相同::(暫時忽略第三根蠟蠋)
  61. # 1. 蠟蠋1點燃→蠟蠋1熄滅時蠟蠋2立刻點燃→蠟蠋2熄滅
  62. # 2. 蠟蠋1點燃→蠟蠋1熄滅→一會兒後蠟蠋2點燃→蠟蠋2熄滅
  63.  
  64.  
  65. my $on = 0;
  66. my $off = 0;
  67.  
  68. $on = 1 if($x == 1 and $i == 0);
  69. $on = 1 if($y == 1 and $j == 0);
  70. $on = 1 if($z == 1 and $k == 0);
  71.  
  72. $off = 1 if($x == 2 and $i == 1);
  73. $off = 1 if($y == 2 and $j == 1);
  74. $off = 1 if($z == 2 and $k == 1);
  75.  
  76. next if($on == 1 and $off == 1);
  77.  
  78. ###############
  79. # go next state
  80.  
  81. &dfs($x,$y,$z);
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
Success #stdin #stdout #stderr 0s 4396KB
stdin
Standard input is empty
stdout
223
stderr
Smartmatch is experimental at prog.pl line 41.
Smartmatch is experimental at prog.pl line 56.