### wdem's blog

By wdem, history, 5 weeks ago,

Could someone help a new c++ learner? — I first wrote it in python3 and got it accepted, but now i'm trying to reproduce the same results in c++ to improve my non existing coding skills and got wrong answers, which i'm unable to reproduce locally where the code works like a charm. There's seems to be some sort of misuse at line 26 where I'm comparing

if (price_coordinate_x > price_coordinate_y)


Here's my latest submission:

89767134

Here's also my code:

#include <iostream>

using namespace std;

int main(){
// take inputs
long int board_length;
long int price_coordinate_x;
long int price_coordinate_y;

cin >> board_length;
cin >> price_coordinate_x;
cin >> price_coordinate_y;

// starting points for kings
int white_x = 1;
int white_y = 1;
long int black_x = board_length;
long int black_y = board_length;

// Difference between price coordinate points
long int point_difference_white = labs(price_coordinate_x-price_coordinate_y);
long int point_difference_black = labs(price_coordinate_x-price_coordinate_y);

// if x > y => y will be the first value we'll reach
if (price_coordinate_x > price_coordinate_y) {
point_difference_white += labs(white_x-price_coordinate_y);
point_difference_black += labs(black_x-price_coordinate_x);
}
else if (price_coordinate_x < price_coordinate_y) {
point_difference_white += labs(white_x-price_coordinate_x);
point_difference_black += labs(black_y-price_coordinate_y);
}
else {
point_difference_white += labs(white_x-price_coordinate_x);
point_difference_black += labs(black_x-price_coordinate_x);
}

if (point_difference_black >= point_difference_white) {
cout << "White" << endl;
}
else if (point_difference_black < point_difference_white) {
cout << "Black" << endl;
}
return 0;
}


• +5

 » 5 weeks ago, # |   0 long int is 32 bit type, better use long long for 64 bit. Not sure if it works then.
•  » » 5 weeks ago, # ^ |   0 Got a little further. Still failing with some large numbersInput: 719386363530333627 620916440917452264 265151985453132665Get's the right answers locally tho.
•  » » » 5 weeks ago, # ^ | ← Rev. 2 →   0 But it simple WA, so you can improve your logic. Since a king can move diagonal, number of steps per player is max(xdiff,ydiff).
•  » » » » 5 weeks ago, # ^ |   +3 Thanks for your help by the way. I will try to rewrite the logic using the method you suggested!
•  » » » » 5 weeks ago, # ^ |   +3 Like so? :) #include using namespace std; int main(){ // take inputs long long board_length; long long price_coordinate_x; long long price_coordinate_y; cin >> board_length; cin >> price_coordinate_x; cin >> price_coordinate_y; // starting points for kings int white_x_y = 1; long long black_x_y = board_length; long long white_diff_x = llabs(price_coordinate_x-white_x_y); long long white_diff_y = llabs(price_coordinate_y-white_x_y); long long black_diff_x = llabs(price_coordinate_x-black_x_y); long long black_diff_y = llabs(price_coordinate_y-black_x_y); long long steps_white = max(white_diff_x,white_diff_y); long long steps_black = max(black_diff_x,black_diff_y); if (steps_black >= steps_white) { cout << "White" << endl; } else if (steps_black < steps_white) { cout << "Black" << endl; } return 0; } 
•  » » » » » 5 weeks ago, # ^ |   0 What about like so?  ll w=max(x,y)-1; ll b=max(n-x, n-y); if(w<=b) cout<<"White"<
•  » » » » » » 5 weeks ago, # ^ | ← Rev. 2 →   +5 Amazing how simple it can get. My solution was way more complicated than it needed to be. I have a lot to learn. Anyways, here's my latest solution, I think I captured what you were aiming for? #include using namespace std; typedef long long ll; int main(){ // take inputs ll board_length; ll coordinate_x; ll coordinate_y; cin >> board_length; cin >> coordinate_x; cin >> coordinate_y; ll w_steps = max(coordinate_x,coordinate_y)-1; ll b_steps = max(board_length-coordinate_x, board_length-coordinate_y); if (w_steps <= b_steps){ cout << "White" << endl; } else { cout << "Black" << endl; } return 0; } 
•  » » » » » » » 5 weeks ago, # ^ |   0 Well done ;)
 » 5 weeks ago, # |   0 The original approach got accepted by using llabs function instead of labs function. Still going to write the logic as suggested by @spookywooky