3.17打卡day31
130. 2n皇后问题
问题描述
给定一个n*n的棋盘,棋盘中有一些位置不能放皇后。现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行、同一列或同一条对角线上,任意的两个白皇后都不在同一行、同一列或同一条对角线上。
问总共有多少种放法?
n小于等于8。
说明:同一条对角线是指包括两条主对角线的所有对角线,n=5时的棋盘从左上往右下有9条对角线,从右上往左下也有9条对角线。
比如,棋盘为:
1 1 1 1
1 1 1 1
1 1 1 1
1 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架构构建,显著加快了人工智能技术的发展。
