HNU software capability training 2-6 Flight chess

Posted by ninib on Thu, 20 Jan 2022 05:41:29 +0100

Write in front

Hello! Welcome to my blog, I hope my ideas can help you!

Problem description

Everyone must have played flying chess. Now, the chess that Lele and Yueyue want to play is very similar to this, but it's a little simpler.

The chessboard is composed of N grids, marked as grid 0 to grid N-1 respectively. There are two kinds of grids. One is an ordinary grid, which means that you can stay in this grid. Otherwise, it is a special grid. Once you go to the top, you should fly to the corresponding grid according to the number marked on the top. If you fly to a special grid, you can continue to fly.

Except for 0, all other squares can only accommodate one player. That is, once player A is already on A grid and player B comes here, player A will be kicked back to grid 0, and player B will stay on this grid.

The nth-1st grid is the end. Once a player comes to this grid, the player wins and the game is over.

At the beginning, both players stand on grid 0, throw dice in turn, and take the corresponding grid number according to the points displayed on the dice. For example, if a player throws 5 points in grid 0, he will go to grid 5. If the player goes beyond the range of the chessboard, he must go back a certain number of steps. For example, there are 7 (0 ~ 6) squares on the chessboard. The player throws 6 points on the fourth grid, and finally he will go to the second grid (4 - > 5 - > 6 - > 5 - > 4 - > 3 - > 2).

According to the observation, the number of dice thrown is also regular.
For each chess game, the first point thrown is F0=(AC+B)%6+1, the second point is F1=(AF0+B)%6+1, the third point is F2=(A*F1+B)%6+1... And so on.

Lele goes first in every game of chess. Now please be the referee to see who can win.

Input form

This topic contains multiple groups of tests. Please deal with it until the end of the document.

Each group of data occupies two rows.
The first line has four integers n, a, B and C (see the title description for the meaning, 6 < n < 200,0 < = a, B, C < = 2 ^ 31).
The second line has N strings, representing the contents of the 0th to N-1 st grids on the chessboard respectively. Two strings are separated by a space.

If the string is "n", it means that this lattice is an ordinary lattice. Otherwise, the string is in the form of "GX"(X is an integer between 0 and N-1), where x means that when the player comes to this grid, he should fly to the X grid immediately.

The data guarantees that the 0 and N-1 grids must be "n".

Output form

For each group of data, the results are output in one row.
If Lele can win the game, it will output "Lele". If Yueyue wins, it will output "Yueyue".

sample input

7 1 0 6
N G3 N N N N N
7 1 0 6
N G4 N N N N N

sample output

Lele
Yueyue

Problem solving ideas

This problem is a simulation of the whole process. Because the end condition is uncertain, we need a forever true loop to simulate the whole process of playing flying chess.

There are two kinds of chessboard, one is an ordinary chessboard, and the other is a chessboard that needs to be operated (pointing to the corresponding number of steps forward). This special chessboard needs a string to store, so it can only be stored in a string array.

After each roll of the dice, there are three situations:
1. Go to the end and win directly.
2. The current position plus the number of points is not enough to reach the end point.
3. The current position plus points reached the end and went back.

Obviously, the third case is the most difficult, so we will only discuss the third case.

Where n is the number of grids and p is the current position

(n − 1 − p) is the distance from the end point to the current position
(F - (n - 1 - P)) is the number of points thrown this time. How many steps are left after reaching the end
(n - 1 - (F - (n - 1 - P))) is the position after return
(please simulate by yourself)

After determining the position, you need to judge whether the current grid is a special grid, and use the string's own function find to find "G". If it is a special grid, we need to convert the numeric characters after G to integers, and then change the current position.

(I don't consider whether the next grid is also a special grid when I come down from a special grid, but it can pass, so there is a problem with the code.)

AC code

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

const int N=2010;
string pos[N];

int main()
{
    int n,a,b,c;
    int pl,py;
    while(cin>>n>>a>>b>>c)
    {
        int f=(a*c+b)%6+1;
        for(int i=0;i<n;i++) cin>>pos[i];
        pl=0;
        py=0;
        while(1)
        {
            if(pl+f==n-1)
            {
                cout<<"Lele"<<endl;
                break;
            }
            else if(pl+f<n-1)
            {
                if(pos[pl+f].find("G")!=-1)
                {
                    string temp=pos[pl+f].substr(1);
                    int d=0;
                    for(int i=0;i<temp.size();i++)
                        d+=((temp[i]-'0')*(pow(10,temp.size()-i-1)));
                    pl=d;
                }
                else pl+=f;
            }
            else if(pl+f>n-1)
            {
                pl=(n-1-(f-(n-1-pl)));
                if(pos[pl].find("G")!=-1)
                {
                    string temp=pos[pl].substr(1);
                    int d=0;
                    for(int i=0;i<temp.size();i++)
                        d+=((temp[i]-'0')*(pow(10,temp.size()-i-1)));
                    pl=d;
                }
            }
            f=(a*f+b)%6+1;
            if(py+f==n-1)
            {
                cout<<"Yueyue"<<endl;
                break;
            }
            else if(py+f<n-1)
            {
                if(pos[py+f].find("G")!=-1)
                {
                    string temp=pos[py+f].substr(1);
                    int d=0;
                    for(int i=0;i<temp.size();i++)
                        d+=((temp[i]-'0')*(pow(10,temp.size()-i-1)));
                    py=d;
                }
                else py+=f;
            }
            else if(py+f>n-1)
            {
                py=(n-1-(f-(n-1-py)));
                if(pos[py].find("G")!=-1)
                {
                    string temp=pos[py].substr(1);
                    int d=0;
                    for(int i=0;i<temp.size();i++)
                        d+=((temp[i]-'0')*(pow(10,temp.size()-i-1)));
                    py=d;
                }
            }
            f=(a*f+b)%6+1;
        }
    }
    system("pause");
    return 0;
}

Write at the end

If there are any problems with the code, please comment or send a private letter

Topics: C++