题解:P2225 [HNOI2001] 棋盘变换
看到
怎么做?
显然不符合实际。我们充分发挥人类智慧,当第一行确定后,后面的也可以依次推出来(依次指的是从上往下),因为当考虑第
至于本质不同,显然是做哈希。但是 map 常数较大,我们可以尝试将 bool 数组直接判断,本地运行应该不会炸。
时间复杂度
:::success[code] 什么?给你表?怎么可能?还是自己写一份跑吧。 :::warning[该代码为打表代码]
#include <bits/stdc++.h>
#pragma GCC optimize(1,2,3,"Ofast","inline")
using namespace std;
int n,ans,a[31],b[31][31],sum[31][31],t[100000005],cnt;
bool pd[536870913];
bool check(){
memset(sum,0,sizeof sum);
for(int i = 1;i<=n;i++) b[1][i]=a[i],sum[1][i-1]+=a[i],sum[1][i+1]+=a[i];
for(int i = 2;i<=n;i++)
for(int j = 1;j<=n;j++)
b[i][j]=sum[i-1][j]%2!=b[i-1][j],sum[i][j-1]+=b[i][j],sum[i][j+1]+=b[i][j],sum[i][j]+=b[i-1][j];
for(int i = 1;i<=n;i++) if(sum[n][i]%2!=b[n][i]) return 0;
int tmp=0;
for(int i = 1;i<=n;i++) tmp=(tmp<<1)+b[1][i];
if(pd[tmp]) return 0;
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = n;i>=1;i--) tmp=(tmp<<1)+b[1][i];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = 1;i<=n;i++) tmp=(tmp<<1)+b[i][1];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = n;i>=1;i--) tmp=(tmp<<1)+b[i][1];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = 1;i<=n;i++) tmp=(tmp<<1)+b[i][n];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = n;i>=1;i--) tmp=(tmp<<1)+b[i][n];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = 1;i<=n;i++) tmp=(tmp<<1)+b[n][i];
pd[tmp]=1;
t[++cnt]=tmp;
tmp=0;
for(int i = n;i>=1;i--) tmp=(tmp<<1)+b[n][i];
pd[tmp]=1;
t[++cnt]=tmp;
return 1;
}
void dfs(int i){
if(i>n){
ans+=check();
return;
}
a[i]=1;
dfs(i+1);
a[i]=0;
dfs(i+1);
}
int main(){
for(int i = 1;i<=30;i++){
n=i;
ans=0;
while(cnt) pd[t[cnt--]]=0;
dfs(1);
cout<<ans<<',';
}
return 0;
}