Hey there,
My algorithm works fine in most cases but isn’t fast enough for the last 2 tests where you have a very limited number of turns to get the right solution. My program uses a minX, maxX and minY, maxY to use as a “search area”, then batman jumps in the middle of that search area and updates those values for the next jump. Works fine but isn’t efficient enough. How can I improve it without looking at the solutions ?
Thx
@ktserkov @Red_Karibou When you calculate your next move, also consider the fact that the bomb cannot be on the floor you are currently on. Try solving the case offline by hand to see how the choices are reduced every turn.
The test cases are designed so that you will always be able to reach the bomb (just) in time if you apply the optimum approach.
‘Correct cutting’ works before submit, but fails after submit. It’s not a matter of something being hardcoded either, because when I click on it in the post submission screen batman fails to find the bomb. Has anybody dealt with this?
Test cases (“before submit”) and validators (“after submit”) are generally different, hence you would have to watch the replay of the validator to debug your code. If you need further help, you may post the link to the replay here.
Hello, I have a problem: the method Math.ceil() does not return the right value. Math.ceil() and Math.floor() are returning the same value. I don’t think I’ve changed anything in the parameters since I’ve started this.
If you apply Math.ceil and Math.floor to an integer, then they should return the same value.
Oh yeah, right! Thanks for the help!
I just made it with Java but discovered Map.ofEntries wasn’t defined, so I had to add the elements one by one with put. Versions of JAva are Oracle JDK 1.8.0 and OpenJDK 11.0.2, so ofEntries should have been accepted.
#include
#include
#include
#include
using namespace std;
/**
- Auto-generated code below aims at helping you parse
- the standard input according to the problem statement.
**/
int main()
{
int w; // width of the building.
int h; // height of the building.
cin >> w >> h; cin.ignore();
int n; // maximum number of turns before game over.
cin >> n; cin.ignore();
int x0;
int y0;
cin >> x0 >> y0; cin.ignore();
int x=0;
int y=0;
int x1=w-1;
int y1=h-1;
// game loop
while (n>=0) {
string bomb_dir; // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
cin >> bomb_dir; cin.ignore();
if(bomb_dir.find('L')!=string::npos){
x1=x-1;
}
else if(bomb_dir.find('R')!=string::npos){
x0=x+1;
}
if(bomb_dir.find('U') != string::npos){
y1=y-1;
}
else if(bomb_dir.find('D')!=string::npos){
y0=y+1;
}
x=x1+ (x1-x0) /2;
y= y1+ (y1-y0) /2;
n--;
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
// the location of the next window Batman should jump to.
cout << x <<' '<<y<< endl;
}
}
Can anyone tell what’s the problem with my code?
The console can. Here is what I see when I run Test 1 with your code:
Game information:
Locate the bombs, save the hostages! Batman is on window (2,3) The bombs are located below and to the right of Batman
Standard Output Stream:
4 10
Game information:
Failure: invalid input. Expected ‘0 <= x < 4’ but found ‘x = 4’
Also, you may format your code properly on this forum by using the </> button in the formatting toolbar.
int main()
{
int w; // width of the building.
int h; // height of the building.
cin >> w >> h; cin.ignore();
int n; // maximum number of turns before game over.
cin >> n; cin.ignore();
int x0;
int y0;
cin >> x0 >> y0; cin.ignore();
int increment_w = w-(x0+1); //reach the max possible coord first
int increment_h = h-(y0+1);
int x = x0;
int y = y0;
// game loop
while (1) {
string bomb_dir; // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
cin >> bomb_dir; cin.ignore();
// Write an action using cout. DON'T FORGET THE "<< endl"
if (bomb_dir == "U") {
y -= increment_h;
} else if (bomb_dir == "D") {
y += increment_h;
} else if (bomb_dir == "L") {
x -= increment_w;
} else if (bomb_dir == "R") {
x += increment_w;
} else if (bomb_dir == "UL") {
x -= increment_w;
y -= increment_h;
} else if (bomb_dir == "UR") {
x += increment_w;
y -= increment_h;
} else if (bomb_dir == "DL") {
x -= increment_w;
y += increment_h;
} else if (bomb_dir == "DR") {
x += increment_w;
y += increment_h;
}
// check if x has changed from its previous value x0, if so /2
if (x != x0) {
increment_w *= 0.5;
}
if (y != y0) {
increment_h *= 0.5;
}
// Update x0 and y0 with the new values of x and y
x0 = x;
y0 = y;
// To debug: cerr << "Debug messages..." << endl;
// the location of the next window Batman should jump to.
cout << x << " " << y << endl;
}
}
I applied bisection method, this code passed test #1, #3, #4, #5.
For whatever reason, the batman just get stuck in #2.
I don’t understand #6 Evasive.
For #7 test, I was very close (~30 units more to pass) but did not find a way to reach it in the end.
Would be nice if anyone can shed light on the solutions. It was fun but I need a break now
Take test #2 for example:
https://www.codingame.com/replay/706441931
Batman starts at y = 29. The bomb is above. Your batman just moves 3 units up in the first move. If you are applying the bisection method, what should be the expected move instead? What may be the issue in the calculation of the increment?
I haven’t taken a look at the others, but you can probably watch the replays, ignore the programming aspect for a moment, focus on what the expected logic is, and think about how the actual result differs from it.
Help (using JS) i don’t understand why all my test are good except the last one can you help me ? here is my code :
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
var inputs = readline().split(' ');
let W = parseInt(inputs[0]); // width of the building.
let H = parseInt(inputs[1]); // height of the building.
const N = parseInt(readline()); // maximum number of turns before game over.
var inputs = readline().split(' ');
let X0 = parseInt(inputs[0]);
let Y0 = parseInt(inputs[1]);
// game loop
while (true) {
let bombDir = readline(); // the bomDir of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
// initialise the limit
let Wmin = Hmin = 0
let Wmax = W - 1
let Hmax = H - 1
// actualise the limit and the position of batman when you get a direction from bombDir
if (bombDir.includes('U')) {
console.error('y has to decrement')
H = Y0--
Y0 = Y0 + Math.ceil((Hmin - Y0) / 2)
} else if (bombDir.includes('D')) {
console.error('y has to increment')
Hmin = Y0++
Y0 = Y0 + Math.ceil((Hmax - Y0) / 2)
}
if (bombDir.includes('L')) {
console.error('x has to decrement')
W = X0--
X0 = X0 + Math.ceil((Wmin - X0) / 2)
} else if (bombDir.includes('R')) {
console.error('x has to increment')
Wmin = X0++
X0 = X0 + Math.ceil((Wmax - X0) / 2)
}
// debug
console.error()
// response
console.log(X0, Y0)
}
Try watching and studying a replay carefully. Turn “debug” on for a clearer picture.
For example in this replay:
https://www.codingame.com/replay/706807877
Your Batman was at one point close to the target, but then he moved far away from it. You may want to investigate what goes wrong in the code to cause such a bebaviour.
that’s what i don’t understand any idea why he goes like that ?
I’m not a JS practicionner, but as a tip, i suggest you output in your console.error the values of X and Y you calculate each turn - Hmin/Wmin/X0/Y0, and check what goes wrong.
As i understand (again, i am not a JS practicionner), you are modifying W and H (building dimensions) in your game loop (H = Y0-- / W = X0–) when in the else of the same conditions you modify Wmin and Hmin, why that ? The building remains the same through the game. Maybe outputting the different values you calculate each turn could enlight you on the mistakes in the code.
I’m not sure to understand what you are doing there
Hey, can anyone tell me if this puzzle can be finished in PHP? I don’t want help, I just want to know if I can end it or not with this language
Yes, in fact the puzzle can be finished in all the languages provided on CodinGame.
I had the same problem in go, had to math.Ceil the result in float and convert it back to int so solve it