Analysis of USACO past bronze group real questions | January 2019 Shell Game

Learn C++ from a young age! Record the questions in the study process of preparing for the USACO (American Informatics Olympiad) bronze category competition and record every moment.

Attached is a summary post:Analysis of USACO past bronze group real questions | Summary-CSDN Blog


[Title description]

To pass the time, Bessie the cow and her friend Elsie like to play a game they saw at an agricultural show.

In preparation for the game, Bessie placed three upside-down nut shells on the table and hid a small pebble under one of the shells (at least she hoped it was a pebble - she found it on a pasture ground). Bessie would then switch the nut shells two by two while Elsie tried to guess the location of the pebbles.

The standard format for this game that cows see at agricultural shows is that the player is shown the initial position of the pebble and then asked to guess the final position of the pebble after all swaps have been made.

However, now the cows want to play a game where Elsie doesn't know the initial position of the pebbles, and she can guess the location of the pebbles after each exchange. Bessie knows the correct answer and will give Elsie a score at the end of the game, equal to the number of times she guessed correctly.

Given all exchanges and Elsie's guesses, but not the initial position of the pebble, find the highest possible score for Elsie.

【enter】

The first line of input contains an integer N, which is the number of exchanges (1≤N≤100). Each of the following N lines describes a round of the game, containing three integers a, b, and g, indicating that Bessie exchanged nut shells a and b, and then Elsie guessed nut shell g. All three numbers are one of 1, 2, or 3, and a≠b.

【Output】

Output the highest score that Elsie can get.

【Input sample】

3
1 2 1
3 2 1
1 3 1

【Output sample】

2

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int n;
struct node {
    int a, b, g;
}p[105];
ifstream filein("shell.in");
ofstream fileout("shell.out");
int main()
{
    // cin >> n;
    filein >> n;  // 输入n
    for (int i=1; i<=n; i++) {  // 依次输入n行交换的位置和猜的位置
        filein >> p[i].a >> p[i].b >> p[i].g;
    }
    int maxn = 0, tot=0;  // 定义最高分数,初始为0。定义第一轮统计分数,初始为0
    int a[4] = {0,1,0,0};  // 如果鹅卵石在第1个位置
    for (int i=1; i<=n; i++) {  // 按照n行记录进行位置交换
        swap(a[p[i].a], a[p[i].b]);
        if (a[p[i].g] == 1) tot++;  // 每次交换后检查猜的位置是否有鹅卵石,如果有的话统计分数自增1
    }
    maxn = max(maxn, tot);  // 比较得到最高分数
    tot=0;  // 第二轮统计分数,重新初始化为0
    int b[4] = {0,0,1,0};  // 如果鹅卵石在第2个位置
    for (int i=1; i<=n; i++) {  // 按照n行记录进行位置交换
        swap(b[p[i].a], b[p[i].b]);
        if (b[p[i].g] == 1) tot++;  // 每次交换后检查猜的位置是否有鹅卵石,如果有的话统计分数自增1
    }
    maxn = max(maxn, tot);  // 比较得到最高分数
    tot=0;  // 第三轮统计分数,重新初始化为0
    int c[4] = {0,0,0,1};  // 如果鹅卵石在第3个位置
    for (int i=1; i<=n; i++) {  // 按照n行记录进行位置交换
        swap(c[p[i].a], c[p[i].b]);
        if (c[p[i].g] == 1) tot++;  // 每次交换后检查猜的位置是否有鹅卵石,如果有的话统计分数自增1
    }
    maxn = max(maxn, tot);  // 比较得到最高分数
    fileout << maxn << endl;  // 最后输出最高分数
    return 0;
}

【operation result】

3
1 2 1
3 2 1
1 3 1
2

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/134808379