题解分享
题解分享简介
扫雷
```cpp
// 请把代码粘贴在这里
```
include
using namespace std;
typedef pair
pii;
// 自定义哈希函数,用于 unordered_map 支持 pair
struct HashPair {
size_t operator()(const pii& p) const {
return (size_t)p.first 1000000007 + p.second;
}
};
// 存储:坐标 -> {最大半径, 数量}
unordered_map
, HashPair> mp;
// 记录已引爆的坐标
unordered_set
visited;
int n, m;
int ans = 0;
// 深度优先搜索
void dfs(int x, int y, int r) {
// 扫描正方形范围:[x-r, x+r] [y-r, y+r]
// 题目保证 r
(long long)r r) continue;
// --- 命中 ---
visited.insert(p); // 标记爆炸
ans += mp[p].second; // 累加该坐标的数量
// 递归:用这个新炸的最大半径继续引爆
dfs(i, j, mp[p].first);
}
}
}
int main() {
// 优化 IO
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
// 1. 读入
for (int i = 0; i
> x >> y >> r;
pii p = {x, y};
// 如果同一点有多个,保留数量,半径取最大值
if (mp.count(p)) {
mp[p].second++;
mp[p].first = max(mp[p].first, r);
} else {
mp[p] = {r, 1};
}
}
// 2. 读入火箭并触发 DFS
for (int i = 0; i
> x >> y >> r;
// 火箭也是引爆源,直接开始 DFS
dfs(x, y, r);
}
cout << ans << endl;
return 0;
}
查看全文
1
0
0
14
扫雷(编程题) - 题解
```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
495
扫雷(编程题) - 题解
```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
20



