题解分享
题解分享简介
扫雷(编程题) - 题解
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
struct circle{int x,y,r;};
bool check(circle c,pii p){
int t1 = (c.x - p.first)*(c.x-p.first);
int t2 = (c.y-p.second)*(c.y-p.second);
return c.r >= sqrt(t1+t2);
}
int main() {
int n,m;
cin >> n >> m;
map<pii,pii> mp;//圆心,表示最大半径和对应数量
//处理炸弹
for(int i =1;i <=n;i++){
int x,y,r;cin >> x >> y >> r;
pii p ={x,y};
mp[p].second++;
mp[p].first = max(mp[p].first,r);//最大半径
}
//处理火箭弹
queue<circle> q;
for(int i =1;i <=m;i++){
int x,y,r;cin >> x >> y >>r;
q.push({x,y,r});
}
//记录爆炸坐标
set<pii> s;
//记录答案即爆炸数量
int ans = 0;
//队列,图的搜索
while(!q.empty()){
circle cur = q.front();q.pop();
int x = cur.x,y = cur.y,r = cur.r;
//以i,j为中心,长宽均为2r的正方形范围,正方形更方便
for(int i =x-r;i <= x+r;i++){
for(int j = y-r;j <= y+r;j++){
pii p ={i,j};
//已经爆炸,不处理
if(s.count(p))continue;
//该坐标无雷,不处理
if(!mp.count(p))continue;
//不在范围
if(!check(cur,p))continue;
s.insert(p);
ans +=mp[p].second;
q.push({i,j,mp[p].first});
}
}
}
cout << ans <<endl;
return 0;
}
```
查看全文
0
0
0
2
扫雷(编程题) - 题解
```cpp
// https://dashoj.com/d/lqbproblem/p/89
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
ll n, m, ans = 0;
map<PLL, PLL> g; //
map<PLL, ll> cnt;
queue<pair<PLL, PLL>> qp; //
int main() {
// in
cin >> n >> m;
for (ll i = 0; i < n; i++) {
ll x1, y1, r1;
cin >> x1 >> y1 >> r1;
if (g.count({x1, y1})) g[{x1, y1}] = {max(g[{x1, y1}].first, r1), 1};
else g[{x1, y1}] = {r1, 1};
cnt[{x1, y1}]++;
}
for (ll i = 0; i < m; i++) {
ll x2, y2, r2;
cin >> x2 >> y2 >> r2;
qp.push({{x2, y2}, {r2, 1}});
while (!qp.empty()) { //
pair<PLL, PLL> node = qp.front();
qp.pop();
ll x = node.first.first, y = node.first.second, r = node.second.first;
for (ll xx = x - r; xx <= x + r; xx++)
for (ll yy = y - r; yy <= y + r; yy++) {
if (g.count({xx, yy}) && g[{xx, yy}].second && (xx - x) * (xx - x) + (yy - y) * (yy - y) <= r * r) {
g[{xx, yy}].second = 0;
qp.push({{xx, yy}, {g[{xx, yy}].first, 0}});
ans+= cnt[{xx, yy}];
}
}
}
}
cout << ans << endl;
return 0;
}
```
查看全文
0
0
0
0



