中南大学算法设计与分析实验报告.doc_第1页
中南大学算法设计与分析实验报告.doc_第2页
中南大学算法设计与分析实验报告.doc_第3页
中南大学算法设计与分析实验报告.doc_第4页
中南大学算法设计与分析实验报告.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

姓名:学号:0909122824班级:信安1202指导老师:李敏算法设计与分析基础实验报告实验一 分治 最近点对1 问题ProblemHave you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.InputThe input consists of several test cases. For each case, the first line contains an integer N (2 = N = 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.OutputFor each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places. 2 分析思路 题目是给n个点的坐标,求距离最近的一对点之间距离的一半。第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标。 首先,假设点是n个,编号为1到n。找一个中间的编号mid,先求出1到mid点的最近距离设为d1,还有mid+1到n的最近距离设为d2。如果说最近点对中的两点都在1-mid集合中,或者mid+1到n集合中,则d就是最小距离了。但是还有可能的是最近点对中的两点分属这两个集合,若存在,则把这个最近点对的距离记录下来,去更新d。这样就得到最小的距离d了。3 源代码#include#include#includeusing namespace std;#define N 1000010struct point double x; double y;p1N,p2N;double dis ( point a , point b ) return sqrt( pow (a.x-b.x,2) + pow ( a.y-b.y,2 ) );double min ( double a , double b ) return ab?a:b;bool cpx ( point a , point b ) return a.x b.x ;bool cpy ( point a , point b ) return a.y b.y ;double mindis ( int l , int r ) if( l + 1 = r ) return dis ( p1l ,p1r ); if( l + 2 = r ) return min ( dis ( p1l , p1l+1 ) , min ( dis ( p1l+1 , p1r ) , dis ( p1l , p1r ) ) ); else int mid , count=0 ; double mini; mid = ( l + r) /2 ; mini = min ( mindis ( l , mid ) , mindis ( mid+1 , r ) ); for( int i = l ; i = r ; i+ ) if ( fabs ( p1i.x - p1mid.x ) = mini ) p2count+=p1i; sort ( p2 , p2+count , cpy ); for ( int i=0 ; i count ; i+ ) for ( int j = i+1; j =mini) break; else if(dis (p2j,p2i)mini) mini=dis(p2j,p2i); return mini; int main() int n ; double dia ; while(scanf(%d,&n)=1&n) for(int i=0;in;i+) scanf(%lf%lf,&p1i.x,&p1i.y); sort ( p1 , p1 + n-1 , cpx ); dia = mindis ( 0 , n-1 ); printf(%.2fn, dia / 2 ); return 0;4 运行结果实验二 回溯 堡垒问题一问题Problem DescriptionSuppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. InputThe input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a . indicating an open space and an uppercase X indicating a wall. There are no spaces in the input file. OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.二分析思路 对于每个点,若能放置大炮则能选择放或者不放两种情况,若不能放置大炮则就只有一种情况。直接搜索,回溯的时候把点的状态恢复.三源代码#include#include#include#define MAX_SIZE 10char mapMAX_SIZEMAX_SIZE;int d42=0,1,0,-1,1,0,-1,0;int N,cnt;int ok(int x, int y) if(mapxy!=.) return 0; int i,xx,yy; for(i=0;i=0 & xx=0 & yyN & mapxxyy!=X) if(mapxxyy=o) return 0; xx+=di0; yy+=di1; return 1;int search(int x, int y, int c) int i,j; for(j=y;jN;j+) if(ok(x,j) mapxj=o; search(x,j+1,c+1); mapxj=.; for(i=x+1;iN;i+) for(j=0;jcnt) cnt=c; return 0;int main() int i; while(scanf(%d,&N)&N) for(i=0;iN;i+) scanf(%s,mapi); cnt=0; search(0,0,0); printf(%dn,cnt); return 0;四运行结果实验三 动态规划 免费馅饼一问题Problem Description都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼) Input输入数据有多组。每组数据的第一行为以正整数n(0n100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0T100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。 Output每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。二分析思路 将所有的时间段和馅饼看成是一个矩阵,时间就是行数,掉馅饼的就是列数,则就是数字三角形问题,从最底层找一条路径,使得路径上的和最大。 状态转移方程为:dpij=max(dpi+1j-1,dpi+1j,dpi+1j-1)+pieij。pieij为时间i时在j位置掉的馅饼数目。三源代码#include #include #include #define MAX 100001int pieMAX12;int Max(int a, int b) return (a b)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论