您的位置:首页 > 其它

计算几何—Toy 叉积判断点与线段的相对位置

2015-06-16 16:02 323 查看


A - TOYS

<span class="crawlInfo" id="crawling" style="display: none;"><strong><span style="color:green;">Crawling in process...</span></strong>		 </span>		 <span class="crawlInfo" id="crawlFailed" style="display: none;"><strong><span style="color:red;">Crawling failed</span></strong>		 </span>		 <span class="crawlInfo" id="crawlSuccess"><strong>Time Limit:</strong><span id="timeLimit">2000</span>MS    			 <strong>Memory Limit:</strong><span id="memoryLimit">65536</span>KB    			 <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span>		 </span>

Submit			 Status

<div style="margin: auto; width: 960px;"><div class="hiddable" id="vj_description" style="display: block;"><p class="pst">Description</p><div class="textBG"><div class="ptx" lang="en-US">	Calculate the number of toys that land in each bin of a partitioned toy box.
	Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.

	John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
<img src="http://poj.org/images/2318_1.jpg" alt="" />
	For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.</div></div></div><div class="hiddable" id="vj_input" style="display: block;"><p class="pst">Input</p><div class="textBG"><div class="ptx" lang="en-US">	The input file contains one or more problems. The first line of a problem consists of six integers, n m <span data-scayt_word="x1" data-scaytid="1">x1</span> <span data-scayt_word="y1" data-scaytid="3">y1</span> <span data-scayt_word="x2" data-scaytid="6">x2</span> <span data-scayt_word="y2" data-scaytid="8">y2</span>. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (<span data-scayt_word="x1" data-scaytid="2">x1</span>,<span data-scayt_word="y1" data-scaytid="4">y1</span>) and (<span data-scayt_word="x2" data-scaytid="7">x2</span>,<span data-scayt_word="y2" data-scaytid="9">y2</span>), respectively. The following n lines contain two integers per line, <span data-scayt_word="Ui" data-scaytid="11">Ui</span> Li, indicating that the ends of the <span data-scayt_word="i-th" data-scaytid="13">i-th</span> cardboard partition is at the coordinates (<span data-scayt_word="Ui" data-scaytid="12">Ui</span>,<span data-scayt_word="y1" data-scaytid="5">y1</span>) and (Li,<span data-scayt_word="y2" data-scaytid="10">y2</span>). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, <span data-scayt_word="Xj" data-scaytid="14">Xj</span> <span data-scayt_word="Yj" data-scaytid="15">Yj</span> specifying where the <span data-scayt_word="j-th" data-scaytid="16">j-th</span> toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.</div></div></div><div class="hiddable" id="vj_output" style="display: block;"><p class="pst">Output</p><div class="textBG"><div class="ptx" lang="en-US">	The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.</div></div></div><div class="hiddable" id="vj_sampleInput" style="display: block;"><p class="pst">Sample Input</p><div class="textBG"><pre class="sio">5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
 5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0


Sample Output

0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2


Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.


//上述是问题描述//

##include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;
const int max_n = 100000;
int Up[max_n] = { 0 };
int Down[max_n] = { 0 };
int ans[max_n + 1] = { 0 };
struct Vector
{
	int x, y;
	Vector(){};
	Vector(int _x, int _y)//以原点为起点的向量坐标
{
x = _x;
	y = _y;
	}
	Vector(int _x1, int _y1, int _x2, int _y2)//以其他的任意两点的向量
{
x = _x2 - _x1;
	y = _y2 - _y1;
	}
	Vector operator +(const Vector &b)
	{
	return (Vector(x + b.x, y + b.y));
	}
	Vector operator -(const Vector &b)
	{
	return (Vector(x - b.x, y - b.y));
	}
	int operator *(const Vector &b)
	{
	return (x*b.x + y*b.y);
	}
	int operator ^(const Vector &b)
	{
	return (x*b.y - y*b.x);
	}

};

int main()
{
	int n, m;
	int x1, y1, x2, y2;
	void BinarySearch(int x, int y, int card_n, int y1, int y2);
	while (cin >> n)
	{
	if (n != 0)
	{
	cin >> m >> x1 >> y1 >> x2 >> y2;

	for (int i = 1; i < n + 1; i++)
	{
	cin >> Up[i] >> Down[i];

	}
	Up[0] = x1;
	Down[0] = x1;
	Up[n + 1] = x2;
	Up[n + 1] = x2;
	for (int i = 0; i < m; i++)
	{
	int x, y;
	cin >> x >> y;
	BinarySearch(x, y, n, y1, y2);

	}
	for (int i = 0; i < n + 1; i++)
	{
	cout << i << ":" << ans[i] << endl;
	}
	}
	else
	{
	break;
	}
	memset(ans,0,sizeof(ans));

	}

	system("pause");
	return 0;
}
void BinarySearch(int x, int y, int card_n,int y1,int y2)
{
	int left, right;
	left = 0;
	right = card_n+1;
	int mid;
	int temp;
	while (right >left+1)
	{
	mid = (right + left) / 2;
	struct Vector card = Vector(Down[mid], y2, Up[mid], y1);
	struct Vector toy = Vector(Down[mid], y2, x, y);
	int flag = toy^card;

	if (flag < 0)
	{
	//temp = mid;
	right = mid;
	}
	else
	{
	left = mid;
	}
	}
	ans[left]++;

}include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;
const int max_n = 100000;
int Up[max_n] = { 0 };
int Down[max_n] = { 0 };
int ans[max_n + 1] = { 0 };
struct Vector
{
 int x, y;
 Vector(){};
 Vector(int _x, int _y)//以原点为起点的向量坐标
 {
  x = _x;
  y = _y;
 }
 Vector(int _x1, int _y1, int _x2, int _y2)//以其他的任意两点的向量
 {
  x = _x2 - _x1;
  y = _y2 - _y1;
 }
 Vector operator +(const Vector &b)
 {
  return (Vector(x + b.x, y + b.y));
 }
 Vector operator -(const Vector &b)
 {
  return (Vector(x - b.x, y - b.y));
 }
 int operator *(const Vector &b)
 {
  return (x*b.x + y*b.y);
 }
 int operator ^(const Vector &b)
 {
  return (x*b.y - y*b.x);
 }

};

int main()
{
 int n, m;
 int x1, y1, x2, y2;
 void BinarySearch(int x, int y, int card_n, int y1, int y2);
 while (cin >> n)
 {
  if (n != 0)
  {
   cin >> m >> x1 >> y1 >> x2 >> y2;

   for (int i = 1; i < n + 1; i++)
   {
    cin >> Up[i] >> Down[i];

   }
   Up[0] = x1;
   Down[0] = x1;
   Up[n + 1] = x2;
   Up[n + 1] = x2;
   for (int i = 0; i < m; i++)
   {
    int x, y;
    cin >> x >> y;
    BinarySearch(x, y, n, y1, y2);

   }
   for (int i = 0; i < n + 1; i++)
   {
    cout << i << ":" << ans[i] << endl;
   }
  }
  else
  {
   break;
  }
  memset(ans,0,sizeof(ans));

 }

 system("pause");
 return 0;
}
void BinarySearch(int x, int y, int card_n,int y1,int y2)
{
 int left, right;
 left = 0;
 right = card_n+1;
 int mid;
 int temp;
 while (right >left+1)
 {
     mid = (right + left) / 2;
  struct Vector card = Vector(Down[mid], y2, Up[mid], y1);
  struct Vector toy = Vector(Down[mid], y2, x, y);
  int flag = toy^card;
  
  if (flag < 0)
  {
   //temp = mid;
   right = mid;
  }
  else
  {
   left = mid;
  }
 }
 ans[left]++;

}
总结:该题用到了计算几何中通过向量的叉积来判断两个向量之间顺逆时针的方法,即通过顺逆时针的判断来确定点与直线(线段)的相对位置关系
该题是计算几何中一个入门的题,需要自己独立认真完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: