Problem:

In a chess board, only 1 King is left. Given his current position and his destination position, find number of minimum moves, in which king can reach his destinations.

Valid moves are same as that of King in Chess Game.

Valid moves are same as that of King in Chess Game.

**Input Format:**

T(number of test cases)

Next T lines having initial position and destination position like a1 d7

**Output Format:**

T lines having minimum number of moves

**Constraints:**

None

Sample Input

5

a1 h8

e4 e4

c2 d5

e7 d2

h2 b1

a1 h8

e4 e4

c2 d5

e7 d2

h2 b1

Sample Output:

7

0

3

5

6

7

0

3

5

6

Explanations:

for c2 d5, moves can be c2-d3, d3-d4, d4-d5. So 3 moves minimum

for c2 d5, moves can be c2-d3, d3-d4, d4-d5. So 3 moves minimum

Solution:

for($i=0;$i<$t;$i++)

{

chomp($line=<STDIN>);

@arr=split(" ",$line);

$arr[0]=~ /(\w)(\d*)/;

$c1=ord($1);

$r1=$2;

$arr[1]=~ /(\w)(\d*)/;

$c2=ord($1);

$r2=$2;

if($c1>$c2)

{

$d1=$c1-$c2;

}

else

{

$d1=$c2-$c1;

}

if($r1>$r2)

{

$d2=$r1-$r2;

}

else

{

$d2=$r2-$r1;

}

if($d1>$d2)

{

push(@out,$d1);

}

else

{

push(@out,$d2);

}

}

foreach(@out)

{

print "$_\n";

}

Tips:

Maximum (diff of rows, diff of columns)

## No comments:

## Post a Comment