2014-05-10 22:58

题目链接

原题:

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 = , int _y = ): x(_x), y(_y) {};
}; struct hashFunctor {
size_t operator () (const Point &p) {
return p.x * + 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[], int y[], int px, int py)
{
int sum = ; sum += twoArea(x[], y[], x[], y[], px, py);
sum += twoArea(x[], y[], x[], y[], px, py);
sum += twoArea(x[], y[], x[], y[], px, py);
return sum == twoArea(x[], y[], x[], y[], x[], y[]);
} void DFS(int x[], int y[], int px, int py, point_set &um, point_set &visited)
{
static const int dir[][] = {
{-, },
{+, },
{, -},
{, +}
};
int i;
int newx, newy; visited.insert(Point(px, py));
um.insert(Point(px, py)); for (i = ; i < ; ++i) {
newx = px + dir[i][];
newy = py + dir[i][];
if (visited.find(Point(newx, newy)) == visited.end() &&
inside(x, y, newx, newy)) {
DFS(x, y, newx, newy, um, visited);
}
}
} void insidePoints(int x[], int y[], vector<Point> &points)
{
point_set um;
point_set visited;
int mx, my; mx = (x[] + x[] + x[]) / ;
my = (y[] + y[] + y[]) / ;
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[];
int y[];
vector<Point> points;
int i;
int n; while (cin >> x[] >> y[]) {
cin >> x[] >> y[];
cin >> x[] >> y[];
insidePoints(x, y, points);
n = (int)points.size();
for (i = ; i < n; ++i) {
cout << points[i].x << ' ' << points[i].y << endl;
}
points.clear();
} return ;
}

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. Mir2源码详解之服务端-选择(角色)网关(SelGate)

    其实,SelGate也就是 LoginGate,其源码实现完全相同.不必怀疑,市面上的都是这么做~!这里单独写这篇文章,就是为了说明这点!

  2. CSS中属性position位置详解功能讲解与实例分析

    position有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页制作时,没有指定 ...

  3. CentOS6 更改Mysql数据库的数据存放位置

    mysql使用yum安装时,默认的数据是存储在/var/lib/mysql下.一般情况下,为了数据的安全性,建议将mysql数据库的数据文件存储在系统的第二块磁盘上的目录下可以按照以下步骤进行操作: ...

  4. 反编译工具(.NET Reflector )

    1.下载安装原程序,然后运行 Reflector.exe 2.运行注册机,点击 Generate 获得序列号 3.把序列号填写到 Reflector 软件上4.断开网络,点击 Activate 激活( ...

  5. HTTP协议的URI及各种方法

    每个Web服务器资源都有一个名字,这样客户端就可以说明他们感兴趣的资源是什么了,服务器资 源名被统称为:统一资源标识符(Uniform Resource Identifier, URI) Joe的五金 ...

  6. Laravel 安装predis 扩展

    在安装predis扩展之前先安装composer,安装教程在https://getcomposer.org/download/: php -r "copy('https://getcompo ...

  7. Java数字处理

    给出一个不多于5位的正整数,要求如下: (1)求出该数是几位数. (2)分别打印出每一位数字. (3)按照逆序打印出各位数值. 按照以上要求,首先得用户从键盘输入一个不多于5位的正整数,可以用Syst ...

  8. TextView字符串波浪式跳动--第三方开源---JumpingBeans

    在github上有一个开源项目:JumpingBeans,其项目主页是:https://github.com/frakbot/JumpingBeans JumpingBeans将一个普通的Androi ...

  9. win7修改软件【授权给…】后面的名称

    很简单,按下图打开注册表,修改: Ctrl+F分别搜索以下三个键值,搜到后修改为你想显示的名称即可即可. 1:UserName 2:RegisteredOrganazition 3:Registere ...

  10. IOS学习2

    1. #import,#include 和@class的区别 都引用一个类,根本定义区别:#include ,#import会把所有的copy一份到该文件 #import比#include的优势,im ...