From 505d9e9c4d27d7b3e26ee1556d593d058e203bd6 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 30 Aug 2025 16:42:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P1896.cpp=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=A3=8B=E7=9B=98=E6=94=BE=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现动态规划算法解决棋盘上放置不互相攻击的国王问题。使用三维数组存储状态,通过位运算检查合法位置,并计算所有可能的放置方案总数。 --- src/8/30/P1896.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/8/30/P1896.cpp diff --git a/src/8/30/P1896.cpp b/src/8/30/P1896.cpp new file mode 100644 index 0000000..98a4162 --- /dev/null +++ b/src/8/30/P1896.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using ll = int64_t; + +ll n,K; +std::vector line; +std::vector>> dp; + +static inline constexpr ll get1n(ll x){ + return __builtin_popcount(x); +} + +int main(){ + std::iostream::sync_with_stdio(false); + std::cin.tie(nullptr); + + std::cin>>n>>K; + line.reserve(n*n+1); + line.push_back(0); + for(ll msk=0;msk<(1ll<>i)&1){ + if((msk>>(i-1))&1 || msk>>(i+1)&1){ + goto nxt; + } + } + } + line.push_back(msk); + nxt:; + } + // for(ll i:line){ + // char buff[10]; + // memset(buff, 0, sizeof(buff)); + // itoa(i, buff, 2); + // printf("%s\n",buff); + // } + dp.resize(n+1,std::vector>(line.size(),std::vector(K+1))); + + + for(ll j=1;jK)continue; + if( + line[j] & line[k] + || line[j] & (line[k]<<1) + || line[j] & (line[k]>>1) + ){ + continue; + }else{ + for(ll x=cur1n;x<=K;K++){ + dp[i][j][x]+=dp[i-1][k][x-cur1n]; + } + } + } + } + } + + ll ans=0; + for(ll j=1;j