spoj HS12MBR - Minimum Bounding Rectangle

Compute the Minimum Bounding Rectangle (MBR) that surrounds the given set of 2D objects, i.e., the axis-aligned rectangle, which contains all of the specified objects and is the one with minimum area among all rectangles with this property.

Input

First, you are given t (t<100) - the number of test cases.

Each of the test cases starts with one integer n (n < 100) - the number of objects in the set. In the successive n lines, the descriptions of the objects follow.

Each object is described by one character and some parameters:

  • a point: p x y, where x and y are point coordinates.
  • a circle: c x y r, where x and y are the center coordinates and r is the radius of the circle.
  • a line segment: l x1 y1 x2 y2, where xi, yi are the coordinates of the endpoints of the line.

Successive test cases are separated by an empty line.

Output

For each of the test cases output four numbers - the coordinates of the two points that correspond to the lower left and the upper right corner of the MBR, in the following order: first the x-coordinate of the lower left corner, then the y-coordinate of the lower left corner, the x-coordinate of the upper right corner and the y-coordinate of upper right corner.

You can assume that all object parameters are integersand that -1000 -1000 1000 1000 is a bounding rectangle for of all of them.

Example

Input:
3
1
p 3 3 

2
c 10 10 20
c 20 20 10

1
l 0 0 100 20

Output:
3 3 3 3 
-10 -10 30 30
0 0 100 20

Test case description

test 1: points only    (2 pts)
test 2: circles only   (2 pts) 
test 3: lines only     (2 pts)
test 4: mixed          (2 pts)
test 5: mixed          (2 pts)

题意 :给出一些几何图形,点,圆,线段,求能将其包含的最小矩形(与x轴,y轴平行)

思路:实际上就是比较x,y轴上找到对应的最小值和最大值

代码 如下:

<?php
	$t = fgets(STDIN);	
	$t = intval(trim($t));
	while ($t--)
	{
		$n = fgets(STDIN);	
		$n = intval(trim($n));
				
		$x1 = 1001; $y1 = 1001; $x2 = -1001; $y2 = -1001;
		while ($n--)
		{
			$line = fgets(STDIN);	
			$data = explode(' ', trim($line));

			if ('p' == $data[0])	
			{
				$x1 = min($x1, intval($data[1]));
				$x2 = max($x2, intval($data[1]));
				$y1 = min($y1, intval($data[2]));
				$y2 = max($y2, intval($data[2]));	
			}
			else if ('c' == $data[0])
			{
				$x = intval($data[1]); $y = intval($data[2]); $r = intval($data[3]);
				$x1 = min($x1, $x - $r);
				$y1 = min($y1, $y - $r);
				$x2 = max($x2, $x + $r);
				$y2 = max($y2, $y + $r);	
			}
			else if ('l' == $data[0])
			{
				$px1 = intval($data[1]); $py1 = intval($data[2]); $px2 = intval($data[3]); $py2 = intval($data[4]);
				if ($px1 > $px2)
				{
					$tmp = $px1; $px1 = $px2; $px2 = $tmp;
				}

				if ($py1 > $py2)	
				{
					$tmp = $py1; $py1 = $py2; $py2 = $tmp;
				}

				$x1 = min($x1, $px1);
				$y1 = min($y1, $py1);
				$x2 = max($x2, $px2);
				$y2 = max($y2, $py2);	
			}
		}
		echo $x1, ' ', $y1, ' ', $x2, ' ', $y2, ' ', PHP_EOL;
		if (0 != $t) fgets(STDIN);
	}	
	
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值