Codeforces 907 矩阵编号不相邻构造 团操作状压DFS
A.
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; const double eps = 1.0e-8; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e6 + 10; const int maxm = 300; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //next_permutation ll mod = 1e9 + 7; int main() { //freopen("out1.txt", "w", stdout); int v1, v2, v3, vm; int anser; cin >> v1 >> v2 >> v3 >> vm; int flag = 0; if (vm >= v2) { cout << -1 << endl; exit(0); } for (int i = v3; i <= 2 * v3; i++) { if (i >= vm && i <= 2 * vm) { flag = 1; anser = i; break; } } if (flag) { cout << 2 * v1 << endl; cout << 2 * v2 << endl; cout << anser << endl; } else { cout << -1 << endl; } }
B.
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; const double eps = 1.0e-8; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e6 + 10; const int maxm = 300; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //next_permutation ll mod = 1e9 + 7; char f[100][100]; bool judge(int x) { int flag = 0; int cur = (x - 1) / 3; x = x % 3; if (x == 0) { x = 3; } int cur1 = x - 1; // cout << "y" << " " << cur1 * 3 + 1 << " " << cur1 * 3 + 3 << endl; // cout << "x" << " " << cur * 3 + 1 << " " << cur * 3 + 3 << endl; for (int i = cur * 3 + 1; i <= cur * 3 + 3; i++) { for (int j = cur1 * 3 + 1; j <= cur1 * 3 + 3; j++) { if (f[i][j] == '.') { f[i][j] = '!'; flag = 1; } } } if (flag) { return false; } else { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (f[i][j] == '.') { f[i][j] = '!'; } } } return true; } } int main() { //freopen("in.txt", "r", stdin); //freopen("out1.txt", "w", stdout); char now; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { scanf("%c", &now); if (now != '.' && now != 'x' && now != 'o') { j--; } else { f[i][j] = now; } } } int x, y; cin >> x >> y; x = x % 3; if (x == 0) { x = 3; } y = y % 3; if (y == 0) { y = 3; } int want = (x - 1) * 3 + y; //cout << want << endl; judge(want); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { cout << f[i][j]; if (j % 3 == 0 && j % 9 != 0) { cout << " "; } if (j % 9 == 0) { cout << endl; } } if (i % 3 == 0) { cout << endl; } } }
C.
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; const double eps = 1.0e-8; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e6 + 10; const int maxm = 300; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //next_permutation ll mod = 1e9 + 7; int flag = 0; int cur = 0; char letter = '.'; int num[30]; int test[30]; unordered_set<char> ans; int main() { //freopen("out1.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; int anser = 0; cin >> n; char a; string b; for (int i = 0; i < 26; i++) { ans.insert('a' + i); } for (int i = 1; i <= n; i++) { cin >> a >> b; if (ans.size() == 1) { if (a == '!') { anser++; } else if (a == '?') { if (b[0] == letter) { break; } else if(!ans.count(b[0])) { anser++; } } continue; } if (a == '!') { unordered_set<char> now; for (char j : b) { now.insert(j); } for (auto it = ans.begin(); it != ans.end();) { if (!now.count(*it)) { ans.erase(it++); } else { it++; } } } else { for (char j : b) { ans.erase(j); } } } cout << anser << endl; }
D.给你一个N*M的矩阵 每位的编号为(i-1)*m+j 要求你重新排列 使得重新排列后每个数与原来相邻的数互不相邻 不存在则输出-1
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; const double eps = 1.0e-8; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e6 + 10; const int maxm = 300; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //next_permutation ll mod = 1e9 + 7; int n, m; int ans[100005]; int pop = 1; int now[100005]; int f[30][30]; int num[30][30]; int cur[30][30]; int flagf = 0; stack<int> s; void dfs(int x, int y) { if (x == n * m + 1) { flagf = 1; for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { s.push(cur[i][j]); } } return ; } if (flagf) { return ; } int aimx = (x - 1) / m + 1; int aimy = (x - 1) % m + 1; for (int i = 1; i <= n * m; i++) { if ((y & (1 << i)) == 0) { if ((!num[cur[aimx - 1][aimy]][i]) && (!num[cur[aimx][aimy - 1]][i])) { int numcur = y | (1 << i); //cout << aimx << " " << aimy << " " << i << endl; cur[aimx][aimy] = i; dfs(x + 1, numcur); } } } } void solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { f[i][j] = (i - 1) * m + j; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k <= 3; k++) { int dx = i + turn[k][0]; int dy = j + turn[k][1]; if (f[dx][dy] != 0) { num[f[i][j]][f[dx][dy]] = num[f[dx][dy]][f[i][j]] = 1; } } } } dfs(1, 0); if (flagf) { cout << "YES" << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << s.top() << " "; s.pop(); } cout << endl; } } else { cout << "NO" << endl; } } int main() { //freopen("in.txt", "r", stdin); //freopen("out1.txt", "w", stdout); while (cin >> n >> m) { if (n <= 4 && m <= 4) { solve(); exit(0); } if (n < m) { printf("YES\n"); for (int i = 1; i <= n; i++) { if (i & 1) { for (int j = (i - 1) * m + 1; j <= i * m; j += 2) { cout << j << " "; } for (int j = (i - 1) * m + 2; j <= i * m; j += 2) { cout << j << " "; } cout << endl; } else { for (int j = (i - 1) * m + 2; j <= i * m; j += 2) { cout << j << " "; } for (int j = (i - 1) * m + 1; j <= i * m; j += 2) { cout << j << " "; } cout << endl; } } } else { printf("YES\n"); for (int i = 1; i <= m; i++) { if (i & 1) { now[i] = 1; } else { now[i] = 2; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ans[pop++] = (now[j] - 1) * m + j; now[j] += 2; if (now[j] > n) { now[j] = (j & 1) + 1; } //cout<<now[j]<<endl; } } for (int i = 1; i < pop; i++) { printf("%d", ans[i]); if (i % m == 0) { printf("\n"); } else { printf(" "); } } } } }
E.状压DFS
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; const double eps = 1.0e-8; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e6 + 10; const int maxm = 300; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //next_permutation ll mod = 1e9 + 7; int n, m; int num[30][30]; stack<int> cur, ans; int anser = 30; void dfs(int now) { if (cur.size() >= ans.size()) { return ; } if (now == n) { for (int i = 0; i < n; i++) { if (num[n][i] != (1 << n) - 1) { return ; } } ans = cur; return ; } memcpy(num[now + 1], num[now], sizeof(num[now])); dfs(now + 1); for (int i = 0; i < n; i++) { if ((1 << i) & num[now][now]) { num[now + 1][i] |= num[now][now]; } } cur.push(now); dfs(now + 1); cur.pop(); } int main() { //freopen("in.txt", "r", stdin); //freopen("out1.txt", "w", stdout); cin >> n >> m; int pa, pb; for (int i = 0; i < n; i++) { num[0][i] = 1 << i; //cout<<p[i]<<endl; ans.push(i); } for (int i = 1; i <= m; i++) { scanf("%d %d", &pa, &pb); pa--, pb--; num[0][pa] |= 1 << pb; num[0][pb] |= 1 << pa; } //pf(); dfs(0); cout << ans.size() << endl; while (!ans.empty()) { cout << ans.top() + 1 << " "; ans.pop(); } cout << endl; }