题目链接
[ABC211E] Red Polyomino(luogu)
解题思路
从第三个样例可以看出总的方案数一定很少,因此我们可以直接确定第一个被染色的格子后直接向外爆搜,搜到最后可以使用哈希判重,但光凭这样的话 \(2\) 秒钟肯定跑不过去,因此我们可以在搜索的过程中使用哈希存储状态来剪枝,如果这个状态之前搜过了就不搜了。这样剪枝就足以通过此题了。
参考代码
这里使用双哈希。
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
//#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
ll n,m,ans;
ll Base1=666,Base2=223;
ll mod1=1e9+9,mod2=998244353;
ll dx[]={0,0,1,-1},dy[]={1,-1,0,0};
char a[10][10];
map<ll,map<ll,ll>>mp;
map<ll,map<ll,ll>>vis;
bool check(ll x,ll y){
return x>=1 && x<=n && y>=1 && y<=n && a[x][y]=='.';
}
void check2()
{
ll sum1=0,sum2=0;
/* forl(i,1,n)
{
forl(j,1,n)
cout<<a[i][j];
cout<<endl;
}
cout<<endl;*/
forl(i,1,n)
forl(j,1,n)
sum1*=Base1,sum1+=a[i][j]*(i*n+j),sum1%=mod1,
sum2*=Base2,sum2+=a[i][j]*(i*n+j),sum2%=mod2;
ans+=++mp[sum1][sum2]==1;
}
bool check3()
{
ll sum1=0,sum2=0;
/* forl(i,1,n)
{
forl(j,1,n)
cout<<a[i][j];
cout<<endl;
}
cout<<endl;*/
forl(i,1,n)
forl(j,1,n)
sum1*=Base1,sum1+=a[i][j]*(i*n+j),sum1%=mod1,
sum2*=Base2,sum2+=a[i][j]*(i*n+j),sum2%=mod2;
if(vis[sum1][sum2])
return 1;
vis[sum1][sum2]=1;
return 0;
}
void dfs(ll last)
{
if(check3())
return ;
if(last==0)
{
check2();
return ;
}
forl(x,1,n)
forl(y,1,n)
if(a[x][y]=='*')
forl(i,0,3)
{
ll fx=x+dx[i],fy=y+dy[i];
//cout<<fx<<","<<fy<<">>\n";
if(check(fx,fy))
a[fx][fy]='*',dfs(last-1),a[fx][fy]='.';
}
}
void solve()
{
cin>>n>>m;
forl(i,1,n)
forl(j,1,n)
cin>>a[i][j];
forl(i,1,n)
forl(j,1,n)
if(a[i][j]=='.')
a[i][j]='*',dfs(m-1),a[i][j]='.';
cout<<ans<<endl;
}
int main()
{
IOS;
t=1;
// cin>>t;
while(t--)
solve();
QwQ;
}