When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

wdem's blog

By wdem, history, 4 years ago, In English

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;
}
  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

long int is 32 bit type, better use long long for 64 bit. Not sure if it works then.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Got a little further. Still failing with some large numbers

    Input: 719386363530333627 620916440917452264 265151985453132665

    Get's the right answers locally tho.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      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).

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Thanks for your help by the way. I will try to rewrite the logic using the method you suggested!

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Like so? :)

        #include <iostream>
        
        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;
            
        }
        
        
        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          What about like so?

              ll w=max(x,y)-1;
              ll b=max(n-x, n-y);
              if(w<=b)
                  cout<<"White"<<endl;
              else
                  cout<<"Black"<<endl;
          
          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
            Rev. 2   Vote: I like it +5 Vote: I do not like it

            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 <iostream>
            
            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;
            }