8.27 网易互娱笔试
第一题:
int main() { int c; cin >> c; while (c--) { int n, m; cin >> n >> m; vector<string> v(n, string(n, ' ')); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> v[i][j]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { putchar(v[(abs(i - m / 2) % n + n / 2) % n][(abs(j - m / 2) % n + n / 2) % n]); } putchar('\n'); } putchar('\n'); } }
第三题 :总共有28种小图案,dfs然后用位图记录图案是否出现,放到unordered_set去重
#include <iostream> #include <vector> #include <unordered_set> #include <assert.h> using namespace std; void add(long long& flag, int a, int b) { if (a > b) swap(a, b); assert(a < b); if (a == 1) { switch (b){ case 2: flag |= 1; break;// case 3: flag |= 3; break;// case 4: flag |= 1 << 6; break;// case 5: flag |= 1 << 12; break; case 6: flag |= 1 << 20; break; // case 7: flag |= (1 << 6) | (1 << 9); break;// case 8: flag |= 1 << 24; break; // case 9: flag |= (1 << 12) | (1 << 15); break; // default: assert(0); } } if (a == 2) { switch (b){ case 3: flag |= 2; break;// case 4: flag |= 1 << 16; break; // case 5: flag |= 1 << 7; break;// case 6: flag |= 1 << 13; break; // case 7: flag |= 1 << 26; break; // case 8: flag |= (1 << 7) | (1 << 10); break;// case 9: flag |= 1 << 25; break; // default: assert(0); } } if (a == 3) { switch (b){ case 4: flag |= 1 << 22; break; // case 5: flag |= 1 << 17; break; // case 6: flag |= 1 << 8; break;// case 7: flag |= (1 << 17) | (1 << 18); break; // case 8: flag |= 1 << 27; break; // case 9: flag |= (1 << 8) | (1 << 11); break;// default: assert(0); } } if (a == 4) { switch (b){ case 5: flag |= 1 << 2; break;// case 6: flag |= (1 << 2) | (1 << 3); break;// case 7: flag |= 1 << 9; break;// case 8: flag |= 1 << 14; break; // case 9: flag |= 1 << 21; break; // default: assert(0); } } if (a == 5) { switch (b){ case 6: flag |= 1 << 3; break;// case 7: flag |= 1 << 18; break;// case 8: flag |= 1 << 10; break;// case 9: flag |= 1 << 15; break; // default: assert(0); } } if (a == 6) { switch (b){ case 7: flag |= 1 << 23; break;// case 8: flag |= 1 << 19; break; // case 9: flag |= 1 << 11; break;// default: assert(0); } } if (a == 7) { switch (b){ case 8: flag |= 1 << 4; break;// case 9: flag |= (1 << 4) | (1 << 5); break;// default: assert(0); } } if (a == 8) { switch (b){ case 9: flag |= 1 << 5; break;// default: assert(0); } } } void dfs(vector<int>& v, int idx, int n, long long flag, unordered_set<long long>& uset) { if (idx > 1) { add(flag, v[idx - 1], v[idx - 2]); uset.insert(flag); } if (idx == n) return; for (int i = idx; i < n; i++) { swap(v[i], v[idx]); dfs(v, idx + 1, n, flag, uset); swap(v[i], v[idx]); } } int main() { int a; cin >> a; int n; while (a--) { vector<int> v; for (int i = 0; i < 9; i++) { char c; cin >> c; if (c == '.') v.push_back(i + 1); } unordered_set<long long> uset; dfs(v, 0, v.size(), 0, uset); cout << uset.size() << endl; } }