dx+dy is the manhattan distance, optimal solution takes min(dx, dy) + |dx-dy| = max(dx,dy) steps to reach solution, because each dx,dy step can be paired to reduce the cost by 1 leaving |dx-dy| remaining cardinal direction steps to go
then construct strings using a zipper:
NNNNNNNN
EEEEE
becomes
NE NE NE NE NE N N N
which easily generalizes to all 4 possible combinations
My code isn't working for 3rd and 4th cases. It does not change its direction when x or y coord. becomes equal to that of the light. Please guide me!!
if( TX>LX)
{
if(TY<LY) cout<<"S"<<endl;
else if(TY>LY) cout<<"N"<<endl;
else cout<<"W"<<endl;
}
else if(TX<LX)
{
if(TY<LY) cout<<"S"<<endl;
else if(TY>LY) cout<<"N"<<endl;
else cout<<"E"<<endl;
}
else if(TX==LX)
{
if(TY<LY) cout<<"S"<<endl;
else cout<<"N"<<endl;
}
if(TX == TY && TY < LY)
System.out.println(āNā);
if(TX == TY && TY > LY)
System.out.println(āSā);
if(TX < TY && TY == LY)
System.out.println(āWā);
if(TX < TY && TY < LY)
System.out.println(āNWā);
if(TX < TY && TY > LY)
System.out.println(āSWā);
if(TX > TY && TY == LY)
System.out.println(āEā);
if(TX > TY && TY < LY)
System.out.println(āNEā);
if(TX > TY && TY > LY)
System.out.println(āSEā);
Actually no, its heuristic is quite hermetic, but all cases are mutually exclusive (on the other hand, they donāt cover all possibilities). That being said, thatās the only thing he got rightā¦ Itās amazing to see how such a simple problem as going from A to B can lead to such convoluted algorithm. Does the actual problem description has changed by specifying that Thor is sailing a ship and that the wind blows from (0, 0)? Because I canāt see any other reason to explain than the direction to take could actually depend of the north pole origin locationā¦
This topic is now 52 messages long, including 3-4 differents solutions to do it properly. Please try to read the whole topic properly and then if you still need to, ask a specific question that wasnāt asked before.
EDIT: ok so the topic was heavily modded (including by myself) so most of the previous solutions arenāt there. Still, read carefully, people are giving precious advises
My code is breaking on tests 3 and 4 and for some reason Thor keeps walking on the same direction (SW instead of W / NE instead of E)
Hereās the part of my code that should be running for test 3, not sharing the rest of the code so mods donāt delete this but you can probably have an idea of the rest of it
An issue that I personally ran into and it seems a lot of others here ran into is a misinterpretation of this line in the problem:
Line 1: 4 integers LX LY TX TY. (LX, LY) indicate the position of the light of power. (TX, TY) indicate Thorās starting position.
The key word there is āstartingā. Thorās position will NOT automatically update to his ācurrentā position. You must include his current position as part of your program. The way that I managed to do that was that I incremented and/or decremented TX and/or TY every time Thor moved one step. I hope that helps everyone.
Iāll be interested to see an actual implementation of what you are suggesting. I was too busy figuring out where the variables were getting their updates to pay real attention to otherās solutions.
It doesnāt help that the console makes it look like the TX and TY vars are being updated as Thor is moving. This ābugā has actually helped me in other challenges because if it is not in the per-lopp input, then you track it yourself.
Perhaps a slight update to the challenge description text will help others out.
Iām having some problem, canāt get 100%.
When I click on āRun all Testsā it works perfectly, I have all the " green Checked "
But when I click on āSubmitā, I get the first two tests wrong, and Thor has only 2 energy.
Anyone could give me a hands up ?
If I remember right you should only ever use the amount of energy equivalent to the Manhattan Distance which is also the shortest distance to the power of light. Since there are no obstacles this can easily be found blind. To be honest, I completely ignore the energy and just find the shortest path from the beginning.
Any hints on how to get the āupdatedā positions?
Iām trying to learn Python. Can get past the first 2 screens but canāt get it to change his mind once he starts moving.
Thanks!