SRM144 DIV1 1100 point

Problem Statement

    

NOTE: There are images in the examples section of this problem statement that help describe the problem. Please view the problem statement in the HTML window to view them.

Given a picture composed entirely of horizontal and vertical line segments, calculate the minimum number of times you must lift your pen to trace every line segment in the picture exactly n times.

Each line segment will be of the form "<x1> <y1> <x2> <y2>" (quotes for clarity), representing a segment from (x1,y1) to (x2,y2). Segments may cross each other. Segments may also overlap, in which case you should count the overlapping region as appearing in the drawing only once. For example, say the drawing were composed of two lines: one from (6,4) to (9,4), and one from (8,4) to (14,4). Even though they overlap from (8,4) to (9,4), you should treat the drawing as if it were a single line from (6,4) to (14,4). You would not need to lift your pen at all to trace this drawing.

Definition

    
Class: PenLift
Method: numTimes
Parameters: vector <string>, int
Returns: int
Method signature: int numTimes(vector <string> segments, int n)
(be sure your method is public)
    
 

Notes

- The pen starts on the paper at a location of your choice. This initial placement does not count toward the number of times that the pen needs to be lifted.

Constraints

- segments will contain between 1 and 50 elements, inclusive.
- Each element of segments will contain between 7 and 50 characters, inclusive.
- Each element of segments will be in the format "<X1>_<Y1>_<X2>_<Y2>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces.
- <X1>, <Y1>, <X2>, and <Y2> will each be between -1000000 and 1000000, inclusive, with no unnecessary leading zeroes.
- Each element of segments will represent a horizontal or vertical line segment. No line segment will reduce to a point.
- n will be between 1 and 1000000, inclusive.

Examples

0)  
    
{"-10 0 10 0","0 -10 0 10"}
1
Returns: 1

This picture looks like a plus sign centered at the origin. One way to trace this image is to start your pen at (-10,0), move right to (10,0), lift your pen and place it at (0,-10), and then move up to (0,10). There is no way to trace the picture without lifting your pen at all, so the method returns 1.

1)  
    
{"-10 0 0 0","0 0 10 0","0 -10 0 0","0 0 0 10"}
1
Returns: 1

The picture is the same as the previous example, except that it has been described with four line segments instead of two. Therefore, the method still returns 1.

2)  
    
{"-10 0 0 0","0 0 10 0","0 -10 0 0","0 0 0 10"}
4
Returns: 0

You are now required to trace each segment exactly 4 times. You can do so without lifting your pen at all. Start at (0,0). Move your pen left to (-10,0), then back right to (0,0), then left again to (-10,0), then right again to (0,0). You have now traced the first line segment 4 times. Repeat this process for the other three segments as well. Since no pen lifts were required, the method returns 0.

3)  
    
{"0 0 1 0",   "2 0 4 0",   "5 0 8 0",   "9 0 13 0",
"0 1 1 1", "2 1 4 1", "5 1 8 1", "9 1 13 1",
"0 0 0 1", "1 0 1 1", "2 0 2 1", "3 0 3 1",
"4 0 4 1", "5 0 5 1", "6 0 6 1", "7 0 7 1",
"8 0 8 1", "9 0 9 1", "10 0 10 1", "11 0 11 1",
"12 0 12 1", "13 0 13 1"}
1
Returns: 6

The picture looks like this:

To trace the picture using the minimum number of pen lifts, refer to the following diagram:

Start by placing your pen at the yellow dot. Trace the yellow square. Now lift your pen and place it on the red dot. Move downward, tracing the vertical line segment, and then around the perimeter of the red rectangle. Lift your pen again and place it on the green dot. Trace the green lines using the same method as you did for the red lines. Lift your pen a third time, placing it on the magenta dot. Trace the magenta lines in a similar fashion. You will need to lift your pen three more times to trace each of the leftover white segments, for a grand total of 6 pen lifts.

4)  
    
{"-2 6 -2 1",  "2 6 2 1",  "6 -2 1 -2",  "6 2 1 2",
"-2 5 -2 -1", "2 5 2 -1", "5 -2 -1 -2", "5 2 -1 2",
"-2 1 -2 -5", "2 1 2 -5", "1 -2 -5 -2", "1 2 -5 2",
"-2 -1 -2 -6","2 -1 2 -6","-1 -2 -6 -2","-1 2 -6 2"}
5
Returns: 3

This is an example of overlap. Once all the segments are drawn, the picture looks like this:

You would need to lift your pen 3 times to trace every segment in this drawing exactly 5 times.

5)  
    
{"-252927 -1000000 -252927 549481","628981 580961 -971965 580961",
"159038 -171934 159038 -420875","159038 923907 159038 418077",
"1000000 1000000 -909294 1000000","1000000 -420875 1000000 66849",
"1000000 -171934 628981 -171934","411096 66849 411096 -420875",
"-1000000 -420875 -396104 -420875","1000000 1000000 159038 1000000",
"411096 66849 411096 521448","-971965 580961 -909294 580961",
"159038 66849 159038 -1000000","-971965 1000000 725240 1000000",
"-396104 -420875 -396104 -171934","-909294 521448 628981 521448",
"-909294 1000000 -909294 -1000000","628981 1000000 -909294 1000000",
"628981 418077 -396104 418077","-971965 -420875 159038 -420875",
"1000000 -1000000 -396104 -1000000","-971965 66849 159038 66849",
"-909294 418077 1000000 418077","-909294 418077 411096 418077",
"725240 521448 725240 418077","-252927 -1000000 -1000000 -1000000",
"411096 549481 -1000000 549481","628981 -171934 628981 923907",
"-1000000 66849 -1000000 521448","-396104 66849 -396104 1000000",
"628981 -1000000 628981 521448","-971965 521448 -396104 521448",
"-1000000 418077 1000000 418077","-1000000 521448 -252927 521448",
"725240 -420875 725240 -1000000","-1000000 549481 -1000000 -420875",
"159038 521448 -396104 521448","-1000000 521448 -252927 521448",
"628981 580961 628981 549481","628981 -1000000 628981 521448",
"1000000 66849 1000000 -171934","-396104 66849 159038 66849",
"1000000 66849 -396104 66849","628981 1000000 628981 521448",
"-252927 923907 -252927 580961","1000000 549481 -971965 549481",
"-909294 66849 628981 66849","-252927 418077 628981 418077",
"159038 -171934 -909294 -171934","-252927 549481 159038 549481"}
824759
Returns: 19
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

这道题思路不难,但是还是感觉实现起来好费劲

思路是这样的

step1 将字符串数组编程数字线段列表

step2 去掉overlap的得到不重叠的线段组

step3 找交叉点,包括端点 记录每个点的交叉的出度 再乘以n

step4 一对奇数出度点代表 抬笔一次

这个程序太复杂,根本没写完,自己太挫了,觉得算overlap好麻烦啊

 #include <vector>
#include <list>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h> using namespace std; class PenLift{
public:
int numTimes(vector <string>, int);
};
int PenLift::numTimes(vector <string> segments, int n){
//turn string segment vector to int segment vector
int penlift = ;
int segnum = segments.size();
list<int*> seglist;
int i,j,k;
int start,space;
string sseg;
string snum; // string for single number
string::iterator iter;
for(i=;i<segnum;i++){
start=;
space=;
sseg=segments.at(i);
j=;
k=;
int nseg[];
for(iter=sseg.begin();iter!=sseg.end();iter++){
if(*iter==' '){
space = j;
snum=sseg.substr(start,space-start);
nseg[k]=atoi(snum.c_str());
start = space+;
k++;
}
j++;
}
snum=sseg.substr(start,j-start);
snum=sseg.substr(start,space-start);
nseg[k]=atoi(snum.c_str());
seglist.push_back(nseg);
}
//reduce overlap
list<int*>::iterator iteri,iterj,itert;
for(iteri=seglist.begin();iteri!=seglist.end();iteri++){
for(iterj=iteri++;iterj!=seglist.end();iterj++){
if((int*)(*iteri)[]==(int*)(*iterj)[]){
if(iterj[]<=iterj[]){
if(iteri[]>=iterj[]&&iteri[]<=iterj[]){
if(iteri[]>=iterj[]&&iteri[]<=iterj[]){ }else if( ){}
}
}else{ }
}
}
}
return penlift;
}

topcoder算法练习3的更多相关文章

  1. topcoder算法练习2

    Problem Statement      In most states, gamblers can choose from a wide variety of different lottery ...

  2. ITWorld:2014年全球最杰出的14位编程天才

    近日,ITWorld 整理全球最杰出的 14 位程序员,一起来看下让我们膜拜的这些大神都有哪些?(排名不分先后) 1.Jon Skeet 个人名望:程序技术问答网站 Stack Overflow 总排 ...

  3. BFS/DFS算法介绍与实现(转)

    广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...

  4. IT求职中,笔试、面试的算法准备

    PS:此文章为转载,源地址:http://www.newsmth.net/nForum/#!article/CoderInterview/849     作者应该是在美国进行的笔试面试,感觉面试的的公 ...

  5. *[topcoder]LCMSetEasy

    http://community.topcoder.com/stat?c=problem_statement&pm=13040 DFS集合全排列+LCM和GCD.但事实上,有更简单的算法,列在 ...

  6. *[topcoder]LittleElephantAndBalls

    http://community.topcoder.com/stat?c=problem_statement&pm=12758&rd=15704 topcoder的题经常需要找规律,而 ...

  7. [topcoder]KingdomReorganization

    http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...

  8. [topcoder]ActivateGame

    http://community.topcoder.com/stat?c=problem_statement&pm=10750&rd=14153 http://apps.topcode ...

  9. [topcoder]BestRoads

    http://community.topcoder.com/stat?c=problem_statement&pm=10172&rd=13515 http://community.to ...

随机推荐

  1. HDU-4902 Nice boat

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  2. Robot Framework安装

    Robot Framework(中文站/社交化知识社区,源码)是一款Python编写的通用开源功能测试自动化框架,以作验收测试和验收测试驱动开发(ATDD),它是一种使用表格测试数据语法的关键字驱动的 ...

  3. Building Apps with Over 65K Methods(解决APP引用方法总数超过65536)

    本文翻译自http://developer.android.com/intl/zh-cn/tools/building/multidex.html#about.主要介绍当我们Android App中函 ...

  4. linux平台MongoDB数据库安装

    跟Ruiy哥一起玩转吧; <一,初始化玩转MongoDB> 1,关闭SElinux(Ruiy哥根据经验知红帽的SElinux架设就是个错误,还记得不管啥结构首先要关闭的就是它); 2,设置 ...

  5. 层层递进Struts1(八)之总结

    先来看一下Struts1学习总结的思维导图,画的主要是Struts1中的重点和博客分布,如下所示: 系列博客的侧重点是: Struts1是什么? 为什么要使用它? 如何使用Struts1? Strut ...

  6. windows Nginx基本使用方法

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  7. 基于Centos6.x定制化安装步骤

    1.获取安装界面代码      挂载image/install.img:mount image/install.img /mnt/5 -o loop      复制挂载后的代码至self_intall ...

  8. list的三种遍历方法

    1.最简单的for循环遍历 for(int i = 0; i < list.size(); i++){        list.get(i);        } 2.最方便的foreach循环遍 ...

  9. Memo打印1

              Delphi 打印Memo里面的内容 实现的功能和记事本的打印的功能一样 打印保存为文件时此时的文件名如何设置? 当Memo里的文本数量巨大时 窗体正在打印会出现点数字显示问题 闪 ...

  10. [转] CSS float 浮动属性

    http://www.cnblogs.com/polk6/p/3142187.html CSS float 浮动属性 本篇主要介绍float属性:定义元素朝哪个方向浮动. 目录: 1. 页面布局方式: ...