美文网首页
POJ 算法练习题 2-2

POJ 算法练习题 2-2

作者: 百毒 | 来源:发表于2013-11-05 14:56 被阅读22次
    
    #!usr/bin/perl
    
    use strict;
    use warnings;
    # use POSIX;
    use 5.010;
    
                    # my $my_test =("A"==0);
    # print $my_test;
    
    sub input_check {
    
        my ($x1, $x2, $y1, $y2);
        my @num_of_steps;
    
        # devide input to get coordinates;
        for (@_) {
            $_ = uc($_); #Capitalized the string;
            
            # a simple check if input is valid
            if(length($_) != 6) {
                $x1 = "0";
                $y1 = "0";
                $x2 = "0";
                $y2 = "0";
            } # This exception would be treated later.
            else {
                $x1 = substr $_, 0, 1;
                $y1 = substr $_, 1, 1;
                $x2 = substr $_, 3, 1;
                $y2 = substr $_, 4, 1;
            }
            
            # print join(" ",$x1,$x2,$y1,$y2);
            # check if points are in the same straight line
            my $same_stra_line;
            if (($x1 eq $x2) || ($y1 == $y2)){
                $same_stra_line = 1;
            }
            else {
                $same_stra_line = 0;
            }
            
            # check if points are in the same skew line
            my $same_skew_line;
            if ((ord($x1)-ord($x2)) == ($y1-$y2)){
                $same_skew_line = 1;
            }
            else {
                $same_skew_line = 0;
            }
            
            # check if points are in the same while/black zone
            my $same_zone;
            # $same_skew_line = (((ord($x1)%2) xor ($y1%2)) == ((ord($x2)%2) xor ($y2%2)));
            if (((ord($x1)%2) xor ($y1%2)) == ((ord($x2)%2) xor ($y2%2))){
                $same_zone = 1;
            }
            else {
                $same_zone = 0;
            }
            
            # calculate the max distance
            my $max_dist;
            $max_dist = (abs(ord($x1)-ord($x2)))>= (abs($y1-$y2))? abs(ord($x1)-ord($x2)):abs($y1-$y2);
            
            if($x1 eq "0"){
                push @num_of_steps, "invalid input";
            }
            else {
                push @num_of_steps, join(" ", $max_dist, ($same_stra_line || $same_skew_line)?1:2, $same_stra_line?1:2, ($same_zone?($same_skew_line?1:2):"Inf"));
                # push @num_of_steps, $max_dist." ".$same_stra_line." ".$same_skew_line." ".$same_zone;
            }
        }
        @num_of_steps;  
    }
    
    my @coor_input;
    my @coor_output;
    print "Please input a number of coordinates!\n";
    @coor_input = (<STDIN>);
    # @coor_input = ("A2 E4");
    @coor_output = input_check(@coor_input);
    print "@coor_output\n";
    

    相关文章

      网友评论

          本文标题:POJ 算法练习题 2-2

          本文链接:https://www.haomeiwen.com/subject/bwyqtttx.html