Ciao a tutti, ho bisogno di aiuto per risolvere il problema Chasing.
Ecco il mio codice:
#!/usr/bin/env python3
# NOTE: it is recommended to use this even if you don't understand the following code.
import sys
import math
# uncomment the two following lines if you want to read/write from files
# sys.stdin = open('input.txt')
# sys.stdout = open('output.txt', 'w')
Dx, Dy, Ds = map(int, input().strip().split())
Bx, By, Bs = map(int, input().strip().split())
dir = input().strip()
# INSERT YOUR CODE HERE
x = abs(Bx - Dx)
y = abs(By - Dy)
if Bs < Ds:
v = Ds - Bs
T = math.ceil((x+y)/v)
else:
if dir == 'R' and Dx > Bx or dir == 'L' and Dx < Bx or dir == 'U' and Dy > By or dir == 'D' and Dy < By:
v = Ds + Bs
T = math.ceil((x+y)/v)
else:
T = -1
print(T)
sys.stdout.close()
Ciao!
Se ti può essere utile questi sono 2 test case in cui il tuo codice sbaglia:
1 2 4
4 6 1
D
1 2 1
6 4 3
D
Ho usato questo tuo codice per eseguire:
import sys
import math
sys.stdin = open("input.txt")
sys.stdout = open("output.txt", "w")
Dx, Dy, Ds = map(int, input().strip().split())
Bx, By, Bs = map(int, input().strip().split())
dir = input().strip()
x = abs(Bx - Dx)
y = abs(By - Dy)
if Bs < Ds:
v = Ds - Bs
T = math.ceil((x+y)/v)
else:
if dir == "R" and Dx > Bx or dir == "L" and Dx < Bx or dir == "U" and Dy > By or dir == "D" and Dy < By:
v = Ds + Bs
T = math.ceil((x+y)/v)
else:
T = -1
print(T)
sys.stdout.close()
Tenendo traccia dei due esempi dati (di cui soluzione è 2 e -1?) ho modificato il codice così:
Ma mi esce comunque sbagliato un unico caso che è il testcase 33 (subtask 5 e 6).
Questo mi fa prendere solo 50/100.
#!/usr/bin/env python3
# NOTE: it is recommended to use this even if you don't understand the following code.
import sys
import math
# uncomment the two following lines if you want to read/write from files
# sys.stdin = open('input.txt')
# sys.stdout = open('output.txt', 'w')
Dx, Dy, Ds = map(int, input().strip().split())
Bx, By, Bs = map(int, input().strip().split())
dir = input().strip()
# INSERT YOUR CODE HERE
x = abs(Bx - Dx)
y = abs(By - Dy)
if dir == 'R' or dir == 'L':
t = math.ceil(y / Ds)
xB = Bx + Bs*t
else:
t = math.ceil(x / Ds)
yB = By + Bs * t
if dir == 'R' and xB <= Dx and Dx >= Bx or dir == 'L' and Dx <= Bx and xB >= Dx or dir == 'U' and yB <= Dy and Dy >= By or dir == 'D' and yB >= Dy and Dy <= By:
v = Ds + Bs
else:
v = Ds - Bs
if (v > 0):
T = math.ceil((x+y)/v)
else:
T = -1
print(T)
sys.stdout.close()
Anch’io sbagliavo solo il TC 33 prima di risolvere correttamente questo:
10 5 5
8 15 2
R
soluzione corretta 3 (2,666 periodico)
ma prima il mio codice dava 2.
Provalo.
Nell’esempio che ho proposto intanto che il cane si porta alla stessa altezza della palla, ci vogliono 2 unità di tempo, questa è arrivata da 8 a 12. Adesso è più a destra del cane e questo adesso la deve inseguire. (velocità relativa 5-2=3 con un Deltax=12-10=2. Tempo aggiuntivo 2/3.
Grazie mille, sono riuscito a risolverlo,
ero arrivato alla soluzione, ma avevo arrotondato anche in questa per del codice (avevo fatto math.ceil anche per t) e poi ho sommato :
if dir == 'R' or dir == 'L':
t = abs(Dy-By)/Ds
if dir == 'R':
Bx = Bx + Bs*t
else:
Bx = Bx - Bs*t
else:
t = abs(Dx-Bx)/Ds
if dir == 'U':
By = By + Bs*t
else:
By = By - Bs*t
ciao, scusate se crivo sott questo post, ma volevo chiedere come creare un nuovo argomento, pk non ho trovto il bottone per crearne uno nuovo.
Grazie mille se riuscite a scrivermi.