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 backgroundDuring a game of chess, if the same situation occurs continuously or intermittently for 3 or more times, any side can propose a draw. Problem DescriptionEach 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 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 formatRead 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 formatoutput to standard output. Output a total of n lines, each with an integer, indicating how many times the situation occurs. sample input
sample output
Sample descriptionThe 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. SubtasksThe input data satisfies n≤100. hintJudging 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: