POJ 1788 Building a New Depot 解题报告

Description

Advanced Cargo Movement, Ltd. is successfully expanding. In order to meet new demands on truck maintenance, the management of the company decided to build a new truck depot. A suitable lot for building a depot was purchased and a construction company ``Masonry and Fences for Future, Ltd.'' was hired to build a depot. 

The area of the depot will be enclosed by a fence. The fence is supposed to enclose a connected area of a lot and each part of the fence follows North-South or East-West direction. At each place where the fence changes its direction, there is a single post. The posts are only at points where the fence changes the direction, i.e., there are no unnecessary posts. When MFF workers have built all of the posts, they lost the plan of a depot being built. At this moment, they asked you for a help. 

Given the coordinates of all the posts, your task is to compute the length of the fence. 

Input

The input consists of several blocks. The first line of each block contains a single number P, 1 <= P <= 100 000. P is the number of posts which have been built. Each of the following P lines contains two integers X and Y, 0 <= X, Y <= 10 000, which represent coordinates of the posts in MFF internal units (which no one else is able to understand). No two posts have the same coordinates. 

Each block is followed by a single empty line and the input is terminated by a line containing a single number 0. 

Output

Output contains a single line for each block. The line should contain the text "The length of the fence will be L units.", where L is replaced by an actual length of the fence. You can assume that the fence can always be built.

Sample Input

6
1 1
1 3
3 3
2 1
3 2
2 2

0

Sample Output

The length of the fence will be 8 units.


题目大意:就是数字是柱子的坐标,没两根柱子之间有栅栏 求栅栏的长度。。。
解题思路:两次排序 第一次按X排,第二次按y排,,,,然后分别把差值加起来

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<math.h>
 4 using namespace std;
 5 struct weizhi{
 6     int x;
 7     int y;
 8 }a[200000];
 9 bool cmp1(weizhi a,weizhi b)
10 {
11     if(a.x==b.x)
12         return a.y<b.y;
13     return a.x<b.x;
14 }
15 bool cmp2(weizhi a,weizhi b)
16 {
17     if(a.y==b.y)
18         return a.x<b.x;
19     return a.y<b.y;
20 }
21 int main()
22 {
23     long int n;
24     while(scanf("%d",&n)!=EOF)
25     {
26         if(n==0)break;
27         int i;
28         for(i=0;i<n;i++)
29         {
30             scanf("%d %d",&a[i].x,&a[i].y);
31         }
32         int sum=0;
33         sort(a,a+n,cmp1);
34         for(i=0;i<n;i+=2)
35         {
36             sum+=a[i+1].y-a[i].y;
37         }
38         sort(a,a+n,cmp2);
39         for(i=0;i<n;i+=2)
40         {
41             sum+=a[i+1].x-a[i].x;
42         }
43         printf("The length of the fence will be %d units.\n",sum);
44     }
45     return 0;
46 }
View Code

 

转载于:https://www.cnblogs.com/wangrunwen/p/4083160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1915是一道经典的搜索题目,也被称为“Knight Moves”。下面我将为您提供解题思路和解题步骤: 1. 题目描述 在8x8的国际象棋棋盘上,棋子“马”从初始位置出发,允许走日字形的移动。给定目标位置,求出从初始位置到目标位置最少需要几步。 2. 解题思路 这道题目可以采用广度优先搜索(BFS)算法解决。将初始位置加入队列中,然后依次处理队列中的每个位置,根据“马”的走法,生成下一步可以到达的位置,如果这个位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数。 3. 解题步骤 具体的解题步骤如下: (1)将初始位置加入队列中,并记录步数为0。 (2)依次处理队列中的每个位置,生成下一步可以到达的位置。 (3)如果下一步可以到达的位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数为当前位置的步数+1。 (4)重复步骤(2)和(3),直到队列为空。 (5)如果目标位置被访问过,则返回到达目标位置的步数,否则返回-1。 4. 注意事项 在实现BFS算法的过程中,需要注意以下几点: (1)需要记录每个位置是否被访问过,以避免重复访问。 (2)需要注意边界条件,防止数组越界。 (3)需要注意判断目标位置是否合法,即是否在棋盘范围内。 5. 总结 本题是一道经典的搜索题目,采用BFS算法可以求解出从初始位置到目标位置最少需要几步。在解题过程中,需要注意边界条件和目标位置是否合法等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值