The Doors

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 
Input
The input data for the illustrated chamber would appear as follows. 


4 2 7 8 9 
7 3 4.5 6 7 

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1. 
Output
The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.
Sample Input
1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1
Sample Output
10.00
10.06

最短路+几何

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<iomanip>
//#include<bits/stdc++.h>
//#define eps 1e-8
#define inf 0x7f
//const double inf=100;
using namespace std;
struct Point
{
    double x,y;
}point[444];
struct Line
{
    Point a,b;
}line[333];
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cross(double x1,double y1,double x2,double y2,double x,double y)
{
    return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}
double w[444][444];
double d[444];
bool v[444];
int aa(int x,int y)
{
    int u1=(y+3)/4;
    int u2=(x+3)/4;
    int flag=1;
    for(int i=u2*3+1;i<=3*u1-3;i++)
    {
        if(cross(point[x].x,point[x].y,point[y].x,point[y].y,line[i].a.x,line[i].a.y)*
           cross(point[x].x,point[x].y,point[y].x,point[y].y,line[i].b.x,line[i].b.y)<0)
        {
            flag=0;
            break;
        }
    }
    return flag;
}
int u1,u2;
void spfa()
{
    memset(v,0,sizeof(v));
    d[0]=0;
    queue<int>q;
    q.push(0);
    v[0]=1;
    while(!q.empty())
    {
        int st=q.front();
        q.pop();
        v[st]=0;
        for(int i=0;i<=u1;i++)
        {
           if(d[st]+w[st][i]<d[i])
           {
               d[i]=d[st]+w[st][i];
               if(!v[i])
               {
                   q.push(i);
                   v[i]=1;
               }
           }
        }
    }
}
int main()
{
    int n;
    double x,y1,y2,y3,y4;
    while(~scanf("%d",&n)&&n!=-1)
    {
         u1=1,u2=1;
        point[0].x=0,point[0].y=5;
       for(int i=0;i<n;i++)
       {
         cin>>x>>y1>>y2>>y3>>y4;
         point[u1].x=x,point[u1++].y=y1;
         point[u1].x=x,point[u1++].y=y2;
         point[u1].x=x,point[u1++].y=y3;
         point[u1].x=x,point[u1++].y=y4;
         line[u2].a.x=x,line[u2].a.y=0,line[u2].b.x=x,line[u2++].b.y=y1;
         line[u2].a.x=x,line[u2].a.y=y2,line[u2].b.x=x,line[u2++].b.y=y3;
         line[u2].a.x=x,line[u2].a.y=y4,line[u2].b.x=x,line[u2++].b.y=10;
       }
       point[u1].x=10,point[u1].y=5;
       memset(w,inf,sizeof(w));
       memset(d,inf,sizeof(d));
       for(int i=0;i<u1;i++)
       {
           for(int j=i+1;j<=u1;j++)
           {
               if(point[i].x==point[j].x)
                continue;
               if(aa(i,j))
                   w[i][j]=dis(point[i].x,point[i].y,point[j].x,point[j].y);
           }
       }

       spfa();
       cout<<fixed<<setprecision(2)<<d[u1]<<endl;
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that. Here is the code to simulate the Monty Hall problem: ```python import numpy as np import matplotlib.pyplot as plt def random_door(): return np.random.choice([1,2,3]) def monty_choice(contestant_door, car_door): if contestant_door == car_door: return np.random.binomial(1, 0.5) + 1 else: return 6 - contestant_door - car_door def win_car(strategy, contestant_door, car_door, monty_door): if strategy == 'switch': new_door = 6 - contestant_door - monty_door return new_door == car_door else: return contestant_door == car_door def simulation(n, strategy): car_door = random_door() contestant_door = random_door() monty_door = monty_choice(contestant_door, car_door) successes = 0 failures = 0 for i in range(n): if win_car(strategy, contestant_door, car_door, monty_door): successes += 1 else: failures += 1 return successes, failures n = 1000 switch_successes, switch_failures = simulation(n, 'switch') noswitch_successes, noswitch_failures = simulation(n, 'noswitch') fig, axs = plt.subplots(1, 2, figsize=(10, 5), sharey=True) axs[0].bar(['Switch Successes', 'Switch Failures'], [switch_successes, switch_failures]) axs[0].set_title('Switch Strategy') axs[1].bar(['No Switch Successes', 'No Switch Failures'], [noswitch_successes, noswitch_failures]) axs[1].set_title('No Switch Strategy') plt.show() ``` This code defines three functions: `random_door` to randomly select a door for the car or the contestant, `monty_choice` to choose the door Monty opens based on the contestant's choice and the location of the car, and `win_car` to determine if the contestant wins the car based on the strategy (switch or no switch), the doors chosen by the contestant and Monty, and the location of the car. The `simulation` function runs the simulation for a given strategy and number of iterations. It selects the doors for the car and the contestant, chooses the door Monty opens, and then checks if the contestant wins the car based on the chosen strategy. The code then runs the simulation for both the switch and no switch strategies, and plots the results in side-by-side bar charts. According to the simulation results, the contestant should switch doors to increase their chances of winning the car. The switch strategy has a higher probability of success than the no switch strategy. This result is consistent with the conditional probability of the problem, which shows that the probability of winning the car is 2/3 if the contestant switches doors, and 1/3 if they do not switch.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值