Careercup - Microsoft面试题 - 5120588943196160
2014-05-10 23:23
309 查看
2014-05-10 22:58
题目链接
原题:
题目:给定三个点,找出所有这三点组成的三角形内的整点。(不能组成三角形也无所谓,结果为空即可。)
解法:出题者没有说是整点,但如果不是整点,就有无穷多个了。求整点的个数可以用Pick定律的公式。要求出所有整点的话,我的方法是找出一个内部的整点,然后向四个方向进行DFS,直到找出所有点。搜索过程中,需要判断点是否在三角形内部。我的判断方法是计算面积。对于整数问题,计算公式不要引入浮点数,误差是不必要的。所以海伦公式不可行,用行列式计算面积更为方便、准确。
代码:
题目链接
原题:
Three points are given A(x1, y1), B(x2, y2), C(x3, y3). Write a method returning an array of points (x, y) inside the triangle ABC.
题目:给定三个点,找出所有这三点组成的三角形内的整点。(不能组成三角形也无所谓,结果为空即可。)
解法:出题者没有说是整点,但如果不是整点,就有无穷多个了。求整点的个数可以用Pick定律的公式。要求出所有整点的话,我的方法是找出一个内部的整点,然后向四个方向进行DFS,直到找出所有点。搜索过程中,需要判断点是否在三角形内部。我的判断方法是计算面积。对于整数问题,计算公式不要引入浮点数,误差是不必要的。所以海伦公式不可行,用行列式计算面积更为方便、准确。
代码:
// http://www.careercup.com/question?id=5120588943196160 #include <iostream> #include <unordered_set> #include <vector> using namespace std; struct Point { int x; int y; Point(int _x = 0, int _y = 0): x(_x), y(_y) {}; }; struct hashFunctor { size_t operator () (const Point &p) { return p.x * 10000 + p.y; }; }; struct equalFunctor { bool operator () (const Point &p1, const Point &p2) { return p1.x == p2.x && p1.y == p2.y; }; }; typedef unordered_set<Point, hashFunctor, equalFunctor> point_set; int twoArea(int x1, int y1, int x2, int y2, int x3, int y3) { return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); } bool inside(int x[3], int y[3], int px, int py) { int sum = 0; sum += twoArea(x[0], y[0], x[1], y[1], px, py); sum += twoArea(x[1], y[1], x[2], y[2], px, py); sum += twoArea(x[2], y[2], x[0], y[0], px, py); return sum == twoArea(x[0], y[0], x[1], y[1], x[2], y[2]); } void DFS(int x[3], int y[3], int px, int py, point_set &um, point_set &visited) { static const int dir[4][2] = { {-1, 0}, {+1, 0}, {0, -1}, {0, +1} }; int i; int newx, newy; visited.insert(Point(px, py)); um.insert(Point(px, py)); for (i = 0; i < 4; ++i) { newx = px + dir[i][0]; newy = py + dir[i][1]; if (visited.find(Point(newx, newy)) == visited.end() && inside(x, y, newx, newy)) { DFS(x, y, newx, newy, um, visited); } } } void insidePoints(int x[3], int y[3], vector<Point> &points) { point_set um; point_set visited; int mx, my; mx = (x[0] + x[1] + x[2]) / 3; my = (y[0] + y[1] + y[2]) / 3; DFS(x, y, mx, my, um, visited); point_set::const_iterator usit; for (usit = um.begin(); usit != um.end(); ++usit) { points.push_back(Point(usit->x, usit->y)); } um.clear(); visited.clear(); } int main() { int x[3]; int y[3]; vector<Point> points; int i; int n; while (cin >> x[0] >> y[0]) { cin >> x[1] >> y[1]; cin >> x[2] >> y[2]; insidePoints(x, y, points); n = (int)points.size(); for (i = 0; i < n; ++i) { cout << points[i].x << ' ' << points[i].y << endl; } points.clear(); } return 0; }
相关文章推荐
- Careercup - Microsoft面试题 - 6366101810184192
- Careercup - Microsoft面试题 - 5673934611546112
- Careercup - Microsoft面试题 - 5684901156225024
- Careercup - Microsoft面试题 - 5204967652589568
- Careercup - Microsoft面试题 - 6543214668414976
- Careercup - Microsoft面试题 - 5700293077499904
- Careercup - Microsoft面试题 - 6337018766295040
- Careercup - Microsoft面试题 - 5428361417457664
- Careercup - Microsoft面试题 - 5188169901277184
- Careercup - Microsoft面试题 - 5680049562845184
- Careercup - Microsoft面试题 - 6282862240202752
- Careercup - Microsoft面试题 - 5485521224597504
- Careercup - Microsoft面试题 - 4840369632051200
- Careercup - Microsoft面试题 - 5173689888800768
- Careercup - Microsoft面试题 - 5672369481842688
- Careercup - Microsoft面试题 - 6314866323226624
- Careercup - Microsoft面试题 - 5718181884723200
- Careercup - Microsoft面试题 - 5917873302142976
- Careercup - Microsoft面试题 - 6751316000899072
- Careercup - Microsoft面试题 - 5799446021406720