2014-05-10 22:58

题目链接

原题:

  1. 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,直到找出所有点。搜索过程中,需要判断点是否在三角形内部。我的判断方法是计算面积。对于整数问题,计算公式不要引入浮点数,误差是不必要的。所以海伦公式不可行,用行列式计算面积更为方便、准确。

代码:

  1. // http://www.careercup.com/question?id=5120588943196160
  2. #include <iostream>
  3. #include <unordered_set>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct Point {
  8. int x;
  9. int y;
  10. Point(int _x = , int _y = ): x(_x), y(_y) {};
  11. };
  12.  
  13. struct hashFunctor {
  14. size_t operator () (const Point &p) {
  15. return p.x * + p.y;
  16. };
  17. };
  18.  
  19. struct equalFunctor {
  20. bool operator () (const Point &p1, const Point &p2) {
  21. return p1.x == p2.x && p1.y == p2.y;
  22. };
  23. };
  24.  
  25. typedef unordered_set<Point, hashFunctor, equalFunctor> point_set;
  26.  
  27. int twoArea(int x1, int y1, int x2, int y2, int x3, int y3)
  28. {
  29. return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
  30. }
  31.  
  32. bool inside(int x[], int y[], int px, int py)
  33. {
  34. int sum = ;
  35.  
  36. sum += twoArea(x[], y[], x[], y[], px, py);
  37. sum += twoArea(x[], y[], x[], y[], px, py);
  38. sum += twoArea(x[], y[], x[], y[], px, py);
  39. return sum == twoArea(x[], y[], x[], y[], x[], y[]);
  40. }
  41.  
  42. void DFS(int x[], int y[], int px, int py, point_set &um, point_set &visited)
  43. {
  44. static const int dir[][] = {
  45. {-, },
  46. {+, },
  47. {, -},
  48. {, +}
  49. };
  50. int i;
  51. int newx, newy;
  52.  
  53. visited.insert(Point(px, py));
  54. um.insert(Point(px, py));
  55.  
  56. for (i = ; i < ; ++i) {
  57. newx = px + dir[i][];
  58. newy = py + dir[i][];
  59. if (visited.find(Point(newx, newy)) == visited.end() &&
  60. inside(x, y, newx, newy)) {
  61. DFS(x, y, newx, newy, um, visited);
  62. }
  63. }
  64. }
  65.  
  66. void insidePoints(int x[], int y[], vector<Point> &points)
  67. {
  68. point_set um;
  69. point_set visited;
  70. int mx, my;
  71.  
  72. mx = (x[] + x[] + x[]) / ;
  73. my = (y[] + y[] + y[]) / ;
  74. DFS(x, y, mx, my, um, visited);
  75.  
  76. point_set::const_iterator usit;
  77. for (usit = um.begin(); usit != um.end(); ++usit) {
  78. points.push_back(Point(usit->x, usit->y));
  79. }
  80. um.clear();
  81. visited.clear();
  82. }
  83.  
  84. int main()
  85. {
  86. int x[];
  87. int y[];
  88. vector<Point> points;
  89. int i;
  90. int n;
  91.  
  92. while (cin >> x[] >> y[]) {
  93. cin >> x[] >> y[];
  94. cin >> x[] >> y[];
  95. insidePoints(x, y, points);
  96. n = (int)points.size();
  97. for (i = ; i < n; ++i) {
  98. cout << points[i].x << ' ' << points[i].y << endl;
  99. }
  100. points.clear();
  101. }
  102.  
  103. return ;
  104. }

Careercup - Microsoft面试题 - 5120588943196160的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. Android中SearchView修改字体颜色

    首先获取searchView控件,比如在actionbar上获取: SearchView searchView = (SearchView) menu.findItem(R.id.action_sea ...

  2. JavaScript根据CSS的Media Queries来判断浏览设备的方法

    CSS 部分 首先随便新建一个用来做判断的类,然后通过 Media Queries 来对这个类的 z-index 属性赋予不同的值.这个类仅作为 JavaScript 读取使用,所以需要将其移出屏幕窗 ...

  3. 如何在某个按钮上触发 bootstarp Modal 组件

    <?php Modal::begin([ 'id'=>'myModal', 'header' => '<h2>标题</h2>']); echo '内容'; M ...

  4. 使用userdel命令删除Linux用户

    serdel是什么 userdel 是一个底层用于删除用户的工具.在 Debian 上,我们通常会使用 deluser 命令.userdel 会查询系统账户文件,例如 /etc/password 和  ...

  5. 遇到困难 jsp代码onclick="javascript:return(checklogin());"报错

    <script language="javascript"> function checklogin() { if (document.getElementById(& ...

  6. IE中console的正确使用方法

    本文出处原文链接 转载请注明出处 http://www.cnblogs.com/havedream/p/4519538.html 问题来源:最近在学习easyui,观看的视频教程是孙宇的<EAS ...

  7. jQuery 遍历each()的使用方法

    .each()是一个for循环的包装迭代器.each()通过回调的方式处理,并且会有2个固定的实参,索引与元素(从0开始计数).each()回调方法中的this指向当前迭代的dom元素 遍历方法: & ...

  8. 【PHP】文件上传限制

    上传文件,只判断后缀,貌似还不是很严谨; /** * 判断文件是否合法 * @param $files * @param $arrCode * @return number|boolean */ fu ...

  9. phpmyadmin误删表后的恢复过程(心惊胆跳啊)

    话说今天不知道是抽风了还是失魂了,在用phpmyadmin删除测试数据时,竟然将整个表删除了: 等程序运行出错时,才出现整个表都没有了,而且之前也没有备份好!这下蛋疼了,这个可是production服 ...

  10. DOS下更改编码方式

    使用CHCP命令,CHCP是Change Code Page的缩写. 936 简体中文GBK 437 美国英语 65001 UTF编码 如:chcp 65001则将dos窗口中的字符编码改为UTF编码 ...