Thread Matrix durchlaufen (5 answers)
Opened by hugenyn at 2010-12-18 01:04

topeg
 2010-12-18 07:17
#143681 #143681
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl
use strict;
use warnings;

# spielfeld
my @aa=(
  [qw /28 22 15  6 33  1/],
  [qw / 7 10 32 27 11 14/],
  [qw /23 18  5 31  9 30/],
  [qw / 4 36 29 20 16 24/],
  [qw /13 25 17  3 35 19/],
  [qw /34  2 26 12 21  8/],
);

#*********************************** Programm ***************

sub stepp_trou
{
  my $matrix=shift;
  my $sx=shift;
  my $sy=shift;
  for my $s (0..2)
  {
    for my $t (0..2)
    {
      my $m=$t;
      my $k=$s;

      ($k,$m)=($t,$s) if($sx);
      for (0..3)
      {
        print $matrix->[$k]->[$m]."\t";
        $k+=$sy;
        $m+=$sx;
      }
      print "\n";
    }
    for my $t (3..5)
    {
      my $m=$t;
      my $k=$s;
      ($k,$m)=($t,$s) if($sx);
      for (0..3)
      {
        print $matrix->[$k]->[$m]."\t";
        if($sx && $sy)
        { $k-=$sy; }
        else
        { $k+=$sy }
        $m+=$sx;
      }
      print "\n";
    }
    print "\n";
  }
}

print "*** Die Diagonalen ***\n";
stepp_trou(\@aa,1,1);
print "*** Die Senkrechten ***\n";
stepp_trou(\@aa,0,1);
print "*** Die Waagerechten ***\n";
stepp_trou(\@aa,1,0);


Allgemeingültiger formuliert. Damit kann man mit beliebigen Schrittweiten in beliebigen Matrizen mit beliebigen Längen holen lassen.
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
use strict;
use warnings;

# spielfeld
my @aa=(
  [qw /28 22 15  6 33  1/],
  [qw / 7 10 32 27 11 14/],
  [qw /23 18  5 31  9 30/],
  [qw / 4 36 29 20 16 24/],
  [qw /13 25 17  3 35 19/],
  [qw /34  2 26 12 21  8/],
);

#*********************************** Programm ***************

sub stepp_trou
{
  my $matrix=shift;
  my $sx=shift;
  my $sy=shift;
  my $size=shift;
  for my $y (0..$#$matrix)
  {
    for my $x (0..$#{$matrix->[$y]})
    {
      my @list=();
      my ($rx,$ry)=($x,$y);

      # Reihe suchen
      while($ry<@$matrix && $rx<@{$matrix->[$y]})
      {
        push(@list,$matrix->[$ry]->[$rx]);
        $rx+=$sx;
        $ry+=$sy;
        last if(@list==$size);
      }

      # Reihe gefunden
      if(@list==$size)
      {
        print join("\t",@list)."\n";
        next();
      }

      # Nur bei Diagonalen auch rückwärts suchen
      next unless($sx && $sy);

      @list=();
      ($rx,$ry)=($x,$y);

      # Reihe suchen
      while($ry>=0 && $rx<@{$matrix->[$y]})
      {
        push(@list,$matrix->[$ry]->[$rx]);
        $rx+=$sx;
        $ry-=$sy;
        last if(@list==$size);
      }

      # Reihe gefunden
      print join("\t",@list)."\n" if(@list==$size);
    }
    #print "\n";
  }
}

print "\n*** Die Diagonalen ***\n";
stepp_trou(\@aa,1,1,4);
print "\n*** Die Senkrechten ***\n";
stepp_trou(\@aa,0,1,4);
print "\n*** Die Waagerechten ***\n";
stepp_trou(\@aa,1,0,4);


Für mich sieht das ganze nach Hausaufgaben aus. ;-)

View full thread Matrix durchlaufen