fzu 2148 Moon Game
题意:判断凸四边形
凸四边形:没有角大于180的四边形。
思路:
不能直接用凸包,要用的话要逆排序点然后使用。
面积法:如果4个点中存在某个点D,Sabd + Sacd + Sbcd = Sabc,则说明是凹四边形。
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <list> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const double eps = 1e-8; //定义成double类型 struct point { int x, y; } p[50]; double area(point a, point b, point c) { return (fabs((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x))/2); //一定要fabs(),有可能为负的, } bool ok(point a, point b, point c, point d) { if(fabs(area(b,c,d)-area(a,b,c)-area(a,c,d)-area(a,b,d)) < eps) return false; return true; } int main() { int t, i, j, n, k, ans, a, b; scanf("%d", &t); for(k = 1; k <= t; k++) { ans = 0; scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y); if(n<4) printf("Case %d: %d", k, ans); else { for(i = 0; i < n; i++) for(j = i+1; j < n; j++) for(a = j+1; a < n; a++) for(b = a+1; b < n; b++) if(ok(p[i],p[j],p[a],p[b])&&ok(p[j],p[i],p[a],p[b]) &&ok(p[a],p[i],p[j],p[b])&&ok(p[b],p[i],p[j],p[a])) { ans++; } printf("Case %d: %d\n", k, ans); } } return 0; }
相交法:只要有一对对角线相交,就是凸四边形。
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <list> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const double eps = 1e-8; //定义成double类型 class Point { public: int x, y; Point():x(0), y(0) {} Point(int xx, int yy): x(xx), y(yy) {} }; Point p[35]; int n; void init() { cin >> n; for(int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; } bool judge1(Point a, Point b, Point c, Point d) //判断两条线段是否相交 { long long m1 = (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); long long m2 = (b.x - a.x) * (d.y - a.y) - (d.x - a.x) * (b.y - a.y); long long m3 = (d.x - c.x) * (a.y - c.y) - (a.x - c.x) * (d.y - c.y); long long m4= (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x); if(m1*m2 <= 0 && m3 *m4 <= 0) return true; else return false; } bool judge2(int i, int j, int k) //判断3个点是否在一条直线上 { int x1 = p[i].x, x2 = p[j].x, x3 = p[k].x; int y1 = p[i].y, y2 = p[j].y, y3 = p[k].y; if((y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)) return false; else return true; } int solve() { int cnt = 0; for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { for(int k = j+1; k < n; k++) { for(int m = k + 1; m < n; m++) { if(judge1(p[i], p[j], p[k], p[m])||judge1(p[i], p[m], p[k], p[j]) || judge1(p[i], p[k], p[j], p[m])) cnt++; } } } } return cnt; } int main() { int T; scanf("%d", &T); for(int i = 1; i <= T; i++) { init(); int ans = solve(); cout << "Case " << i << ": " << ans << endl; } return 0; }