3.17打卡day31

发布时间:2026/5/25 20:23:46

3.17打卡day31 130. 2n皇后问题问题描述给定一个n*n的棋盘棋盘中有一些位置不能放皇后。现在要向棋盘中放入n个黑皇后和n个白皇后使任意的两个黑皇后都不在同一行、同一列或同一条对角线上任意的两个白皇后都不在同一行、同一列或同一条对角线上。问总共有多少种放法n小于等于8。说明同一条对角线是指包括两条主对角线的所有对角线n5时的棋盘从左上往右下有9条对角线从右上往左下也有9条对角线。比如棋盘为1 1 1 11 1 1 11 1 1 11 1 1 1表示一个4*4的棋盘所有位置都可放皇后。则可知有2种放法。#include bits/stdc.h using namespace std; int n; int board[10][10]; // 1可放0不可放 int ans 0; // 三个数组标记列、主对角线、副对角线 bool colB[10], dia1B[20], dia2B[20]; // 黑皇后占用 bool colW[10], dia1W[20], dia2W[20]; // 白皇后占用 // 放白皇后 void dfsWhite(int row) { if (row n) { ans; return; } for (int col 0; col n; col) { // 判断是否可以放白皇后 if (board[row][col] 0) continue; // 棋盘不可放 if (colW[col]) continue; // 列冲突 if (dia1W[row - col n]) continue; // 主对角线冲突 if (dia2W[row col]) continue; // 副对角线冲突 // 放白皇后 colW[col] dia1W[row - col n] dia2W[row col] true; dfsWhite(row 1); colW[col] dia1W[row - col n] dia2W[row col] false; } } // 放黑皇后 void dfsBlack(int row) { if (row n) { // 黑皇后放完开始放白皇后 dfsWhite(0); return; } for (int col 0; col n; col) { // 判断是否可以放黑皇后 if (board[row][col] 0) continue; // 棋盘不可放 if (colB[col]) continue; // 列冲突 if (dia1B[row - col n]) continue; // 主对角线冲突 if (dia2B[row col]) continue; // 副对角线冲突 // 放黑皇后 colB[col] dia1B[row - col n] dia2B[row col] true; dfsBlack(row 1); colB[col] dia1B[row - col n] dia2B[row col] false; } } int main() { cin n; for (int i 0; i n; i) { for (int j 0; j n; j) { cin board[i][j]; } } ans 0; dfsBlack(0); cout ans endl; return 0; }131. 8皇后·改问题描述规则同8皇后问题但是棋盘上每格都有一个数字要求八皇后所在格子数字之和最大。#include bits/stdc.h using namespace std; int board[8][8]; // 存储棋盘每个格子的权值0~99 bool col[8]; // col[i] 表示第 i 列是否已经被皇后占用 bool dia1[15]; // dia1[i] 表示第 i 条主对角线是否被占用 // 主对角线行 - 列 为常数范围 -7 到 7统一加 7 映射到 0~14 bool dia2[15]; // dia2[i] 表示第 i 条副对角线是否被占用 // 副对角线行 列 为常数范围 0 到 14 int maxSum 0; // 记录最大权值和 void dfs(int row, int sum) { // 如果已经放完 8 行更新最大和 if (row 8) { if (sum maxSum) maxSum sum; return; } // 尝试在当前行的每一列放置皇后 for (int c 0; c 8; c) { // 检查当前位置是否可放列未占用两条对角线均未占用 if (col[c] || dia1[row - c 7] || dia2[row c]) continue; // 放置皇后标记占用 col[c] true; dia1[row - c 7] true; dia2[row c] true; // 递归放置下一行 dfs(row 1, sum board[row][c]); // 回溯取消标记 col[c] false; dia1[row - c 7] false; dia2[row c] false; } } int main() { // 读入 8×8 的棋盘权值 for (int i 0; i 8; i) { for (int j 0; j 8; j) { cin board[i][j]; } } maxSum 0; // 初始化为 0 dfs(0, 0); // 从第 0 行开始放置 cout maxSum endl; return 0; }132.棋盘多项式问题描述八皇后问题是在棋盘上放皇后互相不攻击求方案。变换一下棋子还可以有八车问题八马问题八兵问题八王问题注意别念反。在这道题里棋子换成车同时棋盘也得换确切说是进行一些改造。比如现在有一张n*n的棋盘我们在一些格子上抠几个洞这些洞自然不能放棋子了会漏下去的。另外一个车本来能攻击和它的同行同列。现在你想想在攻击的过程中如果踩到一个洞便会自取灭亡。故车的攻击范围止于洞。此题给你棋盘的规模n以及挖洞情况求放k个车的方案数(k从0到最多可放车数)#include bits/stdc.h using namespace std; int n; int board[10][10]; bool colUsed[10]; int result[10]; // result[k] 表示放 k 个车的方案数 void dfs(int row, int count) { if (row n) { result[count]; return; } // 当前行不放车 dfs(row 1, count); // 尝试在当前行放车 for (int c 0; c n; c) { if (board[row][c] 1 !colUsed[c]) { colUsed[c] true; dfs(row 1, count 1); colUsed[c] false; } } } int main() { cin n; for (int i 0; i n; i) { for (int j 0; j n; j) { cin board[i][j]; } } // 初始化结果数组 for (int i 0; i n; i) result[i] 0; dfs(0, 0); // 输出从 0 到 n 的方案数 for (int i 0; i n; i) { cout result[i] endl; } return 0; }计算机英语翻译原文The Transformer model is a neural network architecture based on the attention mechanism and has achieved great success in the field of natural language processing. Unlike traditional recurrent neural networks, Transformers do not rely on step-by-step sequence processing. Instead, they use self-attention mechanisms to process the entire sequence simultaneously. This architecture not only improves the model’s ability to perform parallel computation but also enables it to capture long-range dependencies more effectively. In machine translation tasks, the Transformer model can dynamically assign attention weights according to the relationships between different words in a sentence, thereby producing more accurate translations. In addition, Transformer architectures have been widely applied to tasks such as text generation, speech recognition, and even image processing. In recent years, most large-scale pre-trained language models have been built upon the Transformer architecture, which has significantly accelerated the development of artificial intelligence technologies.翻译Transformer模型是一种基于注意力机制的神经网络架构在自然语言处理领域已经取得了重大成功。与传统的xx神经网络不同Transformer不依赖逐步序列的处理。相反它们用自注意力机制来同时处理整个序列。这种架构不仅提升了模型执行平行运算的能力还使得其能更高效地捕捉长范围的依赖。在机器翻译任务中Transformer模型能根据句子中不同词之间的关系动态分配注意力权值从而生成更准确的翻译。此外Transformer架构已广泛应用于文本生成、语音识别甚至图像处理等任务。近年来大多数大规模的预训练语言模型都基于Transformer架构构建显著加快了人工智能技术的发展。计算机英语单词扇贝打卡

相关新闻