#include<bits/stdc++.h> usingnamespace std; vector<int>path; int res = 0; int n; // 相当于对一个路线在加上一个新的皇后后形成的图 vector<vector<int>> build(vector<vector<int>> road) { int x = path.size() - 1; int y = path[x]; for (int i = x + 1; i < n; i++) { road[i][y] = 1; } for (int i = x + 1, j = y + 1; i < n && j < n; i++, j++) { road[i][j] = 1; } for (int i = x + 1, j = y - 1; i < n && j >= 0; i++, j--) { road[i][j] = 1; } return road; }
voidsay(){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (path[i] == j) { cout << "Q"; } else { cout << "."; } } cout << endl; } cout << endl;
} voiddfs(vector<vector<int>> road){ if (path.size() == n) { say(); return; }; int cur_x = path.size(); for (int i = 0; i < n; i++) { if (road[cur_x][i] != 1) { path.push_back(i); dfs(build(road)); path.pop_back(); } }
// 这个函数的目的是以某个点做树的根节点算出他的节点数 intbuild(int x){ val[x] = 1; for (int i = f[x]; i != 0; i = ne[i]) { if (val[e[i]] == 0) { val[x] += build(e[i]); } } return val[x]; }
intmain(){ int n; cin >> n; int a, b; for (int i = 1; i < n; i++) { cin >> a >> b; insert(a, b); insert(b, a); } build(a); int ans = INT_MAX;
for (int i = 1; i <= n; i++) { int cnt = INT_MIN; // 这道题的本质就是计算某个点作为树节点 他的左右孩子节点的权值 和总权值出去他后最大权值的最小值 for (int b = f[i]; b != 0; b = ne[b]) { if (val[e[b]] > val[i]) { cnt = max(cnt, n - val[i]); } else { cnt = max(cnt, val[e[b]]); }
#include<bits/stdc++.h> usingnamespace std; #define PII pair<int, int> intmain(){ int len = 0; int n, m; cin >> n >> m; int nextRoad[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<vector<int>>road(n + 1, vector<int>(m + 1, 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> road[i][j]; } }
queue<PII> que; que.push({1, 1}); while (!que.empty()) { int size = que.size(); for (int i = 0; i < size; i++) { PII cur = que.front(); que.pop(); if (cur.first == n && cur.second == m) { cout << len << endl; return0; }
for (int i = 0; i < 4; i++) { PII ne = {cur.first + nextRoad[i][0], cur.second + nextRoad[i][1] }; if (ne.first <= n && ne.first > 0 && ne.second <= m && ne.second > 0 && road[ne.first][ne.second] == 0) { que.push(ne); road[ne.first][ne.second] = 1; } }
#include<bits/stdc++.h> usingnamespace std; /* 本体的关注点应该在如何去交换,如何判断是否出现过上,考虑到vector的是否存在过判断耗时,我决定采用string,我们先声明一个队列(ps:用于广搜)一个set(ps:用于判断是否出现过),考虑到每一次交换都要先找出x的位置可能会使耗时*8我们可以额外申请一个存储x位置的空间与字符串进行组合,剩下的就是广搜的思路了注意这里交换的化要考虑是否满足交换条件(ps,如果x向左交换需要判断左侧是否有数据)。 */ boolisValid(string num){ for (int i = 1; i <= 8; i++) { if (num[i] != i + '0') returnfalse; } returntrue; }
string myswap(string s, int aim, int from){ string get = s; swap(get[aim], get[from]); char aim_x = '0' + aim; get[s.size() - 1] = aim_x; return get; } intmain(){ set<string> happen; queue<string>que; string now; now = " "; int len = 0; char x_index; for (int i = 1; i <= 9; i++) { char s;cin>>s; now += s; if (s == 'x') { x_index = i + '0'; } } now += x_index; happen.insert(now); que.push(now);
while (!que.empty()) { int size = que.size();
for (int i = 0; i < size; i++) { string front = que.front(); que.pop(); if (isValid(front)) { cout << len << endl; return1; } int index = front[front.size() - 1] - '0';
// 上 if (index > 3) { string get = myswap(front, index - 3, index);
if (!happen.count(get)) { happen.insert(get); que.push(get); } } // 下 if (index < 7) { string get = myswap(front, index + 3, index); if (!happen.count(get)) { happen.insert(get); que.push(get); } } // 左 if (index % 3 != 1) { string get = myswap(front, index - 1, index); if (!happen.count(get)) { happen.insert(get); que.push(get); } } // 右 if (index % 3 != 0) { string get = myswap(front, index + 1, index); if (!happen.count(get)) { happen.insert(get); que.push(get); } } } len++;
/* 用一个字符串相等绝杀我的函数 */ string end = "12345678x"; while (q.size()) { auto t = q.front(); q.pop();
if (t == end) return d[t];
int distance = d[t]; // 用一个find导致我开辟的空间像个傻子 int k = t.find('x'); // 用一个坐标转化加·数字让我的上下左右像个呆瓜 int x = k / 3, y = k % 3; for (int i = 0; i < 4; i ++ ) { int a = x + dx[i], b = y + dy[i]; if (a >= 0 && a < 3 && b >= 0 && b < 3) { swap(t[a * 3 + b], t[k]); if (!d.count(t)) { d[t] = distance + 1; q.push(t); } swap(t[a * 3 + b], t[k]); } } }
return-1; }
intmain() { char s[2];
string state; for (int i = 0; i < 9; i ++ ) { cin >> s; state += *s; }