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};
- set<vector<pair<int,int>>>s;
- vector<pair<int,int>>v;
- bool valid(int i, int j) {
- return i>-1 && j>-1 && i<n && j<n && g[i][j] == '.';
- }
- void backtrack(int i, int j) {
- g[i][j] = 'r';
- v.push_back(make_pair(i, j));
- if(v.size()==k) {
- vector<pair<int,int>>v2 = v;
- sort(v2.begin(), v2.end());
- s.insert(v2);
- v.pop_back();
- g[i][j] = '.';
- return;
- }
- for(auto it : v) {
- int x = it.first, y = it.second;
- for(int k=0; k<4; ++k) {
- int nx = x + dx[k], ny = y + dy[k];
- if(valid(nx, ny))
- backtrack(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]=='.') {
- backtrack(i, j);
- }
- }
- cout << s.size() << '\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