csp certification real questions - repeated situations - Java question solutions

Table of contents

Question background

Problem Description

Input format

Output format

Sample input

Sample output

Sample description

Subtasks

hint

[Analysis of ideas]

【Code】


Question background

During a chess game, if the same situation occurs three or more times continuously or intermittently, either side can propose a draw.

Problem Description

Each chess position can be represented by an 8×8 character array, where each bit corresponds to a square on the chess board. kThe six types of chess pieces, king, queen, rook, bishop, knight, and pawn, are represented by letters , q, r, b, n, respectively  p . The uppercase letters correspond to the white square, and the lowercase letters correspond to the black square. Places without chess pieces on the chessboard  * are represented by characters. If each bit of the two character arrays is the same, it means they correspond to the same situation.

Now that the positions after each move have been sorted out according to the above method, try to count how many times each position occurs.

Input format

Read data from standard input.

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

In the next 8×n lines, the positions after the 1st to nth moves are entered in sequence. Specifically, each line contains a string of length 8, and each 8-line string contains a total of 64 characters corresponding to a situation.

Output format

Output to standard output.

There are n lines of output, each line contains an integer, indicating how many times the situation occurs.

Sample input

8
********
******pk
*****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*
******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 situation after steps 6 and 7 is the same as the situation after steps 2 and 3 respectively. The situation after step 8 corresponds to the picture above.

Subtasks

The input data satisfies n≤100.

hint

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

[Analysis of ideas]

Because in Java it is impossible to compare whether two arrays are the same based on the hash value of a char[][] two-dimensional array, so it is also impossible to use hashmap to save arrays that have already appeared. But it is easy to think that we can combine the char array as a whole into a string , so that we can judge whether they are the same through the hash value of the string. In this way, array comparison is achieved, and arrays that have already appeared can also be stored.

【Code】

 

package CSP;

import java.util.*;

/**
 * @ProjectName: study3
 * @FileName: ex1
 * @author:HWJ
 * @Data: 2023/8/30 16:57
 */
public class ex1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        HashMap<String, Integer> strs = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String str = "";
            for (int j = 0; j < 8; j++) {
                String s = input.next();
                str += s;
            }
            if (i == 0) {
                System.out.println(1);
                strs.put(str, 1);
            } else {
                if (strs.containsKey(str)){
                    strs.put(str, strs.get(str) + 1);
                }else {
                    strs.put(str,1);
                }
                System.out.println(strs.get(str));
            }
        }
    }


}

Guess you like

Origin blog.csdn.net/weixin_73936404/article/details/132584095