剪邮票

    科技2022-09-01  106

    #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<stack> #include<queue> using namespace std; const int M = 1 << 12 ; bool vis[12],has[12]; int ans = 0; stack<int> s; int mp[][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; int dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 }; string str[10000]; int cnt(int i) { int res = 0; for (int j = 0; j < 12; j++) res += i >> j & 1; return res; } bool bfs(int i) { if (cnt(i) != 5) return false; memset(has, 0, sizeof has); queue<int> q; int res = 1; for (int j = 0; j < 12; j++) if (i>>j&1) { q.push(j); has[j] = 1; break; } while (q.size()) { int t = q.front(); q.pop(); int x = t / 4, y = t % 4; for (int j = 0; j < 4; j++) { int nx = x + dx[j], ny = y + dy[j]; if (nx < 0 || nx>2 || ny < 0 || ny>3) continue; int tmp = nx * 4 + ny; if ( (i>>tmp&1)&& !has[tmp]) { q.push(tmp); res++; has[tmp] = 1; } } if (res == 5) return true; } return false; } int main() { int n = 0; for (int i = 0; i < M; i++) { if (bfs(i)) { for (int j = 0; j < 12; j++) if (i >> j & 1) { if (j < 10) str[n] += j + '0'; else str[n] += j - 10 + 'A'; } n++; } } sort(str, str + n); for (int i = 0; i < n; i++) cout << str[i] << endl; cout << n << endl; return 0; }

     

    Processed: 0.008, SQL: 10