Zero-Move Nim | HackerRank

Problem

Nim is a famous game in which two players take turns removing items from distinct piles. During each turn, a player must remove one or more items from a single, non-empty pile. The winner of the game is whichever player removes the last item from the last non-empty pile.

John and Kate modified Nim by adding the following rule, which they call a Zero-Move:

For each non-empty pile, either player can remove items from that pile and have it count as their move; however, this move can only be performed once per pile by either player. For example, let's say pile initially has items in it. If John decides to use a Zero-Move on pile, then neither John nor Kate can perform another Zero-Move on pile; that said, either player is free to perform a Zero-Move on any other non-empty pile that hasn't had a Zero-Move performed on it yet.

John and Kate play games of Zero-Move Nim. Given the number of items in each pile for each game, determine whether or not John can win the game if he always moves first and each player always moves optimally (i.e., never makes a move that causes them to lose if some better, winning move exists). For each game, print W on a new line if John can win; otherwise, print L instead.

Input Format

The first line contains an integer, , denoting the number of games. The subsequent lines describe each game over two lines:

  1. The first line contains an integer, , denoting the number of heaps.
  2. The second line contains space-separated integers describing .

Constraints

Subtasks

Output Format

For each game, print W on a new line if John will win; otherwise, print L instead.

Sample Input 0

2
2
1 2
2
2 2

Sample Output 0

W
L

Explanation 0

John and Kate play the following games:

  1. We have two piles, and . John removes item from , so . Now that there is only item in each pile, gameplay can proceed in either of the following ways:

Because John always wins in either scenario, we print W on a new line.

  1. John cannot win this game because the two piles are of equal size and Kate has an opportunity to counter any move he makes by performing the same action. Consider the following scenarios:

Because John always loses this game, we print L on a new line.

#include<bits/stdc++.h>
using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

char zeroMoveNim(vector<int> p){

}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));
    string g_temp;
    getline(cin, g_temp);

int g = stoi(ltrim(rtrim(g_temp)));

for(int g_itr = 0; g_itr < g; g_itr++){
        string n_temp;
        getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));
        string p_count_temp;
        getline(cin, p_count_temp);

int p_count = stoi(ltrim(rtrim(p_count_temp)));
        string p_temp_temp;
        getline(cin, p_temp_temp);

vector<string> p_temp = split(rtrim(p_temp_temp));
        vector<int> p(p_count);

for(int i = 0; i < p_count; i++){
            int p_item = stoi(p_temp[i]);
            p[i] = p_item;
        }

char result = zeroMoveNim(p);
        fout << result << "\n";
    }
    fout.close();
    return 0;
}

string ltrim(const string &str){
    string s(str);
    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );
    return s;
}

string rtrim(const string &str){
    string s(str);
    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );
    return s;
}

vector<string> split(const string &str){
    vector<string> tokens;
    string::size_type start = 0;
    string::size_type end = 0;
    while((end = str.find(" ", start)) != string::npos){
        tokens.push_back(str.substr(start, end - start));
        start = end + 1;
    }
    tokens.push_back(str.substr(start));
    return tokens;
}