几何加floyd 水题一个。浮点数还是看脸。
题意:在10*10的矩形中,给出一些线段,线段分割矩形,求(0,5)到(10,5)的最小距离。数据很小直接暴力。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define count sum
#define eps 0.0000001
int count = 0,n;
struct point{
double x; //X轴的下标
double y; // Y轴
};
struct line { // 直线方程
double A,B,C;
};
void Coeeficient(line &L,point a,point b) { // 两点求直线方程
L.A = a.y - b.y;
L.B = b.x - a.x;
L.C = -L.A*a.x-L.B*a.y;
}
point data[200]={}; // 点的统计
double dis(point a,point b) { // 欧式距离
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double pd(point a,point b) { // 判断直线与线段是否相交
if(fabs(a.x - b.x) <0.0000001) return 10000000;
line L;
Coeeficient(L,a,b);
for(int i = 0; i < count; i++) {
if(data[i].x > a.x && data[i].x < b.x) {
double Y = (L.A*data[i].x+L.C)/(-L.B);
if(i%4 == 0) {
if(data[i].y > Y)
return 100000000;
}
else if(i%4 == 1 ) {
if(Y > data[i].y && data[i+1].y > Y) {return 10000000;}
}
else if(i % 4 == 3)
if(Y > data[i].y) return 1000000;
}
}
return dis(a,b);
}
void solve() {
double mp[100][100]={};
for(int i = 0; i < count; i++)
for(int j = 0; j < count; j++)
mp[i][j] = 10000000;
for(int i = 0; i < count; i++)
for(int j = 0; j < count; j++) {
double dis = pd(data[i],data[j]);
mp[i][j] = dis;
}
for(int k = 0; k < count; k++)
for(int i = 0; i < count; i++)
for(int j = 0; j < count; j++)
mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
printf("%.2lf\n",mp[count-2][count-1]);
}
int main() {
while(scanf("%d",&n),n != -1) {
count = 0;
double x;
for(int i = 0; i < n; i++) {
scanf(" %lf",&x);
data[count].x = data[count+1].x = data[count+2].x = data[count+3].x = x;
for(int j = 0; j < 4; j++)
scanf(" %lf",&data[count+j].y);
count += 4;
}
data[count].x = 0; data[count++].y = 5;
data[count].x = 10; data[count++].y = 5;
solve();
}
return 0;
}