Luogu2665[USACO08FEB] 连线游戏

原题链接:https://www.luogu.org/problemnew/show/P2665

连线游戏

题目背景

Farmer John最近发明了一个游戏,来考验自命不凡的贝茜。

题目描述

Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤ Yi ≤ 1,000).

Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.

游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i个点 的横、纵坐标分别为X_i和Y_i (-1,000 <= X_i <=1,000; -1,000 <= Y_i <= 1,000)。 贝茜可以选两个点画一条过它们的直线,当且仅当平面上不存在与画出直线 平行的直线。游戏结束时贝茜的得分,就是她画出的直线的总条数。为了在游戏 中胜出,贝茜找到了你,希望你帮她计算一下最大可能得分。

输入输出格式
输入格式:

第1行: 输入1个正整数:N

第2..N+1行: 第i+1行用2个用空格隔开的整数X_i、Y_i,描述了点i的坐标

输出格式:

第1行: 输出1个整数,表示贝茜的最大得分,即她能画出的互不平行的线段数

输入输出样例
输入样例#1:

4
-1 1
-2 0
0 0
1 1

输出样例#1:

4  

说明

贝茜能画出以下4种斜率的直线:-1,0,1/3以及1。

题解

巨水,只需要O(n^2)枚举所有点算出所有可能的斜率,最后扫描一边去重+统计即可AC

注意垂直的情况,算斜率的时候会除以0然后RE

代码
#include<bits/stdc++.h>
#define db double
using namespace std;
struct pt{int x,y;};
pt hh[205];
db ang[40005];
int main()
{
    int n,top=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    scanf("%d%d",&hh[i].x,&hh[i].y);
    for(int i=1;i<=n;++i)
    for(int j=i+1;j<=n;++j)
    {
        if(hh[i].x==hh[j].x) ang[++top]=1e9;
        else ang[++top]=1.0*(hh[j].y-hh[i].y)/(hh[j].x-hh[i].x);
    }
    sort(ang+1,ang+1+top);
    for(int i=1;i<=top;++i)
    if(fabs(ang[i]-ang[i-1])>1e-8) ans++;
    printf("%d",ans);
    return 0;
}
### 关于 USACO 2008 FEB Meteor Shower S 的解题思路 #### 题目概述 题目描述了一种场景,在给定的间内有若干颗流星会坠落在特定位置并影响周围的区域。贝茜需要找到一条安全路径逃脱,避免被流星击中的同尽快到达目的地。 #### 解决方法分析 对于此问题的一种有效解决方案是采用广度优先搜索算法(BFS)[^1]。BFS能够帮助探索所有可能的安全移动路线,并记录下最短间内的逃生路径。具体实现如下: - **初始化队列**:将起点加入到待处理节点列表中。 - **状态表示**:每个状态由当前位置(x,y)及刻t组成。 - **扩展操作**:从当前状态下尝试向四个方向移动;如果目标格子未受流星影响,则将其作为新的状态压入队列继续考察。 - **终止条件**:一旦发现可以跳出地图边界的位置即视为成功逃离。 为了提高效率,还需要注意以下几点优化措施: - 使用集合来快速判断某个位置是否已经被访问过; - 提前计算好每颗流星的影响范围及其生效间段,以便高效查询某刻某处是否安全[^4]。 ```python from collections import deque def bfs(start_x, start_y, meteors): directions = [(0,-1), (-1,0), (0,1), (1,0)] # 初始化受影响间和坐标表 affected_times = [[set() for _ in range(305)] for __ in range(305)] for t,x,y in meteors: for dx,dy in ((0,0),(0,-1),(-1,0),(0,1),(1,0)): nx,ny=x+dx, y+dy if 0<=nx<305 and 0<=ny<305: affected_times[nx][ny].add(t) queue = deque([(start_x,start_y,0)]) visited = set() while queue: cx,cy,time=queue.popleft() if not (0 <= cx < 305 and 0 <= cy < 305): return time if (cx,cy,time) in visited: continue visited.add((cx,cy,time)) next_time=time+1 for dx, dy in directions: nx=cx+dx; ny=cy+dy if next_time not in affected_times[nx][ny]: queue.append((nx,ny,next_time)) return "IMPOSSIBLE" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值