Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
- template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
- int n, k;
- char g[8][8];
- int dx[] {1, -1, 0, 0};
- int dy[] {0, 0, 1, -1};
- map<vector<pair<int,int>>,int>dp;
- vector<pair<int,int>>v;
- bool valid(int i, int j) {
- return i>-1 && j>-1 && i<n && j<n && g[i][j] == '.';
- }
- void solve(int i, int j) {
- g[i][j] = 'r';
- v.push_back(make_pair(i, j));
- vector<pair<int,int>> v2 = v;
- sort(v2.begin(), v2.end());
- if(dp.count(v2)) {
- v.pop_back();
- g[i][j] = '.';
- return;
- }
- dp[v2] = 1;
- if((int)v.size()==k) {
- v.pop_back();
- g[i][j] = '.';
- return;
- }
- int sz = v.size();
- for(int th=0; th<sz; ++th) {
- int xx = v[th].first, yy = v[th].second;
- for(int l=0; l<4; ++l) {
- int nx = xx + dx[l], ny = yy + dy[l];
- if(valid(nx, ny))
- solve(nx, ny);
- }
- }
- v.pop_back();
- g[i][j] = '.';
- }
- void PlayGround() {
- cin >> n >> k;
- for(int i=0; i<n; ++i) {
- for(int j=0; j<n; ++j) {
- cin >> g[i][j];
- }
- }
- for(int i=0; i<n; ++i) {
- for(int j=0; j<n; ++j) if(g[i][j]=='.') {
- solve(i, j);
- }
- }
- int ans = 0;
- for(auto it : dp) {
- ans += (int)it.first.size()==k;
- }
- cout << ans << '\n';
- #ifndef ONLINE_JUDGE
- cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
- #endif
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- PlayGround();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement