+ 1

2d map code coach case4 test faild

I need help please I made the code as you see to found the first "P",the x represent to the row and the y represent to the column could any body tell me why case 4 always faild https://sololearn.com/compiler-playground/cvUpQaUkgF5S/?ref=app

16th Jun 2025, 6:48 PM
ELBARAA Gamal Galal
ELBARAA Gamal Galal - avatar
2 ответов
+ 5
ELBARAA Gamal Galal , the code is nearly perfect, except the line: ... Console.Write((yb-ya)+(xb-xa)); ... in some cases during the calculation of this line (depending on the test cases used), results can get *negative values* which causes incorrect results. we can avoid this by using Math.Abs(...) around of each of both expressions.
16th Jun 2025, 8:33 PM
Lothar
Lothar - avatar
+ 1
ELBARAA Gamal Galal Lothar is right, use Math.Abs() Console.Write(Math.Abs(yb-ya)+Math.Abs(xb-xa)); also, maybe this is a shorter way to do it: https://sololearn.com/compiler-playground/cZg7FxaHsZYX/?ref=app
17th Jun 2025, 4:00 AM
Bob_Li
Bob_Li - avatar