Codeforces Round #284 (Div. 2) C. Crazy Town

来源:互联网 发布:java 选择汉字验证码 编辑:程序博客网 时间:2024/05/13 19:05

原题链接:

http://codeforces.com/contest/499/problem/C


C. Crazy Town
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and biare not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.

Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).

Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.

Input

The first line contains two space-separated integers x1y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.

The second line contains two integers separated by a space x2y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at.

The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106|ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).

Output

Output the answer to the problem.

Sample test(s)
input
1 1-1 -120 1 01 0 0
output
2
input
1 1-1 -131 0 00 1 01 1 -3
output
2
Note

Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):




题意:给出若干直线方程以及A,B两点的坐标,现在要从A点走到B点,规则是每次走一步只能跨越一个相邻区域,相邻区域是指由相邻边的区域,不包括有相邻点的区域,问至少需要多少步。

思路:只需要建立AB两点的直线方程,由于n的范围很小,枚举和每一条直线的交点,若该交点在以AB为对定点的矩形内或者边界上,则需要跨过该直线,具体证明就不例举了。呃。其实我也没有证明。。

其他:

要注意变量设置成y1会报错,所以后来用yy代替了,求交点就是数学公式了。为什么当时没想出这个做法呢。。加油吧~~每晚A一题感觉很好~不过量还是少了~


代码:

#include "stdio.h"#include "iostream"#include "string.h"#include "stdlib.h"#include "algorithm"using namespace std;#define eps double x1,x2,yy,y2,x,y,a,b,c,a1,b1,c1;int n,ans=0;int jude(){if(x1<x2){if(x<x1||x>x2)return 0;}else{if(x<x2||x>x1)return 0;}if(yy<y2){if(y<yy||y>y2)return 0;}else{if(y<y2||y>yy)return 0;}return 1;}int xiangjiao(){if(a*b1==b*a1)return 0;if(a==0){y=-c/b;x=(-c1-y*b1)/a1;}else if(a1==0){y=-c1/b1;x=(-c-y*b)/a;}else{double u=a1/a;y=(-c1+u*c)/(b1-u*b);x=(-c1-y*b1)/a1;}if(jude())return 1;return 0;}int main(){scanf("%lf%lf%lf%lf",&x1,&yy,&x2,&y2);scanf("%d",&n);a1=yy-y2;b1=-x1+x2;c1=x1*y2-x2*yy;for(int i=0;i<n;i++){scanf("%lf%lf%lf",&a,&b,&c);if(xiangjiao())ans++;}printf("%d\n",ans);return 0;}


0 0
原创粉丝点击