[ABC211E] Red Polyomino

· · 题解

思路:

发现数据范围都非常的小,考虑暴力做法。

但是,由于红色方块相互连接,我们可以预测满足条件的组合数量很少。 所以可以跑枚举红色方块连接模式的 DFS(深度优先搜索)就足够了。 ### 完整代码: ```cpp #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pb push_back #define me memset #define rep(a,b,c) for(int a=b;a<=c;++a) #define per(a,b,c) for(int a=b;a>=c;--a) const int N = 1e6 + 10; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; using namespace std; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} int n,k; char s[10][10]; int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0}; ll ans; void dfs(int cnt){ if(cnt==0){ ans++; return; } vector<PII> vis; for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(s[i][j]=='.'){ bool flag=false; for(int t=0;t<4;++t){ int tx=i+dx[t],ty=j+dy[t]; if(tx>=1 && tx<=n && ty>=1 && ty<=n && s[tx][ty]=='@'){ flag=true; } } if(flag){ s[i][j]='@'; dfs(cnt-1); s[i][j]='#'; vis.pb({i,j}); } } } } for(auto it:vis){ s[it.fi][it.se]='.'; } } int main() { scanf("%d %d",&n,&k); for(int i=1;i<=n;++i){ getchar(); for(int j=1;j<=n;++j){ scanf("%c",&s[i][j]); } } for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(s[i][j]=='.'){ s[i][j]='@'; dfs(k-1); s[i][j]='#'; } } } printf("%lld\n",ans); return 0; } ```