文章目录
正文
1. 【3.3】10387 [蓝桥杯 2024 省 A] 训练士兵
题目链接:https://www.luogu.com.cn/problem/P10387
【AC_Code】
#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using ll = long long;
const int N = 1e6 + 10;
ll n, S, p[N], c[N], cnt[N], now, sum, Sum;
void solve()
{
cin >> n >> S;
for (int i = 1; i <= n; i ++)
{
cin >> p[i] >> c[i]; cnt[c[i]] += p[i]; now += p[i]; sum += p[i] * c[i];
}
for (int i = 1; i <= N; i ++)
{
if (now < S) break;
Sum += S; sum -= now; now -= cnt[i];
}
cout << Sum + sum << '\n';
}
int main()
{
IOS; solve();
return 0;
}
2. 【3.4】P8601 [蓝桥杯 2013 省 A] 剪格子
题目链接:https://www.luogu.com.cn/problem/P8601
【AC_Code】
#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int m, n, ff, a[15][15], dis[15][15], sum = 1, num = 0, b, c,
dx[5] = { 0, 1, -1, 0, 0 }, dy[5] = { 0, 0, 0, -1, 1 };
void DFS(int x, int y)
{
if (2 * num > b) return ;
if (b - num == num) { cout << sum; ff = 1; return ; }
for (int i = 1; i <= 4; i ++)
{
int ux = x + dx[i], uy = y + dy[i];
if (dis[ux][uy] == 0 && ux >= 1 && ux <= n && uy >= 1 && uy <= m)
{
dis[ux][uy] = 1; num += a[ux][uy]; sum ++;
DFS(ux, uy);
dis[ux][uy] = 0; num -= a[ux][uy]; sum --;
if (ff) return ;
}
}
return ;
}
void solve()
{
cin >> m >> n;
for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++)
{
cin >> a[i][j]; b += a[i][j]; c = max(c, a[i][j]);
}
if (2 * c == b)
{
if (a[1][1] == c) cout << 1 << '\n';
else cout << n * m - 1 << '\n';
return ;
}
dis[1][1] = 1; num = a[1][1]; DFS(1, 1);
if (!ff) cout << 0 << '\n';
}
int main()
{
IOS; solve();
return 0;
}
3. 【3.5】P9241 [蓝桥杯 2023 省 B] 飞机降落
题目链接:https://www.luogu.com.cn/problem/P9241
【AC_Code】
#include <iostream>
#include <algorithm>
#include <cstring>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int t, N, T[15], D[15], L[15], a[15];
bool DFS(int deep, int time)
{
if (deep > N) return true;
for (int i = 1; i <= N; i ++)
{
if (a[i] || T[i] + D[i] < time) continue;
a[i] = 1;
if (DFS(deep + 1, max(time, T[i]) + L[i])) { a[i] = 0; return 1; }
a[i] = false;
}
return false;
}
void solve()
{
cin >> t;
while (t --)
{
cin >> N; for (int i = 1; i <= N; i ++) cin >> T[i] >> D[i] >> L[i];
memset(a, 0, sizeof a);
if (DFS(1, 0)) cout << "YES\n";
else cout << "NO\n";
}
}
int main()
{
IOS; int _ = 1; while(_ --) solve();
return 0;
}
4. 【3.6】P10578 [蓝桥杯 2024 国 A] 旋转九宫格
题目链接:https://www.luogu.com.cn/problem/P10578
【AC_Code】
#include <iostream>
#include <queue>
#include <map>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int T; string s, tar = "123456789"; char c;
map<string, int> mp; queue<string> q;
void BFS()
{
q.push(tar); mp[tar] = 1;
while (q.size())
{
string u = q.front(); q.pop(); string v[4] = { u, u, u, u };
v[0][0] = u[1], v[0][1] = u[4], v[0][3] = u[0], v[0][4] = u[3];
v[1][1] = u[2], v[1][2] = u[5], v[1][4] = u[1], v[1][5] = u[4];
v[2][3] = u[4], v[2][4] = u[7], v[2][6] = u[3], v[2][7] = u[6];
v[3][4] = u[5], v[3][5] = u[8], v[3][7] = u[4], v[3][8] = u[7];
for (int i = 0; i < 4; i ++)
{
if (!mp[v[i]])
{
mp[v[i]] = mp[u] + 1;
if (v[i] == tar) break;
q.push(v[i]);
}
}
}
}
void solve()
{
cin >> T; BFS();
while (T --)
{
s.clear();
for (int i = 0; i < 9; i ++) cin >> c, s += c;
cout << mp[s] - 1 << '\n';
}
}
int main()
{
IOS; solve();
return 0;
}
5. 【3.7】P8642 [蓝桥杯 2016 国 AC] 路径之谜
题目链接:https://www.luogu.com.cn/problem/P8642
【AC_Code】
#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 25; bool vis[N][N];
int n, a[N], b[N], dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 }, len, ans[410];
inline void DFS(int x, int y)
{
if (x < 1 || x > n || y < 1 || y > n || vis[x][y]) return ;
for (int i = 1; i <= n; i ++) if (a[i] < 0 || b[i] < 0) return ;
vis[x][y] = true; a[y] --; b[x] --; ans[++ len] = (x - 1) * n + y - 1;
if (x == n && y == n)
{
for (int i = 1; i <= n; i ++)
{
if (a[i] != 0 || b[i] != 0) break;
if (i == n) for (int i = 1; i <= len; i ++) cout << ans[i] << ' ';
}
}
else for (int i = 0; i < 4; i ++) DFS(x + dx[i], y + dy[i]);
a[y] ++; b[x] ++; vis[x][y] = false; len --;
}
inline void solve()
{
cin >> n; for (int i = 1; i <= n; i ++) cin >> a[i]; for (int i = 1; i <= n; i ++) cin >> b[i];
DFS(1, 1);
}
int main()
{
IOS; solve(); cout << '\n';
return 0;
}
6. 【3.8】P8694 [蓝桥杯 2019 国 AC] 估计人数
题目链接:https://www.luogu.com.cn/problem/P8694
【AC_Code】
#include <iostream>
#include <vector>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 500; vector<int> g[N]; char c;
int n, m, sum, len, vis[N], f[N][N], M[N];
struct node { int x, y; } p[N];
void add(int a, int b) { g[a].push_back(b); g[b].push_back(a); }
bool go(int a)
{
for (auto n : g[a])
{
if (vis[n]) continue;
vis[n] = 1;
if ( !M[n] || go(M[n]) ) { M[n] = a; return true; }
}
return false;
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++)
{
cin >> c; if (c == '1') p[++ len] = { i, j };
}
for (int i = 1; i <= len; i ++) for (int j = i + 1; j <= len; j ++)
{
if
(
p[i].x == p[j].x && p[i].y + 1 == p[j].y
||
p[i].x + 1 == p[j].x && p[i].y == p[j].y
) f[i][j] = 1;
}
for (int k = 1; k <= len; k ++) for (int i = 1; i <= len; i ++) for (int j = 1; j <= len; j ++)
{
f[i][j] |= (f[i][k] & f[k][j]);
}
for (int i = 1; i <= len; i ++) for (int j = i + 1; j <= len; j ++) if (f[i][j]) add(i, j + len);
for (int i = 1; i <= len; i ++)
{
fill(vis, vis + 1 + len * 2, 0); if (go(i)) sum ++;
}
cout << len - sum << '\n';
}
int main()
{
IOS; solve();
return 0;
}
7. 【3.9】数字接龙
题目链接:https://www.lanqiao.cn/problems/19712/learning/?page=1&first_category_id=1&name=%E6%95%B0%E5%AD%97%E6%8E%A5%E9%BE%99
【AC_Code】
#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 15;
int n, k, nums[N][N],
dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 },
dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
string nber; bool vis[N][N], check[N][N][N][N];
bool dfs(int a, int b)
{
if (a == n - 1 && b == n - 1) return nber.size() == n * n - 1;
vis[a][b] = true;
for (int i = 0; i < 8; i ++)
{
int x = a + dx[i], y = b + dy[i];
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (vis[x][y]) continue;
if (nums[x][y] != (nums[a][b] + 1) % k) continue;
if (i % 2 && (check[a][y][x][b] || check[x][b][a][y])) continue;
check[a][b][x][y] = true; nber += i + '0';
if (dfs(x, y)) return true;
nber.pop_back(); check[a][b][x][y] = false;
}
vis[a][b] = false;
return false;
}
void solve()
{
cin >> n >> k;
for (int i = 0; i < n; i ++) for (int j = 0; j < n; j ++) cin >> nums[i][j];
if (!dfs(0, 0)) cout << -1 << '\n';
else cout << nber << '\n';
}
int main()
{
IOS; solve();
return 0;
}
结语
感谢您的阅读!期待您的一键三连!欢迎指正!