CCF模拟题 202305-1 重复局面

试题编号: 202305-1

试题名称: 重复局面

时间限制: 1.0s

内存限制: 512.0MB

题目背景

国际象棋在对局时,同一局面连续或间断出现3次或3次以上,可由任意一方提出和棋。

问题描述

国际象棋每一个局面可以用大小为 8×8 的字符数组来表示,其中每一位对应棋盘上的一个格子。六种棋子王、后、车、象、马、兵分别用字母 k、q、r、b、n、p 表示,其中大写字母对应白方、小写字母对应黑方。棋盘上无棋子处用字符 * 表示。两个字符数组的每一位均相同则说明对应同一局面。

现已按上述方式整理好了每步棋后的局面,试统计每个局面分别是第几次出现。

输入格式

从标准输入读入数据。

输入的第一行包含一个正整数 n,表示这盘棋总共有 n 步。

接下来 8×n 行,依次输入第 1 到第 n 步棋后的局面。具体来说每行包含一个长度为 8 的字符串,每 8 行字符串共 64 个字符对应一个局面。

输出格式

输出到标准输出中。

输出共 n 行,每行一个整数,表示该局面是第几次出现。

样例输入

复制代码
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*

样例输出

复制代码
1
1
1
1
1
2
2
1

Java代码:

复制代码
// CCF_2023_05_1
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine()); // 读取局面数量

        // 使用HashMap存储每个局面出现的次数
        Map<String, Integer> positionFrequencyMap = new HashMap<>();
        List<Integer> output = new ArrayList<>(); // 存储输出结果

        // 读取每个局面
        for (int i = 0; i < n; i++) {
            StringBuilder position = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                position.append(scanner.nextLine());
            }
            String positionString = position.toString();

            // 更新局面出现次数
            positionFrequencyMap.put(positionString, positionFrequencyMap.getOrDefault(positionString, 0) + 1);
            // 将当前局面的出现次数添加到输出结果列表中
            output.add(positionFrequencyMap.get(positionString));
        }

        // 打印输出结果
        for (int freq : output) {
            System.out.println(freq);
        }

        scanner.close();
    }
}

C语言代码:

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BOARD_SIZE 8
#define MAX_POSITIONS 100

// 函数进行字符串哈希
unsigned long hash(char *str) {
    unsigned long hash = 5381;
    int c;

    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c; // hash * 33 + c
    }
    return hash;
}

int main() {
    int n, step;
    char positions[MAX_POSITIONS][BOARD_SIZE * BOARD_SIZE + 1]; // 存储所有局面字符串
    int counts[MAX_POSITIONS] = {0}; // 存储所有局面的出现次数
    char board[BOARD_SIZE][BOARD_SIZE + 1]; // 暂存棋盘的单一局面
    char position[BOARD_SIZE * BOARD_SIZE + 1]; // 暂存单一局面的字符串表示
    unsigned long hashes[MAX_POSITIONS]; // 存储局面字符串的哈希
    unsigned long hashValue;
    int results[MAX_POSITIONS]; // 存储所有结果

    scanf("%d\n", &n);
    int totalPositions = 0;

    for (step = 0; step < n; ++step) {
        // 读入棋盘局面
        for (int i = 0; i < BOARD_SIZE; ++i) {
            fgets(board[i], BOARD_SIZE + 2, stdin); // +2 for newline and null-terminator
        }

        // 将局面转为一个字符串
        for (int i = 0, k = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j, ++k) {
                position[k] = board[i][j];
            }
        }
        position[BOARD_SIZE * BOARD_SIZE] = '\0'; // 确保正确结束字符串

        // 计算局面字符串的哈希
        hashValue = hash(position);

        // 检查是否已经存在此局面
        int found = 0;
        for (int i = 0; i < totalPositions; ++i) {
            if (hashes[i] == hashValue && strcmp(positions[i], position) == 0) {
                counts[i]++;
                results[step] = counts[i];
                found = 1;
                break;
            }
        }

        // 如果没找到,记录新局面
        if (!found) {
            strcpy(positions[totalPositions], position);
            hashes[totalPositions] = hashValue;
            counts[totalPositions] = 1;
            results[step] = 1;
            totalPositions++;
        }
    }

    // 输出所有结果
    for (int i = 0; i < n; ++i) {
        printf("%d\n", results[i]);
    }

    return 0;
}
相关推荐
BENA ceic4 分钟前
Spring 的三种注入方式?
java·数据库·spring
小雅痞7 分钟前
[Java][Leetcode middle] 209. 长度最小的子数组
java·算法·leetcode
王老师青少年编程10 分钟前
csp信奥赛C++高频考点专项训练之字符串 --【字符串基础】:[NOIP 2018 普及组] 标题统计
c++·字符串·csp·高频考点·信奥赛·专项训练·标题统计
二哈赛车手18 分钟前
新人笔记---项目中简易版的RAG检索后评测指标(@Recall ,Mrr..)实现
java·开发语言·笔记·spring·ai
做时间的朋友。19 分钟前
精准核酸检测
java·数据结构·算法
许彰午31 分钟前
CacheSQL(五):桥接篇
java·数据库·缓存·系统架构
冯诺依曼的锦鲤32 分钟前
从零实现高并发内存池:TCMalloc 核心架构拆解
c++·学习·算法·架构
Thomas_Lee_OR35 分钟前
多Agent路径规划 LaCAM for multi-agent path finding (MAPF)
算法·路径规划·仓储机器人·mapf
ATCH IERV42 分钟前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
一切皆是因缘际会43 分钟前
可落地数字生命工程:从记忆厮杀到自我意识觉醒全链路,AGI内生智能硅基生命心智建模
人工智能·深度学习·算法·机器学习·ai·系统架构·agi