传送门
题意
给你 n × n n × n n×n的单元矩阵,其中各单元属性要么是白要么是黑,现在需要你计算出有多少种方案可以将白块绘制成红块,使得这个区域连通且大小为 k k k。
分析
这个数据范围大概就是用
d
f
s
dfs
dfs去爆搜了,关键怎么去剪枝
我们可以去用
m
a
p
map
map存一下这个图有没有被搜索过,然后进行剪枝即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
vector<string> s;
map<vector<string>, int> M;
int ans;
int n, m;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int num) {
if (M[s]) return;
M[s]++;
if (!num) {
ans++;
return;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (s[i][j] == '.') {
for (int k = 0; k < 4; k++) {
int a = i + dx[k], b = j + dy[k];
if (a >= 0 && a < n && b >= 0 && b < n && s[a][b] == 'X') {
s[i][j] = 'X';
dfs(num - 1);
s[i][j] = '.';
}
}
}
}
void solve() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (s[i][j] == '.') {
s[i][j] = 'X';
dfs(m - 1);
s[i][j] = '.';
}
}
}
int main() {
read(n), read(m);
for (int i = 0; i < n; i++) {
string x;
cin >> x;
s.pb(x);
}
solve();
di(ans);
return 0;
}