-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotRescue.pl
60 lines (52 loc) · 1.13 KB
/
BotRescue.pl
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
#!/usr/bin/perl
use POSIX;
$n = <STDIN>;
chomp($n);
for ($in=0; $in<$n; $in++) {
$line = <STDIN>;
chomp($line);
$line =~ s/([-])([^ ])/$1 $2/gi;
$line =~ s/([^ ])([-])/$1 $2/gi;
$line =~ s/([m])([^ ])/$1 $2/gi;
$line =~ s/([^ ])([m])/$1 $2/gi;
$line =~ s/([p])([^ ])/$1 $2/gi;
$line =~ s/([^ ])([p])/$1 $2/gi;
foreach ($line) { s/^\s+//; s/\s+$//; s/\s+/ /g;}
@gridA = split(/ /, $line);
$gridA = [@gridA];
push (@grid, $gridA);
}
displayPathtoPrincess($n, @grid);
# Head ends here
sub displayPathtoPrincess {
my($n,$grid) = @_;
$mx = floor($n/2);
$my = floor($n/2);
for ($i=0; $i<$n; $i++) {
for ($j=0; $j<$n; $j++) {
if ($grid[$i][$j] eq "p") {
$px = $i;
$py = $j;
}
}
}
$dirx = $px - $mx;
if ($dirx > 0 ) {
do { print "DOWN\n"; $dirx = $dirx -1;
} until ($dirx ==0);
}
elsif ($dirx < 0 ) {
do { print "UP\n"; $dirx = $dirx +1;
} until ($dirx ==0);
}
$diry = $py - $my;
if ($diry > 0 ) {
do { print "RIGHT\n"; $diry = $diry -1;
} until ($diry ==0);
}
elsif ($diry < 0 ) {
do { print "LEFT\n"; $diry = $diry +1;
} until ($diry ==0);
}
}
# Tail starts here