有趣的 C++ 团队 挑战赛&四月赛G03 题解
\bold{A}. 深渊,希望
题意非常明确,几乎类似于模拟。
每走一步前,都进行 BFS,找出最短路,再按照这个方向走就行。
最后,不要忘记输出 We arrived!。
题目中给出的算法有一个名字,叫做泛洪算法。
#include<bits/stdc++.h>
using namespace std;
const int N=20;
int n, nowx, nowy, tx, ty;
char mp[N][N];
bool vis[N][N];
int X[4] = {-1, 0, 1, 0};
int Y[4] = {0, 1, 0, -1};
string dir = "URDL";
map<char, int>id = {//方位名转方向编号
{'U', 0}, {'R', 1}, {'D', 2}, {'L', 3}
};
struct stt{
int x, y, dis;
char firstStep;
};
queue<stt>q;
char get(){
memset(vis, 0, sizeof vis);
while(!q.empty()) q.pop();
for(int i=0; i<4; i++)
q.push({nowx + X[i], nowy + Y[i], 1, dir[i]});
while(!q.empty())
{
stt now = q.front();
q.pop();
if(now.x == tx && now.y == ty) return now.firstStep;
if(mp[now.x][now.y] != '.') continue;
if(vis[now.x][now.y]) continue;
vis[now.x][now.y] = 1;
for(int i=0; i<4; i++)
q.push({now.x + X[i], now.y + Y[i], now.dis + 1, now.firstStep});
}
}
int main()
{
cin >>nowx >>nowy >>tx >>ty >>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
mp[i][j] = '.';
while(nowx != tx || nowy != ty){
char res = get();
cout <<res <<endl <<flush;
char x;
cin >>x;
int nx = nowx + X[id[res]], ny = nowy + Y[id[res]];
if(x == '#') mp[nx][ny] = '#';
else nowx = nx, nowy = ny;
}
cout <<"We arrived!" <<endl <<flush;
return 0;
}
\bold{B}. 路与风之哲
无论如何,总会有
于是直接枚举出所有步数的排列,再计算消耗的体力即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=12;
int n, m, k, c;
int h[N][N], d[N][N];
int pay(string way){
int x = 1, y = 1, res = 0;
for(char step : way){
int nx = x, ny = y, add;
if(step == 'R') ny ++;
else nx ++;
int ha = h[x][y], hb = h[nx][ny];
if(ha > hb) add = max(k - 2*(ha - hb), c);
else if(ha == hb) add = k;
else add = k + 3*(hb - ha);
res += add * d[x][y];
x = nx, y = ny;
}
return res;
}
signed main()
{
cin >>n >>m >>k >>c;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin >>h[i][j];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin >>d[i][j];
string way = string(m-1, 'R') + string(n-1, 'D');
int ans1 = pay(way);
string ans2 = way;
while(next_permutation(way.begin(), way.end(), greater<char>())){
int tmp = pay(way);
if(tmp < ans1) ans1 = tmp, ans2 = way;
}
cout <<ans1 <<endl <<ans2 <<endl;
return 0;
}
\bold{C.} 导角
就是一道裸的,需要维护集合大小的并查集。C 指令视为合并,Q 指令视为查询。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int p[N], sz[N];
int find(int s){
if(p[s] != s){
p[s] = find(p[s]);
sz[s] = sz[p[s]];
}
return p[s];
}
void merge(int x, int y){
int px = find(x), py = find(y);
if(px != py)
p[px] = py, sz[py] += sz[px];
}
int main()
{
for(int i=1; i<N; i++)
p[i] = i, sz[i] = 1;
int q;
cin >>q;
while(q--){
char op;
int i, j;
cin >>op >>i >>j;
if(op == 'C') merge(i, j);
else if(op == 'Q'){
int px = find(i);
int py = find(j);
if(px == py) puts("YES");
else puts("NO");
cout <<sz[px]-1 <<endl;
}
}
return 0;
}
\bold{D.} 海盗分金
注:下文中的除号都指地板除法。
假设最后一个海盗的编号为
对于一共有
显然,当有
在拉拢的过程中,计算拉拢这些人所需要的金币总数。如果拉拢所需的金币总数大于 MUST DIE。
#include<bits/stdc++.h>
using namespace std;
const int N=210;
int way[N];//分法
int newway[N];
bool vis[N];//是否已经要到了第 i 个海盗的选票
int main()
{
int n, m;
cin >>n >>m;
memset(way, -1, sizeof way);//-1代表必死
way[1] = m;//倒序存储,最后分金的是 1 号
for(int i=2; i<=n; i++){
//back_up存上一个人的方案
memset(vis, 0, sizeof vis);
memset(newway, 0, sizeof newway);
int needCoin=0, needVote=i/2;
//needCoin:至少需要将多少金币分出去才能不死
//needVote:需要的选票(除了自己的票)
for(int j=1; j<=needVote; j++){
//贪心策略,获取代价最少的人的选票
int smallestId = 0, smallest = 0x3f3f3f3f;
for(int k=i-1; k>=1; k--){
if(!vis[k] && way[k] <= smallest)
smallestId = k, smallest = way[k];
}
vis[smallestId] = 1;
//cout <<smallestId <<endl;
newway[smallestId] = way[smallestId] + 1;
needCoin += newway[smallestId];
}
if(needCoin > m)
way[i] = -1;//必死
else memcpy(way, newway, sizeof way), way[i] = m - needCoin;
}
if(way[n] == -1) puts("MUST DIE");
else for(int i=n; i>=1; i--)
cout <<way[i] <<' ';
puts("");
}