CCF-CSP Zhenti "202305-1 Repeated Situation" thinking + python, c++ full score solution

Students who want to check the real questions and solutions of other questions can go to: CCF-CSP real questions with solutions


Question No.: 202305-1
Question name: Repeat position
time limit: 1.0s
Memory limit: 512.0MB
Problem Description:

topic background

During a game of chess, if the same situation occurs continuously or intermittently for 3 or more times, any side can propose a draw.

Problem Description

Each chess situation can be represented by an 8×8 character array, where each bit corresponds to a grid on the board. The six chess pieces king, queen, rook, bishop, horse, and pawn are represented by letters  k, q, r, b, n, respectively p , wherein uppercase letters correspond to white squares, and lowercase letters correspond to black squares. There are no chess pieces on the chessboard to  * represent with characters. If each bit of the two character arrays is the same, it means that they correspond to the same situation.

Now that the situation after each chess move has been sorted out in the above-mentioned way, try to count how many times each situation occurs.

input format

Read data from standard input.

The first line of input contains a positive integer n, indicating that there are a total of n moves in this game.

In the next 8×n lines, enter the position after the 1st to nth moves in turn. Specifically, each line contains a string with a length of 8, and each 8-line string has a total of 64 characters corresponding to a situation.

output format

output to standard output.

Output a total of n lines, each with an integer, indicating how many times the situation occurs.

sample input


********
******pk
*****r*p
p*pQ****
*b******
****B*PP
****qP**
**R***K*
********
******pk
*****r*p
p*p*****
*b**Q***8
********
******pk
*****r*p
p*pQ****
********
**b*B*PP
****qP**
**R***K*
****B*PP
****qP**
**R***K*
******k*
******p*
*****r*p
p*p*****
*b**Q***
****B*PP
****qP**
**R***K*
******k*
******p*
*****r*p
p*pQ****
*b******
****B*PP
****qP**
**R***K*
********
******pk
*****r*p
p*pQ****
*b******
****B*PP
****qP**
**R***K*
********
******pk
*****r*p
p*p*****
*b**Q***
****B*PP
****qP**
**R***K*
********
******pk
******rp
p*p*****
*b**Q***
****B*PP
****qP**
**R***K*

sample output

1
1
1
1
1
2
2
1

Sample description

The positions after steps 6 and 7 are the same as the positions after steps 2 and 3 respectively. The position after step 8 corresponds to the picture above.

Subtasks

The input data satisfies n≤100.

hint

Judging repeated positions only involves string comparison, without considering the actual chess rules.

Source of real question: field measurement

 Interested students can code in this way for practice submission

Ideas explained:

It seems complicated. In fact, it is only necessary to save the chess pieces of each chessboard entered in the character array, and then judge whether the string has appeared in the map. If it has appeared, add one to the number of occurrences, otherwise record the occurrence of the string. The number of times is 1.

 Python full score solution:

n = int(input())
chess = {}
for i in range(n):
    temp = ''
    for j in range(8):
        temp += input()
    if temp not in chess:
        chess[temp] = 1
    else:
        chess[temp] += 1
    print(chess[temp])

operation result:


C++ full score solution:

#include <bits/stdc++.h>
using namespace std;
int n;
char pieces[64];
map<string, int> status_map;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 64; j++) 
            cin >> pieces[j];
        if (status_map.count(pieces)) 
            status_map[pieces] ++;
        else 
            status_map[pieces] = 1;
        cout << status_map[pieces] << endl;
    }
    return 0;
}

 operation result:

 

Guess you like

Origin blog.csdn.net/weixin_53919192/article/details/131489840