Editorial for Rock-Paper-Scissors


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Editorialist: PHPeasant

The only observation to make is that a game with a distinct winner must have three players matching. This cuts the branching logic into four distinct cases (one for each possible winner) each with three possible sub-cases.

An example solution in C++14 is given below:

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string a,b,c,d;
    cin >> a >> b >> c >> d;
        string winner = "?";
    if(a == b && a == d){
        // S can win
        if(c == "rock"){
            if(b == "scissors")
                winner = "S";
        } else if(c == "paper"){
            if(b == "rock")
                winner = "S";
        } else {
            if(b == "paper")
                winner = "S";
        }
    } else if(a == c && a == d){
        // D can win
        if(b == "rock"){
            if(a == "scissors")
                winner = "D";
        } else if(b == "paper"){
            if(a == "rock")
                winner = "D";
        } else {
            if(a == "paper")
                winner = "D";
        }
    } else if (b == c && b == d){
        // A can win
        if(a == "rock"){
            if(b == "scissors")
                winner = "A";
        } else if(a == "paper"){
            if(b == "rock")
                winner = "A";
        } else {
            if(b == "paper")
                winner = "A";
        }
    } else if (a == b && a == c){
        // T can win
        if(d == "rock"){
            if(b == "scissors")
                winner = "T";
        } else if(d == "paper"){
            if(b == "rock")
                winner = "T";
        } else {
            if(b == "paper")
                winner = "T";
        }
    }
    cout << winner;
}

Comments

There are no comments at the moment.