11.29 罗湖CC课上代码
2025-11-29 16:07:45
发布于:广东
观星
#include <iostream>
#include <queue>
using namespace std;
const int N = 2e3;
char mp[N][N];
bool vis[N][N];
int n, m, cnt[N * N];
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
bool inMap(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void bfs(int x, int y) {
int tot = 1;
queue<pair<int, int>> q;
vis[x][y] = true, q.push({x, y});
while(!q.empty()) {
int nowx = q.front().first, nowy = q.front().second;
q.pop();
for(int i = 0; i < 8; i++) {
int nx = nowx + dx[i], ny = nowy + dy[i];
if(inMap(nx, ny) && !vis[nx][ny] && mp[nx][ny] == '*')
vis[nx][ny] = true, q.push({nx, ny}), tot++;
}
}
cnt[tot]++;
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> mp[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(mp[i][j] == '*' && !vis[i][j]) bfs(i, j);
int ans1 = 0, ans2 = 0;
for(int i = 0; i <= n * m; i++) {
if(cnt[i]) ans1++;
ans2 = max(ans2, cnt[i] * i);
}
cout << ans1 << ' ' << ans2 << '\n';
return 0;
}
八皇后
#include <iostream>
using namespace std;
const int N = 15;
int n, sta[N], cnt;
bool check(int x, int y) {
for(int i = 1; i < x; i++) {
if(sta[i] == y) return true;
if(abs(x - i) == abs(y - sta[i])) return true;
}
return false;
}
void dfs(int now) {
if(now == n + 1) {
cnt++;
if(cnt <= 3) for(int i = 1; i <= n; i++)
cout << sta[i] << " \n"[i == n];
}
for(int i = 1; i <= n; i++) {
if(check(now, i)) continue;
sta[now] = i;
dfs(now + 1);
}
}
int main() {
cin >> n;
dfs(1);
cout << cnt << endl;
return 0;
}
考前临时抱佛脚
普通深搜
#include <iostream>
using namespace std;
int dfs(int now, int *a, int n, int l, int r) {
if(now == n) return max(l, r);
return min(dfs(now + 1, a, n, l + a[now], r),
dfs(now + 1, a, n, l, r + a[now]));
}
int main() {
int s[4], ans = 0;
for(int i = 0; i < 4; i++) cin >> s[i];
for(int i = 0; i < 4; i++) {
int a[s[i]];
for(int j = 0; j < s[i]; j++) cin >> a[j];
ans += dfs(0, a, s[i], 0, 0);
}
cout << ans << endl;
return 0;
}
二进制枚举
#include <iostream>
using namespace std;
int main() {
int s[4], ans = 0;
for(int i = 0; i < 4; i++) cin >> s[i];
for(int i = 0; i < 4; i++) {
int a[s[i]], res = 1e9;
for(int j = 0; j < s[i]; j++) cin >> a[j];
for(int k = 0; k < (1 << s[i]); k++) {
int l = 0, r = 0;
for(int j = 0; j < s[i]; j++) {
if((1 << j) & k) r += a[j];
else l += a[j];
}
res = min(res, max(l, r));
}
ans += res;
}
cout << ans << endl;
return 0;
}
填涂颜色
#include <iostream>
using namespace std;
const int N = 35;
int a[N][N], n;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
bool inMap(int x, int y) { return x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1; }
void dfs(int x, int y) {
for(int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(inMap(nx, y) && a[nx][ny] == 0)
a[nx][ny] = -1, dfs(nx, ny);
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
a[0][0] = -1, dfs(0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
if(a[i][j] == -1) a[i][j] = 0;
else if(a[i][j] == 0) a[i][j] = 2;
cout << a[i][j] << " \n"[j == n];
}
return 0;
}
小码君喝牛奶
#include <iostream>
#include <set>
using namespace std;
const int N = 25;
bool vis[N][N][N];
int a, b, c;
set<int> ans;
void dfs(int x, int y, int z) {
if(vis[x][y][z]) return;
vis[x][y][z] = true;
if(x == 0) ans.insert(z);
dfs(max(0, x + y - b), min(x + y, b), z);
dfs(min(x + y, a), max(0, x + y - a), z);
dfs(x, max(0, y + z - c), min(y + z, c));
dfs(x, min(y + z, b), max(0, y + z - b));
dfs(max(0, x + z - c), y, min(x + z, c));
dfs(min(x + z, a), y, max(0, x + z - a));
}
int main() {
cin >> a >> b >> c;
dfs(0, 0, c);
for(auto it : ans) cout << it << ' ';
return 0;
}
全部评论 4
#include <iostream>
using namespace std;
const int N = 35;
int a[N][N], n;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
bool inMap(int x, int y) { return x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1; }
void dfs(int x, int y) {
for(int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(inMap(nx, y) && a[nx][ny] == 0)
a[nx][ny] = -1, dfs(nx, ny);
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
a[0][0] = -1, dfs(0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
if(a[i][j] == -1) a[i][j] = 0;
else if(a[i][j] == 0) a[i][j] = 2;
cout << a[i][j] << " \n"[j == n];
}
return 0;
}2025-11-29 来自 广东
0
2025-11-29 来自 广东
0







































































































































































































2025-11-29 来自 广东
0
2025-11-29 来自 广东
0















有帮助,赞一个