HDU 3272-Mission Impossible (Computational ry)

Source: Internet
Author: User
HDU 3272-Mission Impossible (Computational ry)

ACM

Question address:
HDU 3272-Mission Impossible

Question:
On a two-dimensional plane, an initial position (HX, Hy) is provided. You need to obtain four types of resources: A at any position on the X axis and B at any position on the Y axis, the C and D positions will tell you. Ask how long it takes to return (HX, Hy) after obtaining four types of resources.

Analysis:
There are three conditions for intersection of three line segments with the X and Y axes:

  1. Intersection with X and Y. The shortest distance is the perimeter.
  2. Only intersection with X or Y. In this case, each edge is enumerated, and the distance is calculated using the image symmetry, and the minimum value is obtained.
  3. It is hard to think about not intersection with X and Y.
    I naively thought that as long as each side is enumerated, it can be turned to the midpoint...
    There are two possible cases:
    First:
     
    Second:
     
    In the first case, we need to enumerate six times, and one side is responsible for removing the X axis and the other for taking the Y axis.
    In the second case, we need to enumerate three times. The top two endpoints of each edge take both the X axis and the Y axis.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        3272.cpp*  Create Date: 2014-08-15 20:56:16*  Descripton:   */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 0;struct Point {double x;double y;} h, o, p[3];double dis(const Point& a, const Point& b) {return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));}// calc h->a->b->hdouble calc(int a, int b) {double ret = dis(h, p[a]) + dis(p[a], p[b]) + dis(p[b], h);double ha, ab, bh;bool cx = 0, cy = 0;if (h.x * p[a].x <= 0 || p[a].x * p[b].x <= 0 || p[b].x * h.x <= 0)cx = 1;if (h.y * p[a].y <= 0 || p[a].y * p[b].y <= 0 || p[b].y * h.y <= 0)cy = 1;if (cx == 0 && cy == 0) {// 3Point t1, t2;t1.x = h.x;t1.y = -h.y;t2.x = -p[a].x;t2.y = p[a].y;ha = ret - dis(h, p[a]) + dis(t1, t2);t1.x = p[b].x;t1.y = -p[b].y;t2.x = -p[a].x;t2.y = p[a].y;ab = ret - dis(p[a], p[b]) + dis(t1, t2);t1.x = p[b].x;t1.y = -p[b].y;t2.x = -h.x;t2.y = h.y;bh = ret - dis(h, p[b]) + dis(t1, t2);double ans = min(ha, min(ab, bh));// 6// ha->x ab->yt1.x = p[a].x;t1.y = -p[a].y;t2.x = -p[a].x;t2.y = p[a].y;ans = min(ans, ret - dis(p[a], h) + dis(t1, h) - dis(p[a], p[b]) + dis(t2, p[b]));// ha->x bh->yt1.x = h.x;t1.y = -h.y;t2.x = -h.x;t2.y = h.y;ans = min(ans, ret - dis(p[a], h) + dis(p[a], t1) - dis(h, p[b]) + dis(t2, p[b]));// ab->x ha->yt1.x = p[a].x;t1.y = -p[a].y;t2.x = -p[a].x;t2.y = p[a].y;ans = min(ans, ret - dis(p[a], p[b]) + dis(t1, p[b]) - dis(p[a], h) + dis(t2, h));// ab->x bh->yt1.x = p[b].x;t1.y = -p[b].y;t2.x = -p[b].x;t2.y = p[b].y;ans = min(ans, ret - dis(p[a], p[b]) + dis(t1, p[a]) - dis(p[b], h) + dis(t2, h));// bh->x ha->yt1.x = h.x;t1.y = -h.y;t2.x = -h.x;t2.y = h.y;ans = min(ans, ret - dis(p[b], h) + dis(p[b], t1) - dis(h, p[a]) + dis(t2, p[a]));// bh->x ab->yt1.x = p[b].x;t1.y = -p[b].y;t2.x = -p[b].x;t2.y = p[b].y;ans = min(ans, ret - dis(h, p[b]) + dis(t1, h) - dis(p[b], p[a]) + dis(t2, p[a]));ret = ans;} else if (cx == 1 && cy == 0) {Point tmp;// tmp is x axis mirror of atmp.x = p[a].x;tmp.y = -p[a].y;ha = ret - dis(h, p[a]) + dis(h, tmp);ab = ret - dis(p[a], p[b]) + dis(tmp, p[b]);// tmp is x axis mirror of btmp.x = p[b].x;tmp.y = -p[b].y;bh = ret - dis(h, p[b]) + dis(h, tmp);ret = min(ha, min(ab, bh));} else if (cx == 0 && cy == 1) {Point tmp;// tmp is y axis mirror of atmp.x = -p[a].x;tmp.y = p[a].y;ha = ret - dis(h, p[a]) + dis(h, tmp);ab = ret - dis(p[a], p[b]) + dis(tmp, p[b]);// tmp is y axis mirror of btmp.x = -p[b].x;tmp.y = p[b].y;bh = ret - dis(h, p[b]) + dis(h, tmp);ret = min(ha, min(ab, bh));}return ret;}int main() {// orio.x = 0;o.y = 0;int t;cin >> t;while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y >> h.x >> h.y) {printf("%.2f\n", calc(0, 1));}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.