#!/bin/bash
# McKelian 1 mars 2014
# Tentative d'utilisation bash
signe()
{
if [ "$1" -lt "0" ]
then
retour=-1
else
if [ "$1" -gt "0" ]
then
retour=1
else
retour=0
fi
fi
#echo "Entrée : $1 Sortie : $retour " >&2
echo $retour
}
barycentre()
{
bx=0
by=0
for i in `seq 1 $n`
do
bx=$(($bx+${gx[$i]}))
by=$(($by+${gy[$i]}))
done
bx=$(($bx/$n))
by=$(($by/$n))
}
TrouveDirection()
{
#o = origine
#d = destination
ox=$1
oy=$2
dx=$3
dy=$4
signeX=`signe $[ $dx - $ox ]`
signeY=`signe $[ $dy - $oy ]`
#echo "ox:$ox oy:$oy dx:$dx dy:$dy signeX:$signeX signeY:$signeY " >&2
for i in `seq 0 8`
do
mx=${MouveX[$i]}
my=${MouveY[$i]}
if [ "$mx" -eq "$signeX" -a "$my" -eq "$signeY" ]
then
break
fi
done
echo $i
}
NombreGeantAPorteeMarteau()
{
echo "NombreGeantAPorteeMarteau" >&2
x=$1
y=$2
nombre=0
for i in `seq 1 $n`
do
diffx=$(( ${gx[$i]} - $x ))
diffy=$(( ${gy[$i]} - $y ))
if [ ${diffx#-} -lt 5 -a ${diffy#-} -lt 5 ]
then
(( nombre += 1 ))
fi
done
echo $nombre
}
tThorAPorteeGeant()
{
#echo "tThorAPorteeGeant" >&2
x=$1
y=$2
for i in `seq 1 $n`
do
diffx=$(( ${gx[$i]} - $x ))
diffy=$(( ${gy[$i]} - $y ))
if [ ${diffx#-} -lt 2 -a ${diffy#-} -lt 2 ]
then
#echo " Géant $i proche" >&2
echo 0
exit
fi
done
echo 1
}
Evitement()
{
echo "Evitement (ActionThor:$ActionThor)" >&2
x=$1
y=$2
sortie=8
nombre=0
max=0
min=30000
for ((i=0; i < 9; i++))
do
echo $i >&2
px=$(( $x + ${MouveX[$i]} ))
py=$(( $y + ${MouveY[$i]} ))
distB=$(( (px - bx) * (px - bx) + (py - by) * (py - by) ))
aportee=`tThorAPorteeGeant $px $py`
if [ $px -lt 40 -a $px -ge 0 -a $py -lt 18 -a $py -ge 0 -a "$aportee" -eq "1" ]
then
(( nombre += 1 ))
dist=1
tOk=0
while [ $tOk -eq 0 ]
do
px=$(( x + ${MouveX[$i]} * $dist ))
py=$(( y + ${MouveY[$i]} * $dist ))
#echo "px:$px py:$py " >&2
if [ $px -ge 39 -o $px -le 0 -o $py -ge 17 -o $py -le 0 ]
then
tOk=1
if [ $dist -gt $max ]
then
max=$dist
sortie=$i
fi
# mvt diagonale est un meilleur choix
if [ $dist -ge $max -a $(( sortie % 2)) -lt $(( i % 2)) ]
then
max=$dist
sortie=$i
fi
# se rapprocher du barycentre est encore mieux
if [ $dist -ge $max -a $distB -lt $min ]
then
max=$dist
min=$distB
sortie=$i
fi
echo "dist:$dist max:$max sortie:$sortie distB:$distB min:$min" >&2
else
(( dist += 1 ))
fi
done
fi
done
echo "sortie:$sortie" >&2
echo $sortie
}
lecture()
{
read ligne
espace=`expr index "$ligne" ' '`
longueur=${#ligne}
sortie1=${ligne:0:$espace}
sortie2=${ligne:$espace:3}
sortie=( $sortie1 $sortie2 )
}
# Initialisation variables
Actions=( N NE E SE S SW W NW WAIT STRIKE )
MouveX=( 0 1 1 1 0 -1 -1 -1 0 0 )
MouveY=( -1 -1 0 1 1 1 0 -1 0 0 )
PremierEvitement=0
#for i in `seq 0 ${#Actions[*]}`
#do
# echo "$i Actions: ${Actions[$i]} X=${MouveX[$i]} Y=${MouveY[$i]}" >&2
#done
# Read init information from standard input, if any
lecture
tx=${sortie[0]}
ty=${sortie[1]}
echo "tx:$tx ty:$ty" >&2
#Boucle principale
while true;
do
# Read information from standard input
lecture
h=${sortie[0]}
n=${sortie[1]}
echo "n:$n h:$h" >&2
# lecture des positions des géants
for i in `seq 1 $n`
do
lecture
gx[$i]=${sortie[0]}
gy[$i]=${sortie[1]}
#echo "Geant $i ${gx[$i]},${gy[$i]}" >&2
done
# Compute logic here
barycentre
echo "bx:$bx by:$by" >&2
ActionThor=`TrouveDirection $tx $ty $bx $by`
#Declenchement du marteau
nombre=`NombreGeantAPorteeMarteau $tx $ty`
echo "nombre:$nombre" >&2
if [ "$nombre" -eq "$n" ]
then
ActionThor=9
else
tTAPG=`tThorAPorteeGeant $tx $ty`
if [ "$tTAPG" = "0" ]
then
#existe-t-il une position d'évitement
ActionThor=`Evitement $tx $ty`
#Non
if [ "$ActionThor" -eq "8" ]
then
ActionThor=9
fi
fi
fi
#Mise à jour du deplacement de Thor
tx=$(($tx + ${MouveX[$ActionThor]}))
ty=$(($ty + ${MouveY[$ActionThor]}))
# Write action to standard output
echo "${Actions[$ActionThor]}"
done