Skip to content

USACO 2019 January Contest Bronze Division - Shell Game#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Fix each starting position and then simulate the swaps.

Source codes#

The source codes in C++ and Python can be seen below.

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

struct contents
{
    int a, b, c;
};
int main()
{
    ifstream cin("shell.in");
    ofstream cout("shell.out");

    int n;
    cin >> n;

    contents v[n+1];
    for(int i = 1; i <= n; i++)
        cin >> v[i].a >> v[i].b >> v[i].c;

    int maxscore = 0;
    for(int i = 1; i <= 3; i++)
    {
        int score = 0;
        int poz = i;
        for(int j = 1; j <= n; j++)
        {
            if(poz == v[j].a)
                poz = v[j].b;
            else
                if(poz == v[j].b)
                    poz = v[j].a;
            if(poz == v[j].c)
                score++;
        }
        maxscore = max(maxscore, score);
    }
    cout << maxscore;
    return 0;
}
with open("shell.in", "r") as fin:
    n = int(fin.readline().strip())
    v = [None] * (n + 1)
    for i in range(1, n + 1):
        a, b, c = map(int, fin.readline().split())
        v[i] = (a, b, c)
maxscore = 0
for i in range(1, 4):
    score = 0
    poz = i
    for j in range(1, n + 1):
        a, b, c = v[j]
        if poz == a:
            poz = b
        elif poz == b:
            poz = a
        if poz == c:
            score += 1
    maxscore = max(maxscore, score)
with open("shell.out", "w") as fout:
    fout.write(str(maxscore))