ZeroJudge C297: Baseball Game

Qian-Qian recently got into baseball and wants to write a simplified game-scoring program himself. This program will read the batting results of each player on the team and then calculate the team's score.

 This is a simplified simulation, assuming that the batting results of hitters are only as follows:

  • Base hit: Represented by "1B," "2B," "3B," and "HR" respectively, indicating "single," "double," "triple," and "home run."
  • Out: Represented by "FO", "GO", and "SO".

This simplified version follows the rules below:

  • There are four bases on the field, called home plate, first base, second base, and third base.
  • A player holding a bat at home plate and ready to hit the ball is called a "batter," while those positioned at the other three bases are called "base runners."
  • When the batter hits a "hit," players on the field (both the batter and base runners) can move; when the result is an "out," the base runners stay put, and the batter leaves the field to be replaced by the next batter.
  • The team has a total of nine players, lined up sequentially. The game starts with the first player batting, and when player i finishes batting, player (i + 1) becomes the batter. When the ninth player finishes batting, the cycle returns to the first player.
  • When hitting a K-base hit, the players on the field (batter and runners) advance K base. Advancing one base from home plate takes them to first base, then second base, third base, and finally back to home plate.
  • Each player earns 1 point when returning to home plate.
  • Bases one, two, and three are cleared (runners leave) and reset every three outs.

Please write a program with the following functionality to calculate the total score of the team.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Each set of test data consists of ten lines.

The 1st to 9th lines, in the order of players, each represent the hitting information of a player. Each line starts with a positive integer a (1 ≤ a ≤ 5), representing the player's total number of hits. Following that are a strings (each consisting of two characters), representing the result of each hit in order. Data are separated by a single space character. There are no errors or omissions in the hitting information of the players.

The 10th line contains a positive integer b (1 ≤ b ≤ 27), indicating that we want to calculate the team's score when the total number of outs reaches b. The hitting information provided as input includes at least b outs.
Calculate the total score when the total number of outs reaches b, and output this score in one line.
5 1B 1B FO GO 1B
5 1B 2B FO FO SO
4 SO HR SO 1B
4 FO FO FO HR
4 1B 1B 1B 1B
4 GO GO 3B GO
4 1B GO GO SO
4 SO GO 2B 2B
4 3B GO GO FO
3
0
5 1B 1B FO GO 1B
5 1B 2B FO FO SO
4 SO HR SO 1B
4 FO FO FO HR
4 1B 1B 1B 1B
4 GO GO 3B GO
4 1B GO GO SO
4 SO GO 2B 2B
4 3B GO GO FO
6
5
ZeroJudge C297 範例測資

Thought Process

After collecting the data, you can determine the current batter's hit type. Then start from the third base. If it's a single, the runner on the third base will score and the score will increase by 1, then the third base will be cleared. Then check the second base. If there is a runner on the second base, they will advance to the third base and the second base will be cleared, and so on. It's important to note that all runners on bases should be cleared when the number of outs is a multiple of 3.

Sample Code-ZeroJudge C297: Baseball Game

#include <iostream>
#include <vector>
using namespace std;

int rtn(string str)
{
    if (str[0] == '1') return 1;
    if (str[0] == '2') return 2;
    if (str[0] == '3') return 3;
    if (str[0] == 'H') return 4;
    else return -1;
}

int main() {
    vector<vector<int>>all;
    int max = -999;
    for (int i = 0; i<9; i++)
    {
        int N;
        cin >> N;
        if (N > max) max = N;
        vector<int>score;
        for (int j = 0; j<N; j++)
        {
            string str;
            cin >> str;
            score.push_back(rtn(str));
        }
        all.push_back(score);
    }
    int N;
    cin >> N;
    vector<int>base;
    base.push_back(0);
    base.push_back(0);
    base.push_back(0);
    base.push_back(0);
    int score = 0;
    int out = 0;
    for (int i = 0; i<max; i++)
    {
        for (int j = 0; j<9; j++)
        {
            int aa = all[j][i];
            if (aa == 1)
            {
                if (base[3] == 1)
                {
                    score++;
                    base[3] = 0;
                }
                if (base[2] == 1)
                {
                    base[3] = 1;
                    base[2] = 0;
                }
                if (base[1] == 1) base[2] = 1;
                base[1] = 1;
            }
            else if (aa == 2)
            {
                if (base[3] == 1)
                {
                    score++;
                    base[3] = 0;
                }
                if (base[2] == 1)
                {
                    score++;
                    base[2] = 0;
                }
                if (base[1] == 1)
                {
                    base[1] = 0;
                    base[3] = 1;
                }
                base[2] = 1;
            }
            else if (aa == 3)
            {
                if (base[3] == 1)
                {
                    score++;
                    base[3] = 0;
                }
                if (base[2] == 1)
                {
                    score++;
                    base[2] = 0;
                }
                if (base[1] == 1)
                {
                    score++;
                    base[1] = 0;
                }
                base[3] = 1;
            }
            else if (aa == 4)
            {
                score++;
                if (base[1] == 1) score++;
                if (base[2] == 1) score++;
                if (base[3] == 1) score++;
                base[1] = 0;
                base[2] = 0;
                base[3] = 0;
            }
            else if (aa == -1)
            {
                out++;
                if (out == N)
                {
                    cout << score << "\n";
                    break;
                }
                if (out % 3 == 0)
                {
                    base[1] = 0;
                    base[2] = 0;
                    base[3] = 0;
                }
            }
        }
        if (out == N)
        {
            break;
        }
    }
}

//ZeroJudge C297
//Dr. SeanXD

Comments