Home php教程 php手册 uva10012 不是你想象的那么简单的

uva10012 不是你想象的那么简单的

Jun 21, 2016 am 08:48 AM
center int lt put size

道题第一次做以为是一般的回溯题,最初的思路就是将各圆形全排列,放置新圆的时候让其与前一个圆相切,最后通过回溯得到矩形的最小size。十几分钟编完后结果WA,想了好一会发现问题所在,不能只让其与前一个圆相切(如果第一个圆很大,半径比方说是100,第二个圆很小,半径是1,第三个圆也很大,半径同样是100,放第三个圆的时候如果是和第二个圆相切则必定会与第一个圆相交,就不可以了)。然后就开始修改代码,然后就遇到了各种错误,断断续续地弄了一晚上加一上午。

首先想的是每次放一个圆形的时候还是先让其与前一个放置的圆相切,然后判断它与再之前放的圆是否相交,如不相交,则说明这样放是正确且最节省距离的,如果有任何一个圆与其相交,则说明不能与前一个圆相切,就再往前推一个,让其与前前个圆相切,再判断是否和别的圆相交。。。为了判断是否相交,增加了一个center数组存储各圆的圆心位置。到此思路很正确很清晰,然后遇到了三个Wa的点。

WA1:判断矩形最小size的时候不能用最后一个圆的最右侧位置了,因为有可能最后一个圆很小,其最右侧的位置还不如其前一个圆的最右侧位置远,所以改用判断各圆圆心位置+半径的最大值作为矩形的最小size。

WA2:在放置前几个圆的时候注意不能让圆的圆心位置-圆的半径<0。比如说第一个圆半径是1,第二个元半径100,放第二个元就不能让其与第一个圆相切因为那样圆的左边就超出矩形盒子的壁了!

WA3:主函数中MinL设置的太小,估计测试数据的数有很大的,题目没有说半径最大是多少,开始设的是65536,结果WA,改成DBL_MAX就AC了!!

#include#include#include#include#includeusing namespace std;  
  
int m,Put[10];          //Put[i]:放置的第i个圆的编号  
double MinL,size[10],center[10];//size[i]:编号为i的圆的半径;center[i]:放置的第i个圆的圆心位置  
bool vis[10];  
  
double getlen(int a,int b)      //计算两圆心间的距离  
{  
    return sqrt((size[a]+size[b])*(size[a]+size[b])-(size[a]-size[b])*(size[a]-size[b]));  
}  
bool isok(int a,int b)          //是否和之前放的圆相交  
{  
    for (int i=0;i&lt;b;i++)  
    {  
        if (sqrt((center[i]-center[a])*(center[i]-center[a])+(size[Put[i]]-size[Put[a]])*(size[Put[i]]-size[Put[a]]))&lt;(size[Put[i]]+size[Put[a]]))  
            return false;  
    }  
    return true;  
}  
void dfs(int cur)  
{  
    int i,j;  
    if (cur==m)  
    {     
        double maxsize=0;  
        for (int k=0;kmaxsize)  
                maxsize=center[k]+size[Put[k]];  
        }  
        if (maxsize&lt;MinL)  
            MinL=maxsize;  
    }  
    else  
    {  
        for (i=0;i=0;j--)  
                {  
                    tmpl=getlen(Put[j],i);  
                    center[cur]=center[j]+tmpl;  
                    if (center[cur]-size[Put[cur]]&gt;n;  
    while(n--)  
    {  
        cin&gt;&gt;m;  
        MinL=DBL_MAX;  
        memset(vis,0,sizeof(vis));  
        for (int i=0;i&gt;size[i];  
        for (int i=0;i&lt;m;i++)  
        {  
            Put[0]=i;  
            center[0]=size[i];  
            vis[i]=1;  
            dfs(1);  
            vis[i]=0;  
        }  
        cout&lt;&lt;fixed&lt;&lt;setprecision(3)&lt;&lt;MinL&lt;&lt;endl;  
    }  
    return 0;  
}
Copy after login

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Acer Care Center service is still initializing [Fixed] Acer Care Center service is still initializing [Fixed] Mar 16, 2024 am 10:55 AM

Acer Care Center service is still initializing [Fixed]

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use java's File.length() function to get the size of the file

Detailed explanation of the method of converting int type to bytes in PHP Detailed explanation of the method of converting int type to bytes in PHP Mar 06, 2024 pm 06:18 PM

Detailed explanation of the method of converting int type to bytes in PHP

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

C++ program to convert double type variable to int type C++ program to convert double type variable to int type Aug 25, 2023 pm 08:25 PM

C++ program to convert double type variable to int type

How to solve the problem that SpringBoot2's PUT request cannot receive parameters How to solve the problem that SpringBoot2's PUT request cannot receive parameters May 20, 2023 pm 08:38 PM

How to solve the problem that SpringBoot2's PUT request cannot receive parameters

What is the value range of int32? What is the value range of int32? Aug 11, 2023 pm 02:53 PM

What is the value range of int32?

How many numbers does java int have? How many numbers does java int have? Mar 06, 2023 pm 04:09 PM

How many numbers does java int have?

See all articles