Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <unordered_map>
- #include <queue>
- using namespace std;
- const short max_size = 1000;
- short n;
- vector<string> words;
- unordered_map<string, short> mapa;
- short cnt_words(const vector<string> & c) {
- short res = 0;
- unordered_map<string, short> cnt_map;
- for(short i = 0; i < n; i++) {
- string s = "";
- for(short j = 0; j < n; j++) {
- s += c[i][j];
- }
- cnt_map[s]++;
- }
- for(short j = 0; j < n; j++) {
- string s = "";
- for(short i = 0; i < n; i++) {
- s += c[i][j];
- }
- cnt_map[s]++;
- }
- for(pair<string, short> p : mapa) {
- res += min(p.second, cnt_map[p.first]);
- }
- return res;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- short k;
- cin >> n >> k;
- for(short i = 0; i < k; i++) {
- string s;
- cin >> s;
- words.push_back(s);
- mapa[s]++;
- }
- map<vector<string>, bool> map_visited;
- vector<string> v(n);
- string S = "";
- for(int i = 0; i < n; i++) {
- S += ".";
- }
- for(int i = 0; i < n; i++) {
- v[i] = S;
- }
- queue<vector<string>> q;
- queue<short> q_vis;
- q_vis.push(0);
- q.push(v);
- short res = 0;
- vector<string> tmp;
- while(!q.empty()) {
- vector<string> c = q.front();
- q.pop();
- short visited = q_vis.front();
- q_vis.pop();
- short cnt = 0;
- for(short i = 0; i < n; i++) {
- for(short j = 0; j < n; j++) {
- if(c[i][j] == '.') {
- cnt++;
- break;
- }
- }
- if(cnt > 0) {
- break;
- }
- }
- if(cnt == 0 and cnt_words(c) >= 2 * n) {
- res++;
- // for(short i = 0;i < n; i++) {
- // for(short j = 0; j < n; j++) {
- // cout << c[i][j];
- // }
- // cout << endl;
- // }
- // cout << endl;
- continue;
- }
- if(cnt == 0) {
- continue;
- }
- for(short w = 0; w < k; w++) {
- if(!(visited & (1 << w))) {
- for(short i = 0; i < n; i++) {
- bool check = true;
- for(short j = 0; j < n; j++) {
- if(c[i][j] != '.' and c[i][j] != words[w][j]) {
- check = false;
- break;
- }
- }
- if(check) {
- tmp = c;
- for(short j = 0; j < n; j++) {
- tmp[i][j] = words[w][j];
- }
- if(!map_visited[tmp]) {
- q.push(tmp);
- q_vis.push(visited | (1 << w));
- map_visited[tmp] = true;
- }
- }
- }
- for(short j = 0; j < n; j++) {
- bool check = true;
- for(short i = 0; i < n; i++) {
- if(c[i][j] != '.' and words[w][i] != c[i][j]) {
- check = false;
- break;
- }
- }
- if(check) {
- tmp = c;
- for(short i = 0; i < n; i++) {
- tmp[i][j] = words[w][i];
- }
- if(!map_visited[tmp]) {
- map_visited[tmp] = true;
- q.push(tmp);
- q_vis.push(visited | (1 << w));
- }
- }
- }
- }
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement