Exercise 2-2 棋盘上的距离

注:全部习题来自POJ 项目,原题目要求使用 C/C++ 实现。

问题描述

国际象棋的棋盘是黑白相间的 8 * 8 的方格,棋子放在格子中间。如下图所示:

王、后、车、象的走子规则如下:

  • 王:横、直、斜都可以走,但每步限走一格。
  • 后:横、直、斜都可以走,每步格数不受限制。
  • 车:横、竖均可以走,不能斜走,格数不限。
  • 象:只能斜走,格数不限。

写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。

输入数据{{3}}

第一行是测试数据的组数t(0 <= t <= 20)。以下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用”字母-数字”的形式表示,字母从”a”到”h”,数字从”1″到”8″。

输出要求

对输入的每组测试数据,输出王、后、车、象所需的最少步数。如果无法到达,就输出”Inf”.

输入样例

2

a1 c3

f5 f8

输出样例

2 1 2 1

3 1 1 Inf

代码1

            #! usr/bin/perl
            use strict;
            use warnings;
            use 5.010;

            # my $my_test =("A"==0);
            # print $my_test;

            sub input_check {

                my ($x1, $x2, $sp, $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, $y1, $x2, $y2) = (0,0,0,0);
                    } # This exception would be treated later.
                    else {
                        ($x1, $y1, $sp, $x2, $y2) = split //, $_;
                    }

                    my $same_point; {{4}}
                    if (($x1 eq $x2) && ($y1 == $y2)){
                        $same_point = 1;
                    }
                    else {
                        $same_point = 0;
                    }

                    # 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\n";
                    }
                    elsif($same_point) {
                        push @num_of_steps, join(" ",0,0,0,0)."\n";
                    }
                    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")."\n");
                        # 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>);
            print &input_check(@coor_input);
            # print "@coor_output\n";

[[3]] Again,这里略去了输入测试数据组数的要求。[[3]]

[[4]] 这里有一个奇怪的问题。执行这两行会得到一个警告和输出 1。在使用 == 和 eq 进行比较时,应特别谨慎。[[4]]

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据