2018北航机试

代码均为自做

题目一

在直角坐标系中有若干线段,有的线段回和其他线段的某一段点重合,即某一段点的坐标相同,所以这些线段会形成含两条或者两条以上线段的折线。求若干折线中,含有线段条数最多的折线中包含的线段数目,并输出该折线最左端的坐标。

输入
9
1 3 3 1
2 3 3 4
3 4 6 3
3 1 5 2
4 2 5 2
4 2 5 4
7 3 8 4
8 4 9 0
9 0 10 0

输出
4: (1,3) (3,1)
我猜是这样输出,具体是什么格式我也不知道

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int N;
struct segment{
   
    int srcx, dstx; // x坐标
    int srcy, dsty; // y坐标
    int segCnt; // 折线的最大联通条数
    bool updatesegCnt; // 更新标记,dfs后会得到联通的线段条数,然后再更新线段条数
};
bool isConnect(const segment &a, const segment &b) {
    // 两条线段是否联通
    bool ret = false;
    if (a.dstx == b.srcx && a.dsty == b.srcy) ret = true;
    else if (a.dstx == b.dstx && a.dsty == b.dsty) ret = true;
    else if (a.srcx == b.srcx && a.srcy == b.srcy) ret = true;
    else if (a.srcx == b.dstx && a.srcy == b.dsty) ret = true;
    return ret;
}
void dfs(segment seg[], bool visit[], int index, int &cnt) {
    // 深搜遍历联通的线段
    cnt++;
    visit[index] = true;
    for (int i = 0; i < N; i++) {
   
        if (visit[i] == false && isConnect(seg[index], seg[i])) {
   
            dfs(seg, visit, i, cnt);
        }
    }
}
void updateSegCnt(segment seg[], bool visit[], const int &cnt) {
    // 更新线段条数
    for (int i = 0; i < N; i++) {
   
        if (visit[i] == true && seg[i].updatesegCnt 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值