fork download
  1. #!/usr/bin/perl
  2.  
  3. sub f{($r,$m)=@_;$h=@m=@$m;for$s(0..(($w=$#{$m[0]})<--$h?$w:$h)/2-.5){@_=(@{$m[$s]}[@x=$s..($x=$w-$s)],(map$m[$_][$x],@y=1+$s..($y=$h-$s)-1),reverse(@{$m[$y]}[@x]),(map$m[$h-$_][$s],@y));push@_,shift
  4. for 1..$r;@{$m[$s]}[@x]=map shift,@x;$m[$_][$x]=shift for@y;@{$m[$y]}[@x]=reverse map shift,@x;$m[$h-$_][$s]=shift for@y}@$m=@m}
  5.  
  6. # Testing.
  7.  
  8. use Test::More;
  9.  
  10. sub cmp_matrix {
  11. return unless @{$_[0]} == @{$_[1]};
  12. for my $i (0 .. @{$_[0]} - 1) {
  13. return unless @{$_[0][$i]} == @{$_[1][$i]};
  14. for my $j (0 .. @{$_[0][$i]} - 1) {
  15. return unless $_[0][$i][$j] == $_[1][$i][$j];
  16. }
  17. }
  18. return 1;
  19. }
  20.  
  21. $t = [[1, 2, 3, 4 ],
  22. [5, 6, 7, 8 ],
  23. [9, 10, 11, 12],
  24. [13, 14, 15, 16]];
  25. f(2, $t);
  26. ok cmp_matrix $t,
  27. [[3, 4, 8, 12],
  28. [2, 11, 10, 16],
  29. [1, 7, 6, 15],
  30. [5, 9, 13, 14]];
  31. $t = [[1, 2],
  32. [3, 4],
  33. [5, 6]];
  34. f(2, $t);
  35. ok cmp_matrix $t,
  36. [[4, 6],
  37. [2, 5],
  38. [1, 3]];
  39. $t = [[1, 1],
  40. [2, 2],
  41. [3, 3]];
  42. f(1, $t);
  43. ok cmp_matrix $t,
  44. [[1, 2],
  45. [1, 3],
  46. [2, 3]];
  47. done_testing;
  48.  
Success #stdin #stdout 0s 20304KB
stdin
Standard input is empty
stdout
ok 1
ok 2
ok 3
1..3