fork download
  1. use integer;
  2. while(<>){
  3.  
  4. ($n,$m)=split/ /;
  5. for $i(1..$n){
  6. $_=<>;
  7. @_=/\d/g;
  8. $j=0;
  9. $b[++$j][$i]=$_ for @_;
  10. }
  11.  
  12. $_=<>;
  13.  
  14. @a=(1, 0, -1, 0);
  15. $d=0;
  16. $x = $y = 0;
  17. $ans=0;
  18.  
  19. while (/\w/g){
  20.  
  21. $d += $inc = $& cmp M;
  22. $inc and next;
  23. # print "up/left:$b[$x][$y]|up/rigth:$b[$x+1][$y]|down/left:$b[$x][$y+1]|down/right:$b[$x+1][$y+1]\n";
  24. print "$b[$x][$y]|$b[$x+1][$y]|$b[$x][$y+1]|$b[$x+1][$y+1]\n";
  25.  
  26. if ($a[ $d % 4 ] == 1){
  27. $ans += $b[$x+1][$y+1] + $b[$x+1][$y];
  28. $b[$x+1][$y+1] /= 2;
  29. $b[$x+1][$y] /= 2;
  30. }
  31. if ($a[ $d % 4 ] == -1){
  32. $ans += $b[$x][$y+1] + $b[$x][$y];
  33. $b[$x][$y+1] /= 2;
  34. $b[$x][$y] /= 2;
  35. }
  36. if ($a[ ($d-1) % 4 ] == -1){
  37. $ans += $b[$x+1][$y] + $b[$x][$y];
  38. $b[$x+1][$y] /= 2;
  39. $b[$x][$y] /= 2;
  40. }
  41. if ($a[ ($d-1) % 4 ] == 1){
  42. $ans += $b[$x+1][$y+1] + $b[$x][$y+1];
  43. $b[$x+1][$y+1] /= 2;
  44. $b[$x][$y+1] /= 2;
  45. }
  46.  
  47. $x += $a[ $d % 4 ];
  48. $y += $a[ ($d-1) % 4 ];
  49.  
  50.  
  51. }
  52. print "ans: $ans\n";
  53.  
  54. }
Success #stdin #stdout 0s 3688KB
stdin
3 6
000123
456456
789789
RRLLRRRRMMMMMMRMMMRM
stdout
|||0
0||0|0
0||0|0
0||0|1
0||0|2
0||1|3
0||1|
0|0|6|
3|0|9|
4|0||
ans: 26