小奇画画

5725: 小奇画画

时间限制: 1 Sec  内存限制: 128 MB
提交: 873  解决: 209
[提交] [状态] [讨论版] [命题人:admin]

题目描述

红莲清泪两行欲吐半点却无
如初是你杳然若绯雾还在水榭畔画楼处
是谁衣白衫如初谁红裳如故
——《忆红莲》

小奇想画几朵红莲,可惜它刚开始学画画,只能从画圆开始。小奇画了n个圆,它们的圆心都在x轴上,且两两不相交(可以相切)。现在小奇想知道,它画的圆把画纸分割成了多少块?(假设画纸无限大)

 

输入

第一行包括1个整数n。
接下来n行,每行两个整数x,r,表示小奇画了圆心在(x,0),半径为r的一个圆。

 

输出

输出一个整数表示答案。

 

样例输入

4 
7 5 
-9 11 11 9 
0 20

 

样例输出

6

 

提示

对于 100%数据,1<=n<=300000,-10^9<=x<=10^9,1<=r<=10^9。

 

训练赛时没想出来这题,一碰到跟图有关的就自动挂掉,后来学了一个办法,两层循环,判断如果有大圆里包围几个小圆把该大圆上下分割成两块时,答案就加一,一开始ans=n+1

本来是A了,结果后来加数据重判,wa了

后来看大佬博客

大概思想还是和原来一样,但是这次用map来确定是否有重合,(这让一个STL勉强只会栈 队列的人瑟瑟发抖)

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=3e5+100;
pair<ll,ll> s[maxx];
map<ll,bool> f;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++){
        scanf("%lld%lld",&s[i].first,&s[i].second);
    }
    sort(s+1,s+n+1,greater<pair<ll,ll> >());
    int ans=1;
    ll x,r;
    for(int i=1; i<=n; i++){
        x=s[i].first;
        r=s[i].second;
        ans++;
        if(f[x-r] && f[x+r]) ans++;
        else f[x-r]=f[x+r]=true;
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/renxiaomiao/p/9642676.html

这个问题可以使用广度优先搜索算法(BFS)来解决。具体步骤如下: 1. 创建一个队列,初始时将起点加入队列。 2. 创建一个二维数组dist,用于记录每个位置到起点的最短距离。初始时将起点的距离设为0,其余位置的距离设为无穷大。 3. 进入循环,每次从队列中取出一个位置,遍历其上下左右四个方向的相邻位置,如果相邻位置可达且未被访问过,则将其加入队列,并更新其距离为当前位置的距离+1。 4. 直到队列为空或者找到终点时退出循环。 5. 如果找到了终点,则输出起点到终点的最短距离,并可以通过dist数组反向查找最短路线。 根据题目给出的迷宫结构,可以使用以下代码实现: ```c++ #include <iostream> #include <queue> #include <cstring> using namespace std; const int N = 5; int maze[N][N] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; int dist[N][N]; bool visited[N][N]; struct Point { int x, y; Point(int _x, int _y) : x(_x), y(_y) {}; }; int bfs(Point start, Point end) { queue<Point> q; q.push(start); memset(visited, false, sizeof(visited)); visited[start.x][start.y] = true; memset(dist, 0x3f, sizeof(dist)); dist[start.x][start.y] = 0; int dx[] = { -1, 0, 1, 0 }; int dy[] = { 0, 1, 0, -1 }; while (!q.empty()) { Point cur = q.front(); q.pop(); if (cur.x == end.x && cur.y == end.y) { return dist[cur.x][cur.y]; } for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx < 0 || nx >= N || ny < 0 || ny >= N || maze[nx][ny] == 1 || visited[nx][ny]) { continue; } q.push(Point(nx, ny)); visited[nx][ny] = true; dist[nx][ny] = dist[cur.x][cur.y] + 1; } } return -1; } int main() { Point start(0, 0); Point end(N - 1, N - 1); int shortestPath = bfs(start, end); if (shortestPath == -1) { cout << "No path exists!" << endl; } else { cout << "Shortest path length: " << shortestPath << endl; // 可以通过dist数组反向查找最短路线 } return 0; } ``` 输出结果为: ``` Shortest path length: 8 ``` 即起点到终点的最短距离为8。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值