UVALive 7749 Convex Contour (计算几何)

题意:给定上正方形,圆,三角形,让你求出包围它的最短的路径。

析:首先,如果是这种情况  三角形 三角形 三角形 正方形(圆) 三角形 三角形 三角形 。。这一种就是直接从左边直接连到正方形(圆),也就是相切,剩下的情况都是直接是直线,只要处理一下边界就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef double lb;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const lb PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50;
const int mod = 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

char str[maxn];
double Pow(double x){  return x * x;  }

int main(){
  while(scanf("%d", &n) == 1){
    scanf("%s", str);
    int len = strlen(str);
    int change_pos = len + 1;
    int pre_T = 0, last_T = 0;
    for(int i = 0; i < n; i++){
      if(str[i] == 'T') pre_T++;
      else break;
    }
    for(int i = n - 1; i >= 0; i--){
      if(str[i] == 'T') last_T++;
      else break;
    }
    bool all_T = false;
    lb ans = 0.0;
    if(pre_T){
      lb nn = pre_T;
      int cur = pre_T;
      if(cur >= n){
        all_T = true;
        goto TT;
      }
      if(str[cur] == 'S')
        ans += sqrt(Pow(nn - 0.5) + Pow(2 - sqrt(3)) / 4) + 0.5;
      else if(str[cur] == 'C'){
        lb A = (4 * Pow(nn)) / Pow(sqrt(3) - 1.0) + 1.0;
        lb B = -(2 * nn) / Pow(sqrt(3) - 1);
        lb C = (1.0 / 4.0) / Pow(sqrt(3) - 1.0) - 1.0 / 4.0;
        lb delta = Pow(B) - 4 * A * C;
        lb x1 = (-B - sqrt(delta)) / (2 * A);
        lb y = sqrt(1.0 / 4.0 - Pow(x1));
        lb t2 = Pow(x1) + Pow(1 / 2.0 - y);
        lb ct = (1/2.0 - t2) * 2;
        lb alf = acos(ct);
        lb L = alf / 2.0 + sqrt(Pow(x1 - nn) + Pow(y - (sqrt(3) - 1.0) / 2.0));
        ans += L;
      }
    }

    if(last_T){
      lb nn = last_T;
      int cur = n - 1 - last_T;
      if (cur < 0){
        all_T = true;
        goto TT;
      }
      if (str[cur] == 'S')
        ans += sqrt(Pow(nn - 0.5) + Pow(2 - sqrt(3)) / 4) + 0.5;
      else if (str[cur] == 'C'){
        lb A = (4 * Pow(nn)) / Pow(sqrt(3) - 1.0) + 1.0;
        lb B = -(2 * nn) / Pow(sqrt(3) - 1);
        lb C = (1.0 / 4.0) / Pow(sqrt(3) - 1.0) - 1.0 / 4.0;
        lb delta = Pow(B) - 4 * A * C;
        lb x1 = (-B - sqrt(delta)) / (2 * A);
        lb y = sqrt(1.0 / 4.0 - Pow(x1));
        lb t2 = Pow(x1) + Pow(1 / 2.0 - y);
        lb ct = (1/2.0 - t2) * 2;
        lb alf = acos(ct);
        lb L = alf / 2.0 + sqrt(Pow(x1 - nn) + Pow(y - (sqrt(3) - 1.0) / 2.0));
        ans += L;
      }
    }
    TT:
    if(all_T)  ans += n - 1;
    else  ans += n - pre_T - last_T - 1;
    ans += n;
    if (str[0] == 'S') ans += 1.5;
    if (str[len - 1] == 'S') ans += 1.5;
    if (str[0] == 'C') ans += PI / 2.0 - 0.5;
    if (str[len - 1] == 'C') ans += PI / 2.0 - 0.5;
    if (str[0] == 'T') ans += 1;
    if (str[len - 1] == 'T') ans += 1;
    printf("%.10f
", (double)ans);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7454652.html