Comprobar si el juego es válido o no

Tres jugadores P1, P2 y P3 están jugando un juego. Pero a la vez solo dos jugadores pueden jugar el juego, por lo que decidieron, a la vez dos jugadores jugarán el juego y uno será espectador. Cuando termina un juego, el que perdió el juego se convierte en espectador en el siguiente juego y el que estaba viendo juega contra el ganador. No puede haber empates en ningún juego. El jugador P1 y P2 jugarán el primer juego. La entrada es el número de juegos jugados n y en la siguiente línea el ganador de n juegos. 

Ejemplos: 

Input : 
No. of Games : 4
Winner of the Game Gi : 1 1 2 3 
Output : YES
Explanation : 
Game1 : P1 vs P2 : P1 wins
Game2 : P1 vs P3 : P1 wins
Game3 : P1 vs P2 : P2 wins
Game4 : P3 vs P2 : P3 wins
None of the winners were invalid

Input : 
No. of Games : 2
Winner of the Game Gi : 2 1 
Output : NO
Explanation : 
Game1 : P1 vs P2 : P2 wins
Game2 : P2 vs P3 : P1 wins (Invalid winner)
In Game2 P1 is spectator 

Enfoque: 
comience con P1 y P2 y continúe el juego mientras convierte al perdedor en nuevo espectador restando el espectador actual y el ganador de la suma total de tres jugadores, es decir, 6. Una entrada inválida detendrá el proceso.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ program to check if the game
// is valid
#include <bits/stdc++.h>
using namespace std;
 
string check_valid(int a[], int n)
{
    // starting with player P1 and P2
    // so making P3 spectator
    int spec = 3;
    for (int i = 0; i < n; i++) {
     
        // If spectator wins a game
        // then its not valid
        if (a[i] == spec) {
            return "Invalid";
        }
         
        // subtracting the current spectator
        // and winner from total sum 6 which
        // makes losing player spectator
        spec = 6 - a[i] - spec;
    }
     
    // None of the winner is found spectator.
    return "Valid";
}
 
// Driver program to test above functions
int main()
{
    int n = 4;
    int a[n] = {1, 1, 2, 3};
    cout << check_valid(a, n);
    return 0;
}

Java

// Java program to check if the game
// is valid
import java.io.*;
 
class GFG {
 
 
     
    static String check_valid(int a[], int n)
    {
        // starting with player P1 and P2
        // so making P3 spectator
        int spec = 3;
         
        for (int i = 0; i < n; i++) {
         
            // If spectator wins a game
            // then its not valid
            if (a[i] == spec) {
                return "Invalid";
            }
             
            // subtracting the current spectator
            // and winner from total sum 6 which
            // makes losing player spectator
            spec = 6 - a[i] - spec;
        }
         
        // None of the winner is found spectator.
        return "Valid";
    }
     
    // Driver program to test above functions
    public static void main (String[] args) {
         
        int a[] = {1, 1, 2, 3};
        int n = a.length;
         
        System.out.println(check_valid(a, n));
     
        }
}
 
// This code is contributed by vt_m

Python3

# Python3 code to check if the game
# is valid
 
def check_valid( a , n ):
 
    # starting with player P1 and P2
    # so making P3 spectator
    spec = 3
    for i in range(n):
         
        # If spectator wins a game
        # then its not valid
        if a[i] == spec:
            return "Invalid"
         
        # subtracting the current spectator
        # and winner from total sum 6 which
        # makes losing player spectator
        spec = 6 - a[i] - spec
     
    # None of the winner is found spectator.
    return "Valid"
 
# Driver code to test above functions
n = 4
a = [1, 1, 2, 3]
print(check_valid(a, n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program to check if the game
// is valid
using System;
 
class GFG {
 
    static String check_valid(int []a, int n)
    {
        // starting with player P1 and P2
        // so making P3 spectator
        int spec = 3;
         
        for (int i = 0; i < n; i++) {
         
            // If spectator wins a game
            // then its not valid
            if (a[i] == spec) {
                return "Invalid";
            }
             
            // subtracting the current spectator
            // and winner from total sum 6 which
            // makes losing player spectator
            spec = 6 - a[i] - spec;
        }
         
        // None of the winner is found spectator.
        return "Valid";
    }
     
    // Driver program
    public static void Main ()
    {
        int []a = {1, 1, 2, 3};
        int n = a.Length;
         
        Console.WriteLine(check_valid(a, n));
     
        }
}
 
// This code is contributed by vt_m

PHP

<?php
// PHP program to check if
// the game is valid
function check_valid( $a, $n)
{
    // starting with player P1 and P2
    // so making P3 spectator
    $spec = 3;
    for ($i = 0; $i < $n; $i++)
    {
     
        // If spectator wins a game
        // then its not valid
        if ($a[$i] == $spec)
        {
            return "Invalid";
        }
         
        // subtracting the current
        // spectator and winner from
        // total sum 6 which makes
        // losing player spectator
        $spec = 6 - $a[$i] - $spec;
    }
     
    // None of the winner is
    // found spectator.
    return "Valid";
}
 
// Driver Code
$n = 4;
$a = array(1, 1, 2, 3);
echo check_valid($a, $n);
 
// This code is contributed by vt_m
?>

Javascript

<script>
 
// Javascript program to check if the game
// is valid
function check_valid(a, n)
{
     
    // Starting with player P1 and P2
    // so making P3 spectator
    let spec = 3;
       
    for(let i = 0; i < n; i++)
    {
         
        // If spectator wins a game
        // then its not valid
        if (a[i] == spec)
        {
            return "Invalid";
        }
           
        // Subtracting the current spectator
        // and winner from total sum 6 which
        // makes losing player spectator
        spec = 6 - a[i] - spec;
    }
       
    // None of the winner is found spectator.
    return "Valid";
}
 
// Driver code
let a = [ 1, 1, 2, 3 ];
let n = a.length;
   
document.write(check_valid(a, n));
 
// This code is contributed by sanjoy_62
 
</script>

Producción : 

Valid

Publicación traducida automáticamente

Artículo escrito por Gautam Karakoti y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *