代码均为自做
题目一
在直角坐标系中有若干线段,有的线段回和其他线段的某一段点重合,即某一段点的坐标相同,所以这些线段会形成含两条或者两条以上线段的折线。求若干折线中,含有线段条数最多的折线中包含的线段数目,并输出该折线最左端的坐标。
输入
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