CSP-J semi-finals sprint must-answer questions | P1076 Treasure Hunt

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: CSP-J semi-final sprint must-answer questions | Summary_Blog of a communicator who loves programming-CSDN Blog


[Title description]

Legend has it that a tempting treasure is hidden on the top floor of a distant treasure building. Xiao Ming went through all kinds of hardships and finally found the legendary treasure house. There was a wooden board at the door of the treasure house with several big words written on it: treasure hunting instructions. The contents of the instruction manual are as follows:

There are N + 1 floors in the treasure building . The top floor is the top floor. There is a room on the top floor where the treasure is hidden. In addition to the top floor, there are N floors in the treasure building, with M rooms on each floor. These M rooms form a circle and are numbered 0,..., M −1 in counterclockwise direction. Some of these rooms have stairs leading to the upper floor, and the design of the stairs may be different on each floor. There is a sign in each room, and there is a number x on the sign , which means starting from this room, choose the xth room with stairs in a counterclockwise direction (assuming that the room is numbered k), and go upstairs from this room. Go upstairs and reach Room K on the upper floor. For example, if the sign in the current room says 2, start trying in a counterclockwise direction, find the second room with stairs, and go upstairs from that room. If the current room itself has stairs leading to the upper floor, this room is treated as the first room with stairs.

At the end of the treasure hunt instructions, it reads in large red font: "Treasure Hunting Instructions: To help you find the numbers on the signboards in the rooms upstairs on each floor (that is, the numbers on the signboards in the first room you enter on each floor). The sum is open. Key to the Treasure Chest".

Please help Xiao Ming figure out the key to open the treasure chest.

【enter】

The first line contains two integers N and M , separated by a space. N means that there are N floors in the treasure house except the top floor , and M means that there are M rooms on each floor except the top floor .

The next N × M lines have two integers in each line, separated by a space. Each line describes the situation in a room, where the (i −1 ) × M + j line represents the i-th layer j −1 Room situation ( i =1,2,…, N ; j =1,2,…, M ). The first integer represents whether the room has stairs leading to the upper floor (0 means no, 1 means yes), and the second integer represents the number on the sign. Note that the room you reach by climbing the stairs from room j to the upper floor must also be room j .

The last line, an integer, indicates which room on the bottom floor of the treasure building Xiao Ming entered to start treasure hunting (note: room numbers start from 0).

【Output】

An integer representing the key to open the treasure chest. This number may be very large. Please output the result modulo 20123.

【Input sample】

2 3
1 2
0 3
1 4
0 1
1 5
1 2
1

【Output sample】

5

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
bool is_door[10005][105];  //记录是否有门
int num[10005][105];  //记录指示牌上的数字
int numdoor[10005];  //记录每层楼门的数量
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i=1; i<=n; i++) {
        int door = 0;  //记录该层楼有楼梯的门数,用于之后的门数取模,避免绕圈查找
        for (int j=0; j<m; j++) {
            cin >> is_door[i][j] >> num[i][j];
            if (is_door[i][j]==true) door++;
        }
        numdoor[i] = door;
    }
    int start;
    cin >> start;

    int ans = 0;
    for (int i=1; i<=n; i++) {  //从第一层开始走
        ans += num[i][start];
        ans %= 20123;  //边加边取模

        num[i][start] %= numdoor[i];  //数据处理,避免绕圈查找
        if (num[i][start] == 0) num[i][start] = numdoor[i];  //刚好一圈的情况

        int sum = 0;  //记录这一层已经找到的门的数目
        int j; 
        for (j=start;;j++) {
            if (j==m) j = 0;  //一圈走完,重置为0
            if (is_door[i][j] == true) sum++;  // 是门,sum增加
            if (sum==num[i][start]) break;
        }
        start = j;  //找到下一个上楼点
    }
    cout << ans;
    return 0;
}

【operation result】

2 3
1 2
0 3
1 4
0 1
1 5
1 2
1
5

Guess you like

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